waiting...'s Blog

1+2+……+10^n

 

  1. //题目描述:
  2. //传说中数学王子高斯小时候利用规律很快的将1+2+……+100算出来
  3. //现在有一个类似的问题,就是要求1+2+……+10^n.
  4. //你能很快地算出来么?
  5. //
  6. //输入:
  7. //多个case,每个case只有一个自然数 n (0<=n<=10000)
  8. //
  9. //输出:
  10. //上面那个问题的结果
  11. //
  12. //样例输入:
  13. //1
  14. //2
  15. //
  16. //样例输出:
  17. //55
  18. //5050
  19.  
  20.  
  21. #include "stdafx.h"
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27.         int n;
  28.         void calculate(int n);
  29.         while (cin >> n)
  30.                 calculate(n);
  31.         return 0;
  32. }
  33.  
  34. void calculate(int n)
  35. {
  36.         if (n == 0)
  37.         {
  38.                 cout << "1" << endl;
  39.                 return;
  40.         }
  41.  
  42.         cout << "5";
  43.        
  44.         int num_0 = n;
  45.         while (--num_0 > 0)
  46.                 cout << "0";
  47.  
  48.         cout << "5";
  49.  
  50.         num_0 = n;
  51.         while (--num_0 > 0)
  52.                 cout << "0";
  53.  
  54.         cout << endl;
  55. }

 




Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee