Project euler:problem powerful digit sum

Source: Internet
Author: User

A googol (10100) is a massive number:one followed by one-hundred zeros; 100100 is almost unimaginably large:one followed by two-hundred zeros. Despite their size, the sum of the digits in each number are only 1.

Considering natural numbers of the form, ab, where A, B <, what is the maximum digital sum?



Large number multiplication

It's basically a copy of the code from the previous 29 questions.


#include <iostream> #include <string>using namespace std;string num2str (int n) {string ans = ""; while (n) {int a = n 10;char B = A + ' 0 '; ans = b + ans;n/= 10;} return ans;} void Reverse_data (String &data) {Char temp = ' 0 '; int start = 0;int end = Data.size ()-1;while (Start < end) {temp = Data[start];d ata[start++] = Data[end];d ata[end--] = temp;}}  void Compute_value (String lhs, String rhs, String &result) {reverse_data (LHS); Reverse_data (RHS); int i = 0, j = 0, res_i  = 0;int Tmp_i = 0;int carry = 0;for (i = 0; I! = Lhs.size (); ++i, ++tmp_i) {res_i = tmp_i; In each calculation, the bits of the result store need to be incremented for (j = 0; J! = Rhs.size (); ++j) {carry + = (result[res_i]-' 0 ') + (Lhs[i]-' 0 ') * (Rhs[j]-' 0 ');    Note here that each calculation does not guarantee that the rounding of the previous calculation results is eliminated, and that the previous calculation results are also considered. result[res_i++] = (carry% + ' 0 '); carry/= 10;} while (carry)//multiplier A calculation is complete, there may be some carry no processing {result[res_i++] = (carry% + ' 0 '); carry/= 10;}} for (int i = Result.size ()-1; I >= 0; i--) {if (result[i]! = ' 0 ') break;elseresult.pop_back ();} Reverse_datA (result);} int Count_num (string s) {int count = 0;for (int i = 0; i < s.length (); i++) {count + = s[i]-' 0 ';} return count;} int Powe (string a, int b) {string res = a;for (int II = 1; II < b; ii++) {string tmp (Res.length () + a.length (), ' 0 '); comp Ute_value (Res, A, tmp); res = tmp;} int count = Count_num (res); return count;} int main () {int maxc = 0;for (int a = 1; a <, a++) {for (int b = 1; b <; b++) {string s = Num2str (a); int ans = Powe (s, b); if (ans>maxc) maxc = ans;}} cout << maxc << endl;system ("pause"); return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Project euler:problem powerful digit sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.