HDU 1047 Integer Inquiry
JAVA水过
import java.io.*; import java.util.*; import java.math.*; public class aa { public static void main( String[]args ) { Scanner cin=new Scanner(System.in); int t; cin.hasNextInt(); t = cin.nextInt(); for( int i = 1; i <= t; ++i ) { BigInteger a,b,c; c = BigInteger.valueOf(0); b = c; while( cin.hasNextBigInteger() ) { a = cin.nextBigInteger(); if( a.intValue() == 0 )//这里不能直接=c,否则不过,必须转换成int break; b = b.add(a); } System.out.println( b ); if( i != t ) System.out.println(); } } }
大数相加模板题
#include<stdio.h> #include<string.h> const int Maxsize = 124; int sum[Maxsize]; char num[Maxsize]; void cal( ) { int len = strlen( num ); int t = 0; for( int i = 0; i < len ; ++i ) { sum[i] += ( t + num[len - i - 1] - '0' );//先相加 t = sum[i] / 10;//再进位 sum[i] %= 10; } while( t )//万一相加后位数比num数组长度达 { sum[len++] += t % 10; t /= 10; } } int main( ) { int t; scanf( "%d",&t ); int k = t; while( k-- ) { memset( sum,0,sizeof( sum ) ); while( scanf( "%s",num ),num[0]!= '0' ) cal( ); int len = Maxsize - 1; while( !sum[len] ) --len;//找长度 if( k != t - 1 ) puts( "" ); if( len < 0 )//特殊情况,不要漏了这一句 printf( "0" ); else for( ; len >= 0; --len ) printf( "%d",sum[len] ); puts( "" ); } return 0; }
本人还是新手 ,转载请注明来自Lvsi‘s home