J 计算A+B 大数

https://ac.nowcoder.com/acm/contest/4462/J

 

 有前导0是可以的

 

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     public static  void solve(String m){
 6         char p[]=m.toCharArray();
 7         for(int i=0;i<m.length();i++){
 8             if((p[i]<'0'||p[i]>'9')&&p[i]!='+') {
 9                 System.out.println("skipped");
10                 return;
11             }
12         }
13         if(p[0]=='+'||p[m.length()-1]=='+'){
14             System.out.println("skipped");
15             return;
16         }
17         int index=0;
18         int cnt=0;
19         for(int i=1;i<m.length()-1;i++){
20             if(p[i]=='+'){
21                 cnt++;
22                 index=i;
23             }
24         }
25         if(cnt==0||cnt>1){
26             System.out.println("skipped");
27             return;
28         }
29         BigInteger a= new BigInteger(m.substring(0,index));
30         BigInteger b= new BigInteger(m.substring(index+1));
31         a=a.add(b);
32         String c=a+"";
33         System.out.println(c);
34  
35     }
36     public static void main(String[] args) {
37         Scanner input = new Scanner(System.in);
38         int n = input.nextInt();
39         for(int i=1;i<=n;i++){
40             String m = input.next();
41             solve(m);
42         }
43 
44     }
45 }

 

posted @ 2021-02-25 10:29  古比  阅读(375)  评论(0编辑  收藏  举报