标签:c++ project euler
The series, 11 + 22 + 33 + ... + 1010 = 10405071317.
Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.
#include <iostream> #include <string> using namespace std; string powe(int n) { string s = "1"; for (int i = 1; i <= n; i++) { int len = s.length(); int flag = 0; for (int j = len - 1; j >= 0; j--) { int tp = (s[j] - '0') * n + flag; flag = tp / 10; s[j] = tp % 10 + '0'; } if (flag != 0) { while (flag) { char a = flag % 10 + '0'; s = a + s; flag /= 10; } } } return s; } string pl(string a, string b) //加法 { string res = ""; if (a.length() < b.length()) { string t = a; a = b; b = t; } int lo = a.length(); int sh = b.length(); string s(lo - sh, '0'); b = s + b; int flag = 0; for (int i = lo - 1; i >= 0; i--) { int tmp = a[i] + b[i] - '0' - '0' + flag; int low = tmp % 10; flag = tmp / 10; char a1 = low + '0'; res = a1 + res; } if (flag != 0) res = "1" + res; return res; } int main() { string res = "0"; for (int i = 1; i <= 1000; i++) { string tmp = powe(i); res = pl(res, tmp); } int len = res.length(); string ans = ""; for (int i = len - 1 - 9; i < len; i++) ans = ans + res[i]; cout << ans << endl; system("pause"); return 0; }
Project Euler:Problem 48 Self powers
标签:c++ project euler
原文地址:http://blog.csdn.net/youb11/article/details/46398873