标签:des style class blog c code
Time
Limit: 2000/1000 MS (Java/Others) Memory Limit:
65536/32768 K (Java/Others)
Total Submission(s):
201400 Accepted Submission(s):
38599
1 import java.math.BigDecimal; 2 import java.util.*; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n = cin.nextInt(); 8 for (int i = 1; i <= n; ++i) { 9 10 BigDecimal a1, b1, c1; 11 a1 = cin.nextBigDecimal(); 12 b1 = cin.nextBigDecimal(); 13 c1 = a1.add(b1); 14 System.out.println("Case " + i + ":"); 15 System.out.println(a1 + " + " + b1 + " = " + c1); 16 if (i < n) 17 System.out.println(); 18 } 19 } 20 }
C代码:
1 #include <cstdio> 2 #include <iostream> 3 #include <string.h> 4 using namespace std; 5 6 #define N 1000 7 int main() 8 { 9 int T, f; 10 int m, n, temp; 11 char A[N], B[N]; 12 int sum[N+1], t[N]; 13 14 cin>>T; 15 f=T; 16 17 while(T--) 18 { 19 scanf("%s",A); 20 scanf("%s",B); 21 22 m = strlen(A); 23 n = strlen(B); 24 25 if(m==n) { 26 27 for(int i = m-1; i>=0; i--) t[i]=A[i]+B[i]-‘0‘-‘0‘; 28 29 for(int i = m-1; i>=0; i--) { 30 31 temp = t[i]/10; 32 sum[i+1] = t[i]%10; 33 if(temp && i) t[i-1] += temp; 34 if(i==0) sum[0] = temp; 35 } 36 37 printf("Case %d:\n",f-T); 38 printf("%s + %s = ",A,B); 39 40 if(sum[0]) printf("%d", sum[0]); 41 42 for(int i = 1; i < m; i++) printf("%d", sum[i]); 43 44 printf("%d\n",sum[m]); 45 } 46 else if(m>n) { 47 48 for(int i = m-1; i>=0; i--) t[i]=A[i]-‘0‘; 49 for(int i = m-1; n>=m-i; i--) t[i]+=B[n-m+i]-‘0‘; 50 51 for(int i = m-1; i>=0; i--) { 52 53 temp = t[i]/10; 54 sum[i+1] = t[i]%10; 55 if(temp && i) t[i-1] += temp; 56 if(i==0) sum[0] = temp; 57 } 58 59 printf("Case %d:\n",f-T); 60 printf("%s + %s = ",A,B); 61 62 if(sum[0]) printf("%d", sum[0]); 63 64 for(int i = 1; i < m; i++) printf("%d", sum[i]); 65 66 printf("%d\n",sum[m]); 67 68 } 69 else { 70 71 for(int i = n-1; i>=0; i--) t[i]=B[i]-‘0‘; 72 for(int i = n-1; m>=n-i; i--) t[i]+=A[m-n+i]-‘0‘; 73 74 for(int i = n-1; i>=0; i--) { 75 76 temp = t[i]/10; 77 sum[i+1] = t[i]%10; 78 if(temp && i) t[i-1] += temp; 79 if(i==0) sum[0] = temp; 80 } 81 82 printf("Case %d:\n",f-T); 83 printf("%s + %s = ",A,B); 84 85 if(sum[0]) printf("%d", sum[0]); 86 87 for(int i = 1; i < n; i++) printf("%d", sum[i]); 88 89 printf("%d\n",sum[n]); 90 91 } 92 if(T) printf("\n"); 93 } 94 return 0; 95 }
标签:des style class blog c code
原文地址:http://www.cnblogs.com/sayeter/p/3735732.html