level 1
Potato豆BB
楼主
一直数据文件INT.DAT中有300个四位数,并以调用.........(都是废话省略了),要求编写函数spellVal(),功能为求出千位上的数加上个位上的数等于百位上的数加上十位上的数的个数COUNT,再把满足条件的四位数依次存入数组b中,并对b从大到小排列。
标准答案为:
{ int i, thou, hun, ten, data, j;
for (i=0; i<300; i++)
{ thou = a[i]/1000;
hun = a[i]%1000/100;
ten = a[i]%100/10;
data = a[i]%10;
if (thou+data == hun+ten)
{ b[count] = a[i];
count++;
}
}
for (i=0; i<count-1; i++)
for (j=i+1; j<count; j++)
if (b[i] > b[j])
{ data = b[i];
b[i] = b[j];
b[j] = data;
}
}
我把程序改了一下 ,编译过后C++显示没有错误,也能运行,交卷后被机试模拟系统判为0分 为什么啊? 是模拟系统的问题 还是我的写法不对 改动如下:
{ int i, j;
int a1,a2,a3,a4;
int temp;
for (i=0; i<300; i++)
{ a1 = a[i]/1000;
a2 = a[i]%1000/100;
a3 = a[i]%100/10;
a4 = a[i]%10;
if (a1+a4 == a2+a3)
{ b[count] = a[i];
count++;
}
}
for (i=0; i<count-1; i++)
for (j=i+1; j<count; j++)
if (b[i] > b[j])
{ temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
2010年03月18日 11点03分
1
标准答案为:
{ int i, thou, hun, ten, data, j;
for (i=0; i<300; i++)
{ thou = a[i]/1000;
hun = a[i]%1000/100;
ten = a[i]%100/10;
data = a[i]%10;
if (thou+data == hun+ten)
{ b[count] = a[i];
count++;
}
}
for (i=0; i<count-1; i++)
for (j=i+1; j<count; j++)
if (b[i] > b[j])
{ data = b[i];
b[i] = b[j];
b[j] = data;
}
}
我把程序改了一下 ,编译过后C++显示没有错误,也能运行,交卷后被机试模拟系统判为0分 为什么啊? 是模拟系统的问题 还是我的写法不对 改动如下:
{ int i, j;
int a1,a2,a3,a4;
int temp;
for (i=0; i<300; i++)
{ a1 = a[i]/1000;
a2 = a[i]%1000/100;
a3 = a[i]%100/10;
a4 = a[i]%10;
if (a1+a4 == a2+a3)
{ b[count] = a[i];
count++;
}
}
for (i=0; i<count-1; i++)
for (j=i+1; j<count; j++)
if (b[i] > b[j])
{ temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}