Given a and n, calculate a + aa + aaa +... Sum of a (n a) (Big Data Processing)

Source: Internet
Author: User
Description:

Given a and n, calculate the sum of a + aa + aaa + a... a (n.

Input:

There are multiple groups of test data. Input a, n (1 <= a <= 9,1 <= n <= 100 ).

Output:

Output results for each group of inputs.

Sample input:
1 10
Sample output:
1234567900

From the question, we can see that when a = 9, n = 100, the number of an int type is no less than 100 bits, so we cannot use the common method to find it, the following describes my solution. I declare a vector v to store a + aa + aaa +... the sum of a (n a), temp is used to store... for a (n a), it is added separately from a bit to a high bit, and hight is used to store carry.

# Include <iostream> # include <vector> using namespace std; int main () {int a, n; int sum = 0; vector <int> v; vector <int> temp; vector <int>: iterator it; int hight = 0; // store carry while (cin> a> n) {v. clear (); temp. clear (); v. push_back (a); temp. push_back (a); for (int I = 2; I <= n; I ++) {temp. push_back (a); int j = temp. size ()-1; int k = v. size ()-1; hight = 0; sum = 0; while (k> = 0 & j> = 0) {// sum = temp from low to high [j] + V [k] + hight; hight = 0; if (sum> 9) {hight = sum/10; // returns the bitwise} v [k] = sum % 10; k --; j --;} // if (hight> 0) {while (j> = 0) {// you may need to add more numbers than the total number of digits, for example, 9 + 99; sum = temp [j] + hight; hight = 0; if (sum> 9) {hight = sum/10;} v. insert (v. begin (), sum % 10); j --;} if (hight> 0) {// if there is a carry, put it to the highest digit v. insert (v. begin (), hight);} //} for (it = v. begin (); it! = V. end (); it ++) {cout <* it;} cout <endl;} return 0 ;}

Result: a = 9 n = 200, a + aa + aaa + a... a = 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110910


The following is a simple solution: directly simulate the primary addition from the single digit to add, the carry, and then store it in a stack. Finally, the output of the stack is finished. Code:

#include <cstdio>#include <stack>using namespace std;int main(){//    freopen("1.txt", "r", stdin);    int a, n, i, t, c;    while(~scanf("%d %d", &a, &n))    {        stack<int> S;        for(c=0,i=1; i<=n; i++)        {            t = (n-i+1)*a;            S.push((t+c)%10);            c = (t+c)/10;        }        if(c>0)            S.push(c);        while(!S.empty())        {            printf("%d", S.top());            S.pop();        }        printf("\n");    }    return 0;}

However, this disadvantage is that when n is large, t = (n-I + 1) * a will overflow, and this program runs quickly, the above code runs slowly when n is large. But it does not overflow.

Related Article

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.