PAT 2-06. Sum of series (20)

Source: Internet
Author: User

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)

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.