编程语言
首页 > 编程语言> > 算法之数学验证尼科彻斯定理

算法之数学验证尼科彻斯定理

作者:互联网

 

 分析和思路:

接受输入数据后,从1开始遍历,找到连续相加和的连续奇数即可。back变量的设置有些技巧。

 1 #include <iostream>
 2 #include "iostream"
 3 #include "string"
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9 
10     int number = 0;
11     while (cin >> number)
12     {
13         int b[100] = { 0 };
14         int a = 1;
15         
16         while (1)
17         {
18             int back = a;
19             int sum = 0;
20             for (int i = 0; i < number; i++)
21             {
22                 b[i] = back;
23                 back = back + 2;
24             }
25             for (int i = 0; i < number; i++)
26             {
27                 sum += b[i];
28             }
29             if (sum == number * number*number)
30             {
31             //    cout << "yes";
32                 for (int i = 0; i < number-1; i++)
33                 {
34                     cout << b[i] << "+";
35                     
36                 }
37                 cout << b[number - 1]<<endl;
38                 break;
39             }
40             else
41             {
42                 a = a + 2;
43                 sum = 0;
44                 continue;
45             }
46         }
47     }
48 
49     return 0;
50 }

 

标签:斯定理,尼科,int,sum,number,back,++,算法,include
来源: https://www.cnblogs.com/technologykeeping/p/15966394.html