一道有趣的难题...
题目:请编写一个函数float fun(double h),函数的功能使对变量h中的值保留2位小树,并对第三位进行四舍五入(规定h中的值为正数)。小弟的算法:float fun(double h)
{ int x;float s;
h=h*1000;
x=(int)h;
if(x%10<5) x=x-x%10;
else x=x+(10-x%10);
s=(float)x;
s/=1000;
return s;
}
答案算法:float fun (float h)
{long t;
float s;
h=h*1000;
t=(h+5)/10;
s=(float)t/100.0;
return s;}
事实证明答案算法和我自己的算法都算不出正确答案,可能是数据溢出造成的,请各位高手帮忙!小弟先谢过了~
回复 2# 大丈夫死了 的帖子
字符串?怎么用字符串,字符的范围更小.能不能说详系点回复 3# neverTheSame 的帖子
那应该怎么做?[tk10] [code]/******************************************************************为了使可以转换的数字位数更多,所以使用字符数组来作。
******************************************************************/
#include<stdio.h>
#include<string.h>
#define X {char c;while(c=getchar()!='\n');}
#define N 1000
int ch(char a[],int n)
{
char *p;
int i,b;
b=strlen(a); /* 测出字符串长度 */
for(i=0;i<b;++i)
if(a[i]=='.')
break; /* 找出小数点的位置 */
if(b-i<n) /* 当小数点后的位数小于要保留的位数时 */
{
printf("对不起,您输入的数字与所要保留的位数不符,请重新输入.\n");
sleep(1); /* ~~暂停屏幕1秒 */
return 0; /* 返回 0 ,要求重新输入 */
}
p=a+i+n+1; /* 指针指向要保留的字符位数后一位 */
if(*p>'4')
{
*(p-1)+=1;
*p='\0'; /* 如果保留字符后的数字大于4,保留字符加1,并在后*/
} /* 面加上结束标记 */
else *p='\0'; /* 如果保留字符后的数字小于4,直接将它重新赋为结束标记*/
return 1; /* 如果四舍五入成功,返回 1 */
}
int main(void)
{
char a[N];
int n,b;
do
{
system("cls"); /* 当输入出错后返回时,清屏 */
printf("请输入想要保留的小数后位数:\n");
scanf("%d",&n);
X; /* 清除键盘缓存 */
printf("\n请输入您的数字:\n");
gets(a); /* 输入字符串*/
b=ch(a,n); /* 调用函数四舍五入 */
}
while(!b);
printf("\n四舍五入后的数字为:\n");
puts(a);
getch();
}
[/code] 写这么长,还不看题目要求,真是瞎扯蛋
[quote]
题目:请编写一个函数float fun(double h),函数的功能使对变量h中的值保留2位小树,并对第三位进行四舍五入(规定h中的值为正数)。
[/quote]
[quote]
[color=#2e8b57]float[/color] [color=#000000]fun[/color]([color=#2e8b57]float[/color] [color=#000000]f[/color])
{
[color=#2e8b57]static[/color] [color=#2e8b57]char[/color] [color=#000000]buf[/color][[color=#ff00ff]50[/color]];
[color=#000000]sprintf[/color]([color=#000000]buf[/color],[color=#ff00ff]"%.2f"[/color],[color=#000000]f[/color]);
[color=#000000]sscanf[/color]([color=#000000]buf[/color],[color=#ff00ff]"%f"[/color],&[color=#000000]f[/color]);
[color=#2e8b57]return[/color] [color=#000000]f[/color];
}
[/quote] 根据楼主提供题目的答案算法:float fun (float h)
楼主你的float fun(double h)中输入double返回float肯定写错了,俺没有完全照你的写。
回复 8# VxWorks 的帖子
恩,谢谢大侠了.此论坛真是藏龙卧虎啊回复 6# 广陵绝唱 的帖子
虽然没试你的程序,但我想应该是对的.写了这么长,还有说明!这么负责,素质真是[tk18] 高!!
回复 8# VxWorks 的帖子
不好意思试了一下你的程序好象不好使,位数比较小时可以.比如17.654但是大了就不行了比如17.88具体结果是这样的:输入17.88输出17.89997.还有一个问题这两个sprintf, sscanf是什么函数[tk29] ?
回复 6# 广陵绝唱 的帖子
试了一下你的程序,编译时有三个错:1.error C2065: 'sleep' : undeclared identifier
2.error C2065: 'system' : undeclared identifier
3.error C2065: 'getch' : undeclared identifier 回11L:显示17.89997是因为float的精度过低。可以使用double。
Vx的程序不错……我的实现参见下面代码:
#include <stdio.h>
double fun1(double f)
{
static char buf[50];
sprintf(buf,"%.2f",f);
sscanf(buf,"%lf",&f);
return f;
}
double fun2(double f)
{
return (float)(int)(f*100+0.5)/100;
}
int main(void)
{
printf("%f,%f",fun1(17.88),fun2(17.88));
return 0;
}
[[it] 本帖最后由 StarWing83 于 2008-7-17 20:34 编辑 [/it]]
回复 13# StarWing83 的帖子
谢谢了,大侠的两个算法都不错[[it] 本帖最后由 门外汉2008 于 2008-7-18 07:43 编辑 [/it]] To: 7#,8#
[em09] [em09] [em09] [em09] 是啊,可怜的Vx……
话说,如果真的这么写代码,会被老板骂死的…… [quote][bo][un]Knocker[/un] 在 2008-7-18 10:34 的发言:[/bo]
To: 7#,8#
[em09] [em09] [em09] [em09] [/quote]
tnn的,竟然有人敢笑俺[em05]
哪里好笑?说出来让我也乐呵乐呵[em12] 回Vx,昨天看了功夫熊猫,的确搞笑。
页:
[1]
