HDU 1047 Integer Inquiry

Integer Inquiry
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself. 

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output

370370367037037036703703703670


大数相加问题:

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
char str[105],sum[105],ssum[105];
int main(){
	char c;
	int len,i,flag,slen,k,j;
	//memset(sum,'0',sizeof(sum));
	flag=1;
	while(~scanf("%s",str)){
		len=strlen(str);
		if(len==1&&str[0]=='0')  break;
		if(flag){
			strcpy(sum,str);
			flag=0;
		} 
		else{
			slen=strlen(sum);
			for(k=0;len>=0||slen>=0;len--,slen--,k++){  
	            if(len>=0&&slen>=0) 
					ssum[k]=str[len]+sum[slen]-'0'+c;  
	            if(len<0&&slen>=0) 
					ssum[k]=sum[slen]+c;  
	            if(len>=0&&slen<0) 
					ssum[k]=str[len]+c;  
	            c=0;          //进位加上之后要清零  
	            if(ssum[k]>'9'){ 
					ssum[k]-=10;  
					c=1; 
				}  
			}
			if(c==1) ssum[k]='1';
			for(i=strlen(ssum)-1,j=0;i>=0,j<strlen(ssum)-1;j++,i--){
				sum[j]=ssum[i];
			}
			//printf("%s\n",sum);
		}
	}
	
	printf("%s\n",sum);
	return 0;
} 

JAVA:

import java.io.*;
import java.math.*;

class Bigint {

        public static void main(String[] args) throws Exception{
                        String s1 = null;
                        BigInteger bint = null;
                        BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
                        s1 = bin.readLine();
                        while(!s1.equals("0"))
                        {
                                if(bint == null)
                                {
                                        bint = new BigInteger(s1);
                                }
                                else
                                {
                                        bint = bint.add(new BigInteger(s1));
                                }
                                s1 = bin.readLine();
                        }
                        if(s1.equals("0"))
                        {
                                System.out.println(bint.toString());
                        }
        }

}




你可能感兴趣的:(HDU 1047 Integer Inquiry)