看了题解之后发现确实比我更有规律...
妙啊妙啊
我的:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 long long int n, k, m = 1, p = 0;//k表示增加的次数,m表示第几位 5 long long int num = 1;//num表示当前的数 6 7 void duan(long long int k) 8 { 9 if(m == n) 10 { 11 cout << num; 12 p = 1; 13 } 14 15 m ++; 16 num += 1; 17 18 for(long long int i = 1; i < k; i ++) 19 { 20 if(m == n) 21 { 22 cout << num; 23 p = 1; 24 break; 25 } 26 m ++; 27 num += 2; 28 } 29 30 if(p == 0) duan(k + 1); 31 } 32 33 int main() 34 { 35 cin >> n; 36 if(n == 1) 37 { 38 cout << 1; 39 return 0; 40 } 41 else duan(2); 42 43 return 0; 44 }
题解:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 long long n, cnt = 0; 7 cin >> n; 8 while(n >= cnt + 1) 9 { 10 cnt ++; 11 n -= cnt; 12 } 13 if(n == 0) cout << cnt * cnt << endl; 14 if(n == 1) cout << cnt * cnt + 1 << endl; 15 if(n > 1) cout << cnt * cnt + 1 + 2*(n-1) << endl; 16 17 return 0; 18 }
标签:cnt,编程,cout,int,题解,T5,long,num,挑战赛 From: https://www.cnblogs.com/marswithme/p/16852433.html