• <bdo id='Kk7Eg'></bdo><ul id='Kk7Eg'></ul>
    <tfoot id='Kk7Eg'></tfoot>
    <i id='Kk7Eg'><tr id='Kk7Eg'><dt id='Kk7Eg'><q id='Kk7Eg'><span id='Kk7Eg'><b id='Kk7Eg'><form id='Kk7Eg'><ins id='Kk7Eg'></ins><ul id='Kk7Eg'></ul><sub id='Kk7Eg'></sub></form><legend id='Kk7Eg'></legend><bdo id='Kk7Eg'><pre id='Kk7Eg'><center id='Kk7Eg'></center></pre></bdo></b><th id='Kk7Eg'></th></span></q></dt></tr></i><div id='Kk7Eg'><tfoot id='Kk7Eg'></tfoot><dl id='Kk7Eg'><fieldset id='Kk7Eg'></fieldset></dl></div>
  • <small id='Kk7Eg'></small><noframes id='Kk7Eg'>

    <legend id='Kk7Eg'><style id='Kk7Eg'><dir id='Kk7Eg'><q id='Kk7Eg'></q></dir></style></legend>

        如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?

        时间:2023-10-15
      1. <small id='Y0yP0'></small><noframes id='Y0yP0'>

        • <bdo id='Y0yP0'></bdo><ul id='Y0yP0'></ul>
          <i id='Y0yP0'><tr id='Y0yP0'><dt id='Y0yP0'><q id='Y0yP0'><span id='Y0yP0'><b id='Y0yP0'><form id='Y0yP0'><ins id='Y0yP0'></ins><ul id='Y0yP0'></ul><sub id='Y0yP0'></sub></form><legend id='Y0yP0'></legend><bdo id='Y0yP0'><pre id='Y0yP0'><center id='Y0yP0'></center></pre></bdo></b><th id='Y0yP0'></th></span></q></dt></tr></i><div id='Y0yP0'><tfoot id='Y0yP0'></tfoot><dl id='Y0yP0'><fieldset id='Y0yP0'></fieldset></dl></div>

              <legend id='Y0yP0'><style id='Y0yP0'><dir id='Y0yP0'><q id='Y0yP0'></q></dir></style></legend>
                <tfoot id='Y0yP0'></tfoot>

                    <tbody id='Y0yP0'></tbody>

                  本文介绍了如何在编译时驱动 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++ 语言的过程中偶然发现的一种技术.您可以在 元编程的 wiki 页面上阅读有关此主题的更多信息.似乎可以使用 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++ 元编程的好书是 this一个.有兴趣的值得一看.

                  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;
                  };
                  

                  测试代码:

                  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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Java ClassLoader:两次加载相同的类 下一篇:编译成java字节码(不使用Java)

                  相关文章

                  • <bdo id='p2lf3'></bdo><ul id='p2lf3'></ul>

                  <small id='p2lf3'></small><noframes id='p2lf3'>

                  <i id='p2lf3'><tr id='p2lf3'><dt id='p2lf3'><q id='p2lf3'><span id='p2lf3'><b id='p2lf3'><form id='p2lf3'><ins id='p2lf3'></ins><ul id='p2lf3'></ul><sub id='p2lf3'></sub></form><legend id='p2lf3'></legend><bdo id='p2lf3'><pre id='p2lf3'><center id='p2lf3'></center></pre></bdo></b><th id='p2lf3'></th></span></q></dt></tr></i><div id='p2lf3'><tfoot id='p2lf3'></tfoot><dl id='p2lf3'><fieldset id='p2lf3'></fieldset></dl></div>

                  <legend id='p2lf3'><style id='p2lf3'><dir id='p2lf3'><q id='p2lf3'></q></dir></style></legend>
                    <tfoot id='p2lf3'></tfoot>