武汉大学OJ的一道ACM题目,总是wrong answer。很想知道原因。谢谢!
http://acm.whu.edu.cn/learn/problem/detail?problem_id=1162不要你贴一个源码上来哦!我只是很想知道为什么下面的代码...
http://acm.whu.edu.cn/learn/problem/detail?problem_id=1162
不要你贴一个源码上来哦!我只是很想知道为什么下面的代码会WA。题目的例子能正确计算。非常感谢!
源代码:
#include<stdio.h>
#include<string.h>
// WOJ ACM 1162
#define MAX 1010
unsigned char n1[ MAX], n2[ MAX], res[ MAX]; int len1, len2;
unsigned char * bstr, * sstr, blen, slen;// big and small
void add_and_show(){
int i, j;
for( i = 0; i < blen - slen; ++ i){
res[i] = bstr[i];
}
for( i = blen - slen, j = 0; i < blen; ++ i, ++ j){
res[i] = sstr[j] + bstr[i] - '0';
}
for( i = blen - 1; i > 0; -- i){
if( res[i] - '0' > 9){
res[i] -= 10;
res[i - 1] += 1;
}
}
if( res[0] > '0' + 9){
res[0] -= 10;
printf( "1");
}
res[blen] = '\0';
printf( "%s\n", res);
}
int main(){
int n;
int i;
scanf( "%d", & n);
for( i = 0; i < n; ++ i){
scanf( "%s", n1); len1 = strlen( n1);
scanf( "%s", n2); len2 = strlen( n2);
if( len1 > len2){
blen = len1; slen = len2;
bstr = n1; sstr = n2;
}else{
blen = len2; slen = len1;
bstr = n2; sstr = n1;
}
add_and_show();
}
return 0;
} 展开
不要你贴一个源码上来哦!我只是很想知道为什么下面的代码会WA。题目的例子能正确计算。非常感谢!
源代码:
#include<stdio.h>
#include<string.h>
// WOJ ACM 1162
#define MAX 1010
unsigned char n1[ MAX], n2[ MAX], res[ MAX]; int len1, len2;
unsigned char * bstr, * sstr, blen, slen;// big and small
void add_and_show(){
int i, j;
for( i = 0; i < blen - slen; ++ i){
res[i] = bstr[i];
}
for( i = blen - slen, j = 0; i < blen; ++ i, ++ j){
res[i] = sstr[j] + bstr[i] - '0';
}
for( i = blen - 1; i > 0; -- i){
if( res[i] - '0' > 9){
res[i] -= 10;
res[i - 1] += 1;
}
}
if( res[0] > '0' + 9){
res[0] -= 10;
printf( "1");
}
res[blen] = '\0';
printf( "%s\n", res);
}
int main(){
int n;
int i;
scanf( "%d", & n);
for( i = 0; i < n; ++ i){
scanf( "%s", n1); len1 = strlen( n1);
scanf( "%s", n2); len2 = strlen( n2);
if( len1 > len2){
blen = len1; slen = len2;
bstr = n1; sstr = n2;
}else{
blen = len2; slen = len1;
bstr = n2; sstr = n1;
}
add_and_show();
}
return 0;
} 展开
展开全部
//就改了下面两个注释的地方,其他没改
#include<stdio.h>
#include<string.h>
// WOJ ACM 1162
#define MAX 1010
char n1[ MAX], n2[ MAX], res[ MAX]; int len1, len2; //不需要unsigned吧,普通的char就好了吧
char * bstr, * sstr;
int blen, slen;// *******************改成int,因为char的范围才128,你用char类型存储大数的长度果断不够用啦
void add_and_show(){
int i, j;
for( i = 0; i < blen - slen; ++ i){
res[i] = bstr[i];
}
for( i = blen - slen, j = 0; i < blen; ++ i, ++ j){
res[i] = sstr[j] + bstr[i] - '0';
}
for( i = blen - 1; i > 0; -- i){
if( res[i] - '0' > 9){
res[i] -= 10;
res[i - 1] += 1;
}
}
if( res[0] > '0' + 9){
res[0] -= 10;
printf( "1");
}
res[blen] = '\0';
printf( "%s\n", res);
}
int main(){
int n;
int i;
scanf( "%d", & n);
for( i = 0; i < n; ++ i){
scanf( "%s", n1); len1 = strlen( n1);
scanf( "%s", n2); len2 = strlen( n2);
if( len1 > len2){
blen = len1; slen = len2;
bstr = n1; sstr = n2;
}else{
blen = len2; slen = len1;
bstr = n2; sstr = n1;
}
add_and_show();
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询