解题思路: 规律为 A(n)=A(n-1)*2+2;
#include "iostream" #include "algorithm" #include "string" using namespace std; int main() { int n; string s1; while (cin >> n) { s1 = '0'; for (int i = 0; i < n; i++) { string s2 = s1; int len1 = s1.size() - 1; int len2 = s2.size() - 1; int flag = 0, sum = 0; string str; while (len1 >= 0 && len2 >= 0) { sum = s1[len1--] - '0' + s2[len2--] - '0' + flag; flag = sum / 10; sum = sum % 10; str += char(sum + '0'); } while (len1 >= 0) { sum = s1[len1--] - '0' + flag; flag = sum / 10; sum = sum % 10; str += char(sum + '0'); } while (len2 >= 0) { sum = s2[len2--] - '0' + flag; flag = sum / 10; sum = sum % 10; str += char(sum + '0'); } if (flag) str += char(flag + '0'); reverse(str.begin(), str.end()); string s; int Len = str.size() - 1; sum = str[Len--] - '0' + 2; flag = sum / 10; sum = sum % 10; s += char(sum + '0'); while (Len >= 0) { sum = str[Len--] - '0' + flag; flag = sum / 10; sum = sum % 10; s += char(sum + '0'); } if (flag) s += char(flag + '0'); reverse(s.begin(), s.end()); s1 = s; } cout << s1 << endl; } return 0; }
0.0分
8 人评分
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:827 |
C语言程序设计教程(第三版)课后习题6.10 (C语言代码)浏览:1090 |
剪刀石头布 (C语言代码)浏览:1792 |
C语言程序设计教程(第三版)课后习题12.1 (C语言代码)浏览:689 |
数列排序 (C语言代码)浏览:674 |
班级人数 (C语言代码)浏览:981 |
1415题求解浏览:681 |
用筛法求之N内的素数。 (C语言代码) 详解………………浏览:1195 |
简单的a+b (C语言代码)浏览:676 |
字符逆序 (C语言代码)求大神指出错处,运行结果尝试了也与要求一样,但就是说结果错误,不知错在哪里浏览:446 |