C语言小小编程题,结果疑似数据发生溢出,帮忙改正

最菜的鸟 2009-12-19 04:55:15

/*有一个数,它加上100后是一个完全平方数,
再加上168后又是一个完全平方数,请问该数是多少?*/
#include <stdio.h>
void main()
{
int x=1,i,j,z,y,tag=1;
while(x>=1)
{
z=100+x;
y=168+z;
for(i=1,j=1;i<=z&&j<=y;i++,j++)
{
if(i*i==z&&j*j==y)
{
tag=0;
break;
}
}
if(tag==0)
{
break;
}
x++;
}
printf("这个数为:%d",x);
}

大家好,又要麻烦大家了,先前我按题目要求变了一个小程序,但是疑似发生了数据溢出,结果为-32768,这是为什么呢?帮忙指正一下吧!
...全文
235 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
bladesoft 2009-12-28
  • 打赏
  • 举报
回复
楼主i和j一起++肯定不对,顺便顶下2楼的算法。
最菜的鸟 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xlemuel 的回复:]
首先你的算法有问题。for循环里i和j是相等的。
[/Quote]

一语惊醒梦中人,看到这句话我就知道问题出在什么地方了!
dongdong814617937 2009-12-20
  • 打赏
  • 举报
回复
算法不对,你可以将两个数分开来求,那样理解起来简单也不容易出错。刚开始编程最好不要玩高技术
oxgen350 2009-12-20
  • 打赏
  • 举报
回复
顶下 呵呵
yunfeng7854 2009-12-20
  • 打赏
  • 举报
回复

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float f;
int temp;
int i;
int j;
for(j = 1;j < 1600;j++)
{
temp = j + 100;
f = sqrt(temp);
i = sqrt(temp);
if(f != i) continue;
temp += 68;
f = sqrt(temp);
i = sqrt(temp);
if(f == i) printf("%d\n",j);
}
return 0;
}

输出156
longfeihufengyun 2009-12-20
  • 打赏
  • 举报
回复

[Quote=引用 2 楼 sduxiaoxiang 的回复:]
C/C++ code
#include<stdio.h>void main()
{int x=1,i,j,z,y,tag=1;while(x>=1)
{
z=100+x;
y=168+z;if((int)sqrt(z)*(int)sqrt(z)==z&&(int)sqrt(y)*(int)sqrt(y)==y)
{
?-
[/Quote]
xuell0406 2009-12-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 zuoluoq 的回复:]
C/C++ code/*有一个数,它加上100后是一个完全平方数,
再加上168后又是一个完全平方数,请问该数是多少?*/
#include<stdio.h>void main()
{int x=1,i,j,z,y,tag=1;while(x>=1)
{
z=100+x;
y=168+z;for(i=1,j=1;i<=z&&j<=y;i++,j++)
{if(i*i==z&&j*j==y)
{
tag=0;break;
}
}if(tag==0)
{break;
}
x++;
}
printf("这个数为:%d",x);
}
大家好,又要麻烦大家了,先前我按题目要求变了一个小程序,但是疑似发生了数据溢出,结果为-32768,这是为什么呢?帮忙指正一下吧!
[/Quote]

你的程序问题出在 你的默认条件里隐含的是i,j相等的时候才跳出
i*i==z&&j*j==y

只需在循环里再套个,判断j*j即可
代码如下



#include <stdio.h>
void main()
{
int x=1,i,j,z,y,tag=1;
while(x>=1)
{
z=100+x;
y=168+z;
for(i=1;i<=z;i++)
{
if(i*i==z)
{
for (j=1; j<=y; j++)
{
if (j*j==y)
{
tag=0;
break;
}
}
}
}
if(tag==0)
{
break;
}
x++;
}
printf("这个数为:%d",x);
}



xuell0406 2009-12-19
  • 打赏
  • 举报
回复

#include <math.h>
#include <iostream.h>

void main(void)
{
for (int i=0; i<1000; i++)
{
if ( ( (int((sqrt((double)i + 100.0)) * 10) % 10) == 0) &&
( (int((sqrt((double)i + 100.0 + 168.0)) * 10) % 10) == 0)
)
{
cout<<i<<endl;
}
}
}
东大坡居士 2009-12-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sduxiaoxiang 的回复:]
C/C++ code
#include<stdio.h>void main()
{int x=1,i,j,z,y,tag=1;while(x>=1)
{
z=100+x;
y=168+z;if((int)sqrt(z)*(int)sqrt(z)==z&&(int)sqrt(y)*(int)sqrt(y)==y)
{
tag=0;
}if(tag==0)
{break;
}
x++;
}
printf("这个数为:%d",x);
}
[/Quote]

顶个~~~
z569362161 2009-12-19
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
int x=1,i,j,z,y,tag=1;
while(x>=1)
{
z=100+x;
y=168+z;
if((int)sqrt(z)*(int)sqrt(z)==z&&(int)sqrt(y)*(int)sqrt(y)==y)
{
tag=0;
}
if(tag==0)
{
break;
}
x++;
}
printf("这个数为:%d",x);

return 0;
}
这个数是21。楼上的写法对
ghosty_hand 2009-12-19
  • 打赏
  • 举报
回复
你的算法不对
不过很奇怪这个题自己算就行了 干嘛非得写个程序呢?自己算很简单啊
168 = a^2 - b^2;
b > 10;
再168分解因子就可以了
sduxiaoxiang 2009-12-19
  • 打赏
  • 举报
回复
#include <math>
sduxiaoxiang 2009-12-19
  • 打赏
  • 举报
回复

#include <stdio.h>
void main()
{
int x=1,i,j,z,y,tag=1;
while(x>=1)
{
z=100+x;
y=168+z;
if((int)sqrt(z)*(int)sqrt(z)==z&&(int)sqrt(y)*(int)sqrt(y)==y)
{
tag=0;
}
if(tag==0)
{
break;
}
x++;
}
printf("这个数为:%d",x);
}

xlemuel 2009-12-19
  • 打赏
  • 举报
回复
首先你的算法有问题。for循环里i和j是相等的。

69,371

社区成员

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

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