Title meaning: Given a number a (1<=a<=9) and a nonnegative integer N (0<=n<=100000), the sum of the numbers and S = A + AA + AAA + ... + AA ... A (n a)
At first, I thought it was a big number, but thought about it and felt it was regular.
Solution idea: N A sum divided by 10 of the remainder is the result of the current bit, quotient for the new carry, thus can get the result of each bit
The code is as follows (with comments):
#include <iostream>using namespace Std;int a,n;int sum[100005],k=0;//sum every bit of the result in reverse order int main () {cin>>a> >n;if (n==0) {Cout<<0<<endl;return 0;} int c=0;//c is the result of the carry for (int i=n;i>=1;i--) {c+=i*a;sum[k++]=c%10;//i A sum divided by 10 and the remainder of the current bit c=c/10;//quotient for the new carry}if (c!=0)// C is 0 does not output the highest carry C {cout<<c;} for (int i=k-1;i>=0;i--)//Reverse output {cout<<sum[i];} Cout<<endl;return 0;}
PAT 2-06. Sum of series (20)