请问这个程序错哪儿了?

huaxiamoyun 2010-08-29 11:34:42
题目:
在三位整数(100-999)中找出符合下面条件的整数,并依次从小到大存入数组b中。它是完全平方数,而且有两位数字相同。如144

下面是程序,我检查不出来错那儿了
请高手指教
#include<stdio.h>
void WriteDat();
int jsValue(int bb[])
{
int i,j;
int cnt=0;
int a3,a2,a1;
for(i=100;i<=999;i++)
{ j=sqrt(i);
if(i==j*j)
{
a3=i/100;
a2=i%100/10;
a1=i%10;
if(a1==a2||a1==a3||a2==a3)
{
bb[cnt]=i;
cnt++;
}
}
}
return cnt;
}
void main()
{
int b[20],num;
num=jsValue(b);
WriteDat(num,b);
}
void WriteDat(int num,int b[])
{
FILE *out;
int i;
out=fopen("OUT.DAT","w");
fprintf(out,"%d\n",num);
for(i=0;i<num;i++)
fprintf(out,"%d\n",b[i]);
fclose(out);
}
...全文
82 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
renzhewh 2010-08-29
  • 打赏
  • 举报
回复

lz的代码中检测100-999中每个数,效率不高,其实可以只考虑完全数,
这样只检测完全数中是否两位相同即可,而且sqrt的使用也会使效率下降

#include <stdio.h>

int main()
{
int d = 10; // 10 * 10 = 100
int square = 100; // 保存d对应的完全数

while (d < 32) // 32 * 32 >= 1000
{
if (存在两位相同)
printf("%d\n", square);
// (d + 1)^2 = d^2 + 2 * d + 1
square += 2 * d++ + 1;
}

return 0;
}
wanmonster 2010-08-29
  • 打赏
  • 举报
回复
sqrt()应该包含在math.h头文件中,
WriteDat()定义出了问题。
suguang1573 2010-08-29
  • 打赏
  • 举报
回复
没有错~~鉴定完毕!!

只是少了#include <math.h>

请LZ用GCC编译器编译。
  • 打赏
  • 举报
回复
缺少头文件:

#include <math.h>

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧