如何驱动器C#,C ++或Java编译器来计算1 + 2 + 3 + ... + 1000? [英] How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000?

查看:190
本文介绍了如何驱动器C#,C ++或Java编译器来计算1 + 2 + 3 + ... + 1000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的一次采访中,有人问我一个很奇怪的问题。面试官问我,我怎么能计算1 + 2 + 3 + ... + 1000只使用编译器功能。这意味着,我不能写一个程序并执行它,但我应该只是写一个程序,可以驱动编译器来计算这笔款项,而汇编和编纂完成时打印结​​果。作为一个暗示,他告诉我,我可能会使用的编译器的泛型和pre-处理器功能。它可以使用C ++,C#或Java编译器。任何想法???

In a recent interview, I was asked a really strange question. The interviewer asked me how can I compute 1+2+3+...+1000 just using compiler features. This means that I am not allowed to write a program and execute it, but I should just write a program that could drive the compiler to compute this sum while compilation and print the result when compilation completes. As a hint, he told me that I may use generics and pre-processor features of the compiler. It is possible to use C++, C# or Java compiler. Any ideas???

这个问题是不相关的计算总和没有任何环<一个href=\"http://stackoverflow.com/questions/4568645/printing-1-to-1000-without-loop-or-conditionals/4568650#4568650\">asked这里。此外,应当注意的是,总和应当编译期间进行计算。印刷只是使用C ++编译器的结果是指令不接受。

This question is not related to computing the sum without any loops asked here. In addition, It should be noted that the sum SHOULD be calculated during compilation. Printing just the result using C++ compiler directives is not acceptable.

编辑:

阅读更多关于公布答案,我发现,使用C ++模板编译过程中解决问题,就是所谓的元编程。这是由欧文博士安鲁意外地发现,标准化的C ++语言的过程中的技术。你可以阅读更多关于元编程的 wiki页面上的这个话题。
它似乎是可能的编写使用Java注释在Java程序。你可以看看
maress的 回答以下。

Reading more about the posted answers, I found that solving problems during compilation using C++ templates is called metaprogramming. This is a technique that was discovered accidentally by Dr. Erwin Unruh, during the process of standardizing the C++ language. You may read more about this topic on wiki page of meta-programming. It seems that it is possible to write the program in Java using java annotations. You may take a look at maress's answer below.

编辑2:

关于C ++的元编程一个很好的书是这个。值得如果有兴趣去看看。

A nice book about meta-programming in C++ is this one. Worth to take a look if interested.

修改3

一个有用的C ++元编程库是Boost的MPL 此链接

A useful C++ meta-programming library is Boost's MPL this link.

推荐答案

更新立即改进的递归深度!工程于MSVC10和GCC不增加深度。 :)

Updated Now with improved recursion depth! Works on MSVC10 and GCC without increased depth. :)

简单的编译时间递归+除了:

Simple compile-time recursion + addition:

template<unsigned Cur, unsigned Goal>
struct adder{
  static unsigned const sub_goal = (Cur + Goal) / 2;
  static unsigned const tmp = adder<Cur, sub_goal>::value;
  static unsigned const value = tmp + adder<sub_goal+1, Goal>::value;
};

template<unsigned Goal>
struct adder<Goal, Goal>{
  static unsigned const value = Goal;
};

测试code:

template<unsigned Start>
struct sum_from{
  template<unsigned Goal>
  struct to{
    template<unsigned N>
    struct equals;

    typedef equals<adder<Start, Goal>::value> result;
  };
};

int main(){
  sum_from<1>::to<1000>::result();
}

有关GCC输出:

错误:申报'结构sum_from&LT; 1U> ::为&lt; 1000U> ::等于&LT; 500500u>

error: declaration of ‘struct sum_from<1u>::to<1000u>::equals<500500u>’

上Ideone 的活生生的例子。

有关MSVC10输出:

Output for MSVC10:

error C2514: 'sum_from<Start>::to<Goal>::equals<Result>' : class has no constructors
      with
      [
          Start=1,
          Goal=1000,
          Result=500500
      ]

这篇关于如何驱动器C#,C ++或Java编译器来计算1 + 2 + 3 + ... + 1000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆