Grab the root (ii) (HDU 4554 rebellious xiaoming hdu 1002 A + B problem II, the conversion of numbers (inversion), the addition of large numbers ... )

Source: Internet
Author: User

Reversal of Numbers:

is to save the numbers backwards. (*^__^*) hehe ...

General idea: Take the number one one out, there is an array inside, and then turn it into a number, output.

See the code.

1   while (a)           // Take each digit out and take it out 2         {3             num1[i]=a%;   // take each of you out there in the array, realize the reversal of the number 4             i++;            // changes to the array 5             a/=;           6         }

Example of the strike: Hdu 4554 rebellious Xiao Ming

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4554

The rebellious Xiao Ming

Time limit:3000/1000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 818 Accepted Submission (s): 568


Problem description Rebellious period of Xiao Ming all like to do the opposite, even look at the number is the same (minus), such as:
Xiao Ming would think of 1234 as 4321, 1234 as 4321, 230 as 032 (032=32), 230 as-032 ( -032=-32).

Now, Xiaoming has done some a+b and a-b topics (A, A is an integer and does not contain a leading 0), if you give the correct answers to these questions, can you guess what the answer will be for xiaoming?

Input inputs The first behavior of a positive integer T (t<=10000), indicating that Xiaoming did a total of T-questions.
Next T line, each line is two integers x, y ( -1000000<=x, y<=1000000), x represents the correct answer for A+b, and y represents the correct answer for a-B.
The input is guaranteed to be legal and does not need to consider A or B is a decimal condition.

Output outputs a total of T-lines, each line outputs two integers s t, separated by a space, where s represents the a+b answer that Xiaoming will get, t means that xiaoming will get a A-B answer.

Sample Input320 7-100-140 Output38 247 7-19-23 topic: Chinese title, clear ~ See the code.
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intnum1[Ten],num2[Ten];8 9 intMain ()Ten { One     intT; Ascanf"%d",&T); -      while(t--) -     { the         intX,y,a,b,i=0, j=0; -         intp=0, q=0; -scanf"%d%d",&x,&y); -memset (NUM1,0,sizeof(NUM1)); +memset (num2,0,sizeof(num2)); -A= (x+y)/2; +b=x-A; A          while(a) at         { -num1[i]=a%Ten; -i++; -A/=Ten; -         } -          while(b) in         { -num2[j]=b%Ten; toJ + +; +B/=Ten; -         } the          for(intk=0; k<i; k++)//Change to Numbers *p=num1[k]+p*Ten; $          for(intL=0; l<j; l++)Panax Notoginsengq=num2[l]+q*Ten; -printf ("%d%d\n", p+q,p-q); the     } +     return 0; A}

Large number of additions:

Number is large, if directly add. It's easy to timeout! So the other way is to directly take out a plus, but can not be replaced by numbers.

1 intl=l1>l2?l1:l2;//take one of the longest between two2      for(intI=0; i<l; i++)3     {4Num3[i]=num1[i]+num2[i];//One plus5         if(num3[i-1]>9&&i>=1)//consider rounding, if more than 9 is required .6         {7num3[i]++; 8         }9 Ten     } One     if(num[l-1]>9)//the last one's rounding problem A     { -num[l]++;//still need rounding . -l++;//The length needs to be added one the}

Example: HDU 1002 A + B problem II

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1002

A + B Problem II

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 238717 Accepted Submission (s): 46010


Problem Descriptioni has a very simple problem for you. Given integers A and B, your job is to calculate the Sum of A + B.

Inputthe first line of the input contains an integer T (1<=t<=20) which means the number of test cases. Then T lines follow, each line consists of both positive integers, A and B. Notice that the integers is very large, that M EANs should not process them by using 32-bit integer. Assume the length of each integer would not exceed 1000.

Outputfor Each test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line is the equation "A + b = sum", sum means the result of A + B. Note there is some spaces int the Equati On. Output a blank line between the test cases.

Sample Input21 2112233445566778899 998877665544332211

Sample outputcase 1:1 + 2 = 3Case 2:1.,122,334,455,667,79e,+17 + 998877665544332211 = 1111111111111111110 Refer to the above explanations. Take the number one one out and add it upside down, in the output ~
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intMain ()8 {9     intn,flag=1;Ten     Chara[8000],b[8000]; One     intnum1[8000],num2[8000],ans[8000]; Ascanf"%d",&n); -      while(n--) -     { the         intk=0, s=0; -scanf"%s%s", A, b); -printf ("Case %d:\n", flag++); -         intlen1=strlen (a); +         intLen2=strlen (b); -memset (NUM1,0,sizeof(NUM1)); +memset (num2,0,sizeof(num2)); Amemset (ans,0,sizeof(ans)); at         intL=len1>len2?Len1:len2; -          for(intI=len1-1; i>=0; i--) -         { -num1[k]=a[i]-'0'; -k++; -         } in          for(intj=len2-1; j>=0; j--) -         { tonum2[s]=b[j]-'0'; +s++; -         } the          for(intI=0; i<l; i++) *         { $ans[i]=num1[i]+Num2[i];Panax Notoginseng             if(i>=1) -                 if(ans[i-1]>9) the                 { +ans[i]++; A                 } the                 //cout<<num1[i]<< "" <<num2[i]<< "" <<ans[i]<<endl; +         } -         if(ans[l-1]>9) $         { $ans[l]=1; -l++; -         } theprintf ("%s +%s =", A, b); -          for(inti=l-1; i>=0; i--)Wuyiprintf"%d", ans[i]%Ten); the         if(n) -printf ("\ n"); Wuprintf ("\ n"); -     } About     return 0; $}

Grab the root (ii) (HDU 4554 rebellious xiaoming hdu 1002 A + B problem II, the conversion of numbers (inversion), the addition of large numbers ... )

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.