Zoj problem set-1874 primary arithmetic

Source: Internet
Author: User

Time Limit: 2 seconds memory limit: 65536 KB

Children are taught to add multi-digit numbers from right-to-left one digit at a time. please find the "carry" operation-in which a 1 is carried from one digit position to be added to the next-to be a significant challenge. your job is to count the number of carry operations for each of a set of addition problems so that educators may assess their difficulty.

Input

Each line of input contains two unsigned integers less than 10 digits. The last line of input contains 0 0.

Output

For each line of input records t the last you showould compute and print the number of carry operations that wowould result from adding the two numbers, in the format shown below.

Sample Input
123 456
555 555
123 594
0 0

Sample output
No carry operation.
3 carry operations.
1 carry operation.

Source:University of Waterloo local contest 200000009.23

 
1: # include <iostream>
 
2: # include <String>
 
3: # include <algorithm>
4:Using NamespaceSTD;
 
5:IntCountcarries (Const String& Pre1,Const String& Pre2)
 
6 :{
7: size_t loop = pre1.length ()> pre2.length ()? Pre1.length (): pre2.length ();
 
8:BoolCarry =False;
 
9:IntCarrycount = 0;
 
10:ShortTempresult;
11: size_t I = 0;
 
12:For(I = 0; I <loop; I ++)
 
13 :{
 
14: tempresult = (pre1 [I]-'0') * (I <pre1.length () + (pre2 [I]-'0 ') * (I <pre2.length () + (Short) Carry;
15: Carry = (tempresult> = 10 );
 
16: carrycount + = carry;
 
17 :}
 
18:ReturnCarrycount;
 
19 :}
20:IntMain (Void)
 
21 :{
 
22:StringOP1, OP2;
 
23:While(CIN> OP1> OP2 & (OP1! ="0"| OP2! ="0"))
24 :{
 
25: reverse (op1.begin (), op1.end ());
 
26: reverse (op2.begin (), op2.end ());
 
27:IntC = countcarries (OP1, OP2 );
 
28:If(C = 0)
29 :{
 
30: cout <"No carry operation."<Endl;
 
31 :}
 
32:Else If(C = 1)
 
33 :{
34: cout <C <"Carry operation."<Endl;
 
35 :}
 
36:Else
 
37 :{
38: cout <C <"Carry operations."<Endl;
 
39 :}
 
40 :}
 
41:Return0;
 
42 :}

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.