465 - Overflow
这几次写代码都忘记初始化,调了半天,活该啊!
C++语言: Codee#25721
001 /*
002 +++++++++++++++++++++++++++++++++++++++
003 author: chm
004 +++++++++++++++++++++++++++++++++++++++
005 */
006
007 #include <map>
008 #include <set>
009 #include <list>
010 #include <queue>
011 #include <cmath>
012 #include <stack>
013 #include <bitset>
014 #include <cstdio>
015 #include <cctype>
016 #include <string>
017 #include <vector>
018 #include <cassert>
019 #include <cstdlib>
020 #include <cstring>
021 #include <fstream>
022 #include <sstream>
023 #include <iomanip>
024 #include <iostream>
025 #include <algorithm>
026
027 using namespace std;
028
029 FILE* fin = stdin;
030 FILE* fout = stdout;
031 const int max_size = 10086;
032
033 char stra[max_size], strb[max_size];
034 int numa[max_size], numb[max_size];
035 int res[max_size];
036 char resstr[max_size];
037 char inputstr[max_size];
038
039 void convert(char* str, int* num)
040 {
041 int len = strlen(str);
042
043 for(int i = 0, j = len - 1; j >= 0; ++i, --j) //reverse
044 num[i] = str[j] - '0';
045 }
046
047 bool isvalid(char* stra)
048 {
049 int len = strlen(stra);
050 char max[] = "2147483647";
051
052 // num <= 2147 483 647
053 if(len < 10 ||
054 (len == 10 && strcmp(stra, max) <= 0)) // check length,if len==10
055 // ,compare with INT_MAX
056 return false;
057 else
058 return true;
059 }
060
061 bool anscheck(int idx)
062 {
063 while(idx >= 0 && !res[idx])
064 --idx;
065 for(int i = 0, j = idx; j >= 0; ++i, --j) //reverse again
066 resstr[i] = res[j] + '0';
067 resstr[idx + 1] = '\0';
068 return isvalid(resstr);
069 }
070
071 bool plusnum(int len1, int len2)
072 {
073 int len = len1 > len2 ? len1 + 1 : len2 + 1;
074
075 int cry = 0;
076 int tmp;
077 for(int i = 0; i < len; ++i)
078 {
079 tmp = cry + numa[i] + numb[i];
080 cry = tmp / 10;
081 res[i] = tmp % 10;
082 }
083 int idx = len - 1;
084 return anscheck(idx);
085 }
086
087 bool multiply(int len1, int len2)
088 {
089 int tmp;
090
091 for(int i = 0; i < len1; ++i)
092 for(int j = 0; j < len2; ++j)
093 {
094 tmp = numa[i] * numb[j] + res[i + j];
095 res[i + j] = tmp % 10;
096 res[i + j + 1] += tmp / 10;
097 }
098 int idx = len1 + len2 + 2;
099 return anscheck(idx);
100 }
101
102 void discardprezero(char* str) //delete precede zeros like 00000000001
103 {
104 char* ptr = str;
105 char* tmp = str;
106 while(*ptr == '0')
107 ++ptr;
108 if(ptr != str)
109 while(*str++ = *ptr++);
110 if(*tmp == '\0')
111 {
112 tmp[0] = '0';
113 tmp[1] = '\0';
114 }
115 }
116
117 int main()
118 {
119 #ifndef ONLINE_JUDGE
120 freopen("c:\\in.txt", "r", stdin);
121 fout = fopen("c:\\garage\\out.txt", "w");
122 #endif
123
124 char op[2];
125 int len1, len2;
126 bool flarge, slarge, rlarge;
127
128 while(fgets(inputstr, sizeof(inputstr), stdin))
129 {
130
131 fprintf(fout, "%s", inputstr);
132 sscanf(inputstr, "%s %s %s\n", stra, op, strb);
133
134 memset(numa,0,sizeof(stra));
135 memset(numb,0,sizeof(strb));
136 memset(res, 0, sizeof(res));
137
138 discardprezero(stra);
139 len1 = strlen(stra);
140 convert(stra, numa);
141 flarge = isvalid(stra);
142
143 discardprezero(strb);
144 len2 = strlen(strb);
145 convert(strb, numb);
146 slarge = isvalid(strb);
147
148 if(op[0] == '+')
149 rlarge = plusnum(len1, len2);
150 else
151 rlarge = multiply(len1, len2);
152
153 if(flarge)
154 fprintf(fout, "first number too big\n");
155 if(slarge)
156 fprintf(fout, "second number too big\n");
157 if(rlarge)
158 fprintf(fout, "result too big\n");
159 }
160
161 #ifndef ONLINE_JUDGE
162 fclose(fout);
163 system("c:\\garage\\check.exe");
164 system("notepad c:\\garage\\out.txt");
165 #endif
166 return 0;
167 }
002 +++++++++++++++++++++++++++++++++++++++
003 author: chm
004 +++++++++++++++++++++++++++++++++++++++
005 */
006
007 #include <map>
008 #include <set>
009 #include <list>
010 #include <queue>
011 #include <cmath>
012 #include <stack>
013 #include <bitset>
014 #include <cstdio>
015 #include <cctype>
016 #include <string>
017 #include <vector>
018 #include <cassert>
019 #include <cstdlib>
020 #include <cstring>
021 #include <fstream>
022 #include <sstream>
023 #include <iomanip>
024 #include <iostream>
025 #include <algorithm>
026
027 using namespace std;
028
029 FILE* fin = stdin;
030 FILE* fout = stdout;
031 const int max_size = 10086;
032
033 char stra[max_size], strb[max_size];
034 int numa[max_size], numb[max_size];
035 int res[max_size];
036 char resstr[max_size];
037 char inputstr[max_size];
038
039 void convert(char* str, int* num)
040 {
041 int len = strlen(str);
042
043 for(int i = 0, j = len - 1; j >= 0; ++i, --j) //reverse
044 num[i] = str[j] - '0';
045 }
046
047 bool isvalid(char* stra)
048 {
049 int len = strlen(stra);
050 char max[] = "2147483647";
051
052 // num <= 2147 483 647
053 if(len < 10 ||
054 (len == 10 && strcmp(stra, max) <= 0)) // check length,if len==10
055 // ,compare with INT_MAX
056 return false;
057 else
058 return true;
059 }
060
061 bool anscheck(int idx)
062 {
063 while(idx >= 0 && !res[idx])
064 --idx;
065 for(int i = 0, j = idx; j >= 0; ++i, --j) //reverse again
066 resstr[i] = res[j] + '0';
067 resstr[idx + 1] = '\0';
068 return isvalid(resstr);
069 }
070
071 bool plusnum(int len1, int len2)
072 {
073 int len = len1 > len2 ? len1 + 1 : len2 + 1;
074
075 int cry = 0;
076 int tmp;
077 for(int i = 0; i < len; ++i)
078 {
079 tmp = cry + numa[i] + numb[i];
080 cry = tmp / 10;
081 res[i] = tmp % 10;
082 }
083 int idx = len - 1;
084 return anscheck(idx);
085 }
086
087 bool multiply(int len1, int len2)
088 {
089 int tmp;
090
091 for(int i = 0; i < len1; ++i)
092 for(int j = 0; j < len2; ++j)
093 {
094 tmp = numa[i] * numb[j] + res[i + j];
095 res[i + j] = tmp % 10;
096 res[i + j + 1] += tmp / 10;
097 }
098 int idx = len1 + len2 + 2;
099 return anscheck(idx);
100 }
101
102 void discardprezero(char* str) //delete precede zeros like 00000000001
103 {
104 char* ptr = str;
105 char* tmp = str;
106 while(*ptr == '0')
107 ++ptr;
108 if(ptr != str)
109 while(*str++ = *ptr++);
110 if(*tmp == '\0')
111 {
112 tmp[0] = '0';
113 tmp[1] = '\0';
114 }
115 }
116
117 int main()
118 {
119 #ifndef ONLINE_JUDGE
120 freopen("c:\\in.txt", "r", stdin);
121 fout = fopen("c:\\garage\\out.txt", "w");
122 #endif
123
124 char op[2];
125 int len1, len2;
126 bool flarge, slarge, rlarge;
127
128 while(fgets(inputstr, sizeof(inputstr), stdin))
129 {
130
131 fprintf(fout, "%s", inputstr);
132 sscanf(inputstr, "%s %s %s\n", stra, op, strb);
133
134 memset(numa,0,sizeof(stra));
135 memset(numb,0,sizeof(strb));
136 memset(res, 0, sizeof(res));
137
138 discardprezero(stra);
139 len1 = strlen(stra);
140 convert(stra, numa);
141 flarge = isvalid(stra);
142
143 discardprezero(strb);
144 len2 = strlen(strb);
145 convert(strb, numb);
146 slarge = isvalid(strb);
147
148 if(op[0] == '+')
149 rlarge = plusnum(len1, len2);
150 else
151 rlarge = multiply(len1, len2);
152
153 if(flarge)
154 fprintf(fout, "first number too big\n");
155 if(slarge)
156 fprintf(fout, "second number too big\n");
157 if(rlarge)
158 fprintf(fout, "result too big\n");
159 }
160
161 #ifndef ONLINE_JUDGE
162 fclose(fout);
163 system("c:\\garage\\check.exe");
164 system("notepad c:\\garage\\out.txt");
165 #endif
166 return 0;
167 }
浙公网安备 33010602011771号