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

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

问题描述

在最近的一次采访中,我被问到一个非常奇怪的问题。面试官问我如何使用编译器功能计算1 + 2 + 3 + ... + 1000。这意味着我不允许编写程序并执行它,但我应该编写一个程序,可以驱动编译器在编译时计算这个总和,并在编译完成时打印结果。作为提示,他告诉我,我可能会使用编译器的泛型和预处理器功能。可以使用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???

这个问题与没有任何循环计算总和无关在这里问。另外,应该注意,总和应该在编译期间计算。使用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 ++模板进行编译时解决问题称为元编程即可。这是Erwin Unruh博士在标准化C ++语言过程中偶然发现的一种技术。您可以在元编程的维基页面上阅读有关此主题的更多信息。
似乎可以使用Java注释在Java中编写程序。您可以查看下面的 maress's 答案。

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.

一本关于C ++元编程的好书是这一个。如果感兴趣,值得一看。

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

一个有用的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;
};

Testcode:

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的输出:


错误:声明'struct sum_from< 1u> :: to< 1000u> :: equals< 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天全站免登陆