U E D R , A S I H C RSS

Primary Arithmetic/문보창

소감

2005/02/19 Accepted 0:00.027 64
비록 간단한 문제지만 코드를 최적화시켜 상위에 랭크되었다.

코드

~cpp 
// no10035 - Primary Arithmetic
#include <iostream> 
#include <cstring> 
using namespace std; 

void main() 
{ 
	int num1[10], num2[10];
	char str[22]; 
	int i, j, temp, count, carry, sumCarry; 
	bool turn; 
	while (true) 
	{ 
		cin.getline(str, 22, '\n'); 
		if (strcmp(str, "0 0") == 0) 
			break; 
		turn = false; 
		count = 0; 
		for (i=strlen(str)-1; i>=0; i--) 
		{ 
			if (turn)
				num1[count++] = str[i] - 48;
			else if (str[i] == ' ')	
			{ 
				for (j=count; j<10; j++)
					num2[j] = 0;
				turn = true; 
				count = 0; 
			} 
			else 
				num2[count++] = str[i] - 48; 
		} 
		for (j=count; j<10; j++)
			num1[j] = 0;
		sumCarry = carry = 0; 
		for (i=0; i<10; i++) 
		{ 
			temp = num1[i] + num2[i] + carry; 
			if (temp > 9) 
			{ 
				carry = temp / 10; 
				sumCarry += carry; 
			} 
			else 
				carry = 0; 
		} 
		if (sumCarry == 0) 
			cout << "No carry operation.\n"; 
		else if (sumCarry == 1) 
			cout << sumCarry << " carry operation.\n"; 
		else 
			cout << sumCarry << " carry operations.\n"; 
	} 
}  
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:24:02
Processing time 0.0160 sec