注意:细心决定成败
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=406
题目大意:检测进行运算的数和他们的结果
题目考点:大数 模拟
解题思路:检测,,前导0 的忽略;
解题代码:

1 // File Name: uva465.c 2 // Author: darkdream 3 // Created Time: 2013年01月25日 星期五 20时41分37秒 4 5 #include<stdio.h> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<time.h> 9 #include<math.h> 10 char stand[] = "2147483647"; 11 char a[1000],b[1000]; 12 void mu(int c[],int d[],int e[]) 13 { 14 int i ,j, k ,t ; 15 for (i =0 ;i < strlen(a) ;i ++) 16 { 17 int s = 0 ; 18 for (j = 0 ; j < strlen(b) ;j++) 19 { 20 t = c[i]*d[j] +s +e[i+j] ; 21 s = t / 10 ; 22 e[i+j] = t % 10 ; 23 } 24 for (j = i+j ; j< 550 ; j++) 25 { 26 t = e[j] + s; 27 s = t /10 ; 28 e[j] = t %10 ; 29 } 30 31 } 32 33 34 } 35 void add(int a[] , int b[]) 36 { 37 int i ; 38 int s = 0 ; 39 for (i = 0 ; i < 500 ;i ++) 40 { 41 int t = a[i] + b[i] +s; 42 s = t / 10 ; 43 44 a[i] = t %10 ; 45 46 } 47 48 } 49 int strle(char a[]) 50 { 51 int i ; 52 for (i = 0 ;i < strlen(a) ;i ++) 53 if (a[i] != '0') 54 break; 55 return strlen(a) - i ; 56 57 } 58 int than(char a[]) 59 { 60 if (strle(a) > strlen(stand)) 61 return 1 ; 62 if (strle(a) == strlen(stand)) 63 if( strcmp(a,stand) >0) 64 return 1; 65 else 66 return 0; 67 if (strle(a) < strlen(stand)) 68 return 0; 69 } 70 void change(char a[], int b[]) 71 { 72 73 int i ; 74 for (i = 0 ; i <strlen(a); i++) 75 b[strlen(a) - i -1] = a[i] - '0'; 76 } 77 void rechange(int a[], char b[]) 78 { 79 int i ; 80 for (i = 500 ; i >= 0 ; i-- ) 81 if (a[i] != 0) 82 break; 83 int j ; 84 if (i == -1) 85 b[0] = '0'; 86 else 87 for (j = 0 ; j <= i ; j++) 88 b[j] = a[i-j] + '0'; 89 90 91 } 92 93 int main(){ 94 int i ; 95 int c[1000],d[1000]; 96 int e[1000]; 97 while(scanf("%s",a) != EOF) 98 { 99 memset(c,0,sizeof(c)); 100 memset(d,0,sizeof(d)); 101 memset(e,0,sizeof(e)); 102 char temp[10]; 103 change(a,c); 104 scanf("%s",temp); 105 scanf("%s",b); 106 printf("%s %s %s\n",a,temp,b); 107 108 if (than(a)) 109 printf("first number too big\n"); 110 if (than(b)) 111 printf("second number too big\n"); 112 change(b,d); 113 if(strchr(temp,'+')) 114 { 115 add (c,d); 116 memset(a,0,sizeof(a)); 117 rechange(c,a); 118 if(than(a)) 119 printf("result too big\n"); 120 121 122 } 123 124 if(strchr(temp,'*')) 125 { 126 mu(c,d,e); 127 memset(a,0,sizeof(a)); 128 rechange(e,a); 129 if (than(a)) 130 printf("result too big\n"); 131 } 132 } 133 return 0 ; 134 }