PAT(甲級)Practice1001--Java版

1001 A+BFromatjava

Calculate a+b and output the sum in standard format -- that is,the diits must be seperated into groups of three commas (unless there are less than for digits).git

 Input Spercification :less

Each input file contians one test case.Each case contains a pair of integers a and b where -10^6<a,b>10^6 .The numbers are sparated by a space.ide

Output Specification:spa

For each test case,you should output the sum of a and b in one line.The sum must be written in the standard format.code

Sample Input :orm

-1000000 9blog

Sample Output:
-999,991three

 

 1 package pat;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Main {
 6     private static Scanner sc;
 7     
 8     public static void main (String args[]) {
 9         int a ;
10         int b;
11         int sum;
12         sc=new Scanner (System.in);
13         a=sc.nextInt();
14         b=sc.nextInt();
15         sc.close();
16         sum =a+b;
17         String Ssum1=""+sum;
18         //String Ssum2=String.valueOf(sum);
19         System.out.println(Ssum1.length());
20         //System.out.println(Ssum2.length());
21         //System.out.println(Ssum1.substring(0,Ssum1.length()));
22         for(int i=0;i<Ssum1.length();i++)
23         {
24             //遍歷sum結果的全部字符
25             System.out.print(Ssum1.substring(i,i+1));
26             //在已獲得的結果上加上逗號
27             //if("-".equals(Ssum1.substring(i,i+1)))
28             //    continue;
29             if((i+1)%3==Ssum1.length()%3 && i!=Ssum1.length()-1)
30                 System.out.print(",");    
31  
32         }        
33     }
34     
35 }
View Code
相關文章
相關標籤/搜索