#UVa:424-Integer Inquiry

大數加法即可得解。

C++(0.008)

/*******************************************************/
/* UVa 424 Integer Inquiry                             */
/* Author: LanyiKnight [at] knightzone.org             */
/* Version: 2012/02/02                                 */
/*******************************************************/
#include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int answer[1005] = {0};
  int digit = 0;
  string input;
  while( getline( cin, input ) && !(input[0] == '0' && input[1] == 0)){
    digit = input.length();
    for( int i = 0 ; i < digit ; i++ ){
      answer[i] += (input[digit-i-1] - '0');
      answer[i+1] += answer[i]/10;
      answer[i] %= 10;
    }
  }
  int i;
  for( i = 1004 ; i >= 0 ; i-- )
    if( answer[i] ) break;
  if( i < 0 ) i = 0;
  for( ; i >= 0 ; i-- )
    printf( "%d", answer[i] );
  printf( "\n" );
  return 0;
}

發表迴響