JAVA Blue Bridge Cup algorithm improves the addition of large numbers

JAVA Blue Bridge Cup algorithm improves the addition of large numbers

Resource constraints
Time limit: 1.0s Memory Limit: 256.0MB

Problem Description
  input two positive integers a, b, the output value of a + b.

Input format
  two lines, the first line a, the second line b. a and b are less than the length of 1000.

Output format
  line, a + b values.

Sample input
. 4
2

Sample output
6

import java.util.Scanner;



public class Main {
	public static void main(String[] args) {
		Scanner sca=new Scanner(System.in);
		String s1=sca.next();
		String s2=sca.next();
		int len1=s1.length();
		int len2=s2.length();
		int maxlen=len1>len2?len1:len2;
		int[] ss =new int[maxlen+1];
		while(s1.length()<maxlen) {
			s1="0"+s1;
		}
		while(s2.length()<maxlen) {
			s2="0"+s2;
		}
		int temp=0;
		for(int i=maxlen-1;i>=0;i--) {
			int a=Integer.valueOf(s1.charAt(i)-'0');
			int b=Integer.valueOf(s2.charAt(i)-'0');
			int c=a+b+temp;
			if(c>=10) {
				temp=c/10;
				c=c%10;
			}else {
				temp=0;
			}
			ss[i]=c;
		}
		if(temp!=0) {
			System.out.print(temp);
		}
		for(int i=0;i<maxlen;i++) {
			System.out.print(ss[i]);
		}
		
	}
	
}


Published 17 original articles · won praise 0 · Views 376

Guess you like

Origin blog.csdn.net/qq_36551453/article/details/104469423
Recommended