我要问一个c语言的题目

来源:百度知道 编辑:UC知道 时间:2024/05/27 15:28:16
题目是任意输入两个数,求这两个数的平方根之和
#include<math.h>
main()
{int x,y;
float z;
x=sqrt(rand());
y=sqrt(rand());
z=x+y;
printf("x=%d,y=%d,z=%f",x,y,z);
}请问这样写对不对
为什么运行后结果比实际值大

更正如下:
#include<math.h>
main()
{int x,y; float z;
scanf("%d%d",&x,&y) ;
x=sqrt(x);
y=sqrt(y);
z=x+y;
printf("x=%d,y=%d,z=%f",x,y,z);
getch();
}

#include<math.h>
#include<stdio.h>
main()
{int x,y;
scanf("%d,%d",&x,&y);
float z;
z=(float)(sqrt(x)+sqrt(y));
printf("x=%d,y=%d,z=%f",x,y,z);
}
你那个有问题,我这个运行正确

如果任意输入是指电脑随机,那只要注意一点,x,y出来小数部分已经变为0,应该和你的初衷相反了。
如果不是电脑随机,那还要改很多。
还有就是少了#include<stdio.h>

你审题不对吧,
题目是任意输入两个数,不是让你随机生成两个随机数

应该这么做:
#include<stdio.h>
#include<math.h>
main()
{
float x,y;
float z;
scanf("%f %f",&x,&y);
z=sqrt(x)+sqrt(y);
printf("x=%f,y=%f,z=%f",x,y,z);
}

不对好像应改为
float x,y: