HDU 1229 or a + B

Source: Internet
Author: User

Or a + B Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 15760 accepted submission (s): 7662


Problem description reads two integers A and B smaller than 10000, and calculates a + B. Note that if K (no more than 8) digits at the end of A and B are the same, output-1 directly.
 
The input test input contains several test cases. Each test case occupies one row in the format of "A B K". There is a space interval between two adjacent numbers. When both A and B are 0, the input ends, and the corresponding results are not output.
 
Output outputs one line for each test case, that is, the value of A + B or-1.
 
Sample Input
1 2 111 21 1108 8 236 64 30 0 1
 
Sample output
3-1-1100
 

Idea: Use % to get the end of the number and mark it with a flag to determine how to jump out of the loop.


#include<stdio.h>int main(){    int A,B,K,sum;    while(scanf("%d%d%d",&A,&B,&K)&&A!=0&&B!=0)    {int i,temp,a,b;sum=A+B;for(i=0,temp=0;i<K;i++){a=A%10;b=B%10;if(a==b){A/=10;B/=10;}else{  temp=1;  break;}}if(temp)printf("%d\n",sum);elseprintf("-1\n");}return 0;}



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.