PAT 1001. A+B Format (20)

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

Input

Each input file contains one test case. Each case contains a pair of integers a and b where -1000000 <= a, b <= 1000000. The numbers are separated by a space.

Output

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.

Sample Input

-1000000 9

Sample Output

-999,991
 1 import java.util.Scanner;
 2
 3
 4 public class Main {
 5     public static void main(String[] args) {
 6         Scanner input = new Scanner(System.in);
 7         int a = input.nextInt();
 8         int b = input.nextInt();
 9         int sum = a+b;
10         String temp = sum+"";
11         if(sum<0){
12             if(temp.length()<=4){
13                 System.out.println(sum);;
14             }else{
15                 int m = (temp.length()-1)%3;
16                 System.out.print(temp.charAt(0));
17                 if(m==1){
18                     System.out.print(temp.charAt(1));
19                 }
20                 if(m==2){
21                     System.out.print(temp.charAt(1));
22                     System.out.print(temp.charAt(2));
23                 }
24                 for(int i=m+1;i<temp.length();i++){
25                     if((i-m-1)%3==0&&i!=1){
26                         System.out.print(",");
27                     }
28                     System.out.print(temp.charAt(i));
29
30                 }
31             }
32         }else{
33             if(temp.length()<=3){
34                 System.out.println(sum);;
35             }else{
36                 int m = (temp.length())%3;
37                 if(m==1){
38                     System.out.print(temp.charAt(0));
39                 }
40                 if(m==2){
41                     System.out.print(temp.charAt(0));
42                     System.out.print(temp.charAt(1));
43                 }
44                 for(int i=m;i<temp.length();i++){
45                     if((i-m)%3==0&&i!=0){
46                         System.out.print(",");
47                     }
48                     System.out.print(temp.charAt(i));
49                 }
50             }
51         }
52     }
53 }
时间: 2024-12-21 14:05:06

PAT 1001. A+B Format (20)的相关文章

1001. A+B Format (20) ——PAT (Advanced Level) Practise

题目信息: 1001. A+B Format (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than

【PAT】1001. A+B Format (20)

1001. A+B Format (20) Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Each input file contains one test case. Each case cont

PAT 甲级 1001 A+B Format (20)(20 分)

1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Each input file contains one test case. Each case

PAT甲级 1001. A+B Format (20)

题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Each input file contains one test case. Each case contains a pair of i

PAT (Advanced Level) 1001. A+B Format (20)

简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; int a,b,tot,cnt; int u[20]; char ans[100]; int main() { while(~scanf("%d%d",&a,&b)) { a=a+b

PAT甲级——1001 A+B Format (20分)

Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits) Sample Input: -1000000 9 Sample Output: -999,991 第一种方法,注意题目说明的数字范围,及时处理越界即可. 为啥捏,因为 in

PAT:1001. A+B Format (20)(数组存储,逢三加“,”) AC

#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } int arr[30]; int i=0; do { arr[i]=sum%10; sum/=10; ++i; }while(sum!=0); for(int j=i-1 ; j>=0 ; --j) { pr

PAT:1001. A+B Format (20)(取整取余法) 部分正确

#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } if(sum>1000000) //[思维],两个数字在:-1000000 <= a, b <= 1000000,之和不会超过1000000再加三个0的数量级 printf("%d,%d

PAT:1001. A+B Format (20) AC

#include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); int sum=a+b; if(sum<0) { printf("-"); sum=-sum; } if(sum>=1000000) //[思维],两个数字在:-1000000 <= a, b <= 1000000,之和不会超过1000000再加三个0的数量级 printf("%d,%