我想做个测试中奖概率的程序,但就是遇到不知道怎样确定小数点后面小数位数

zhangbencheng 2012-05-21 12:50:09
这是我的程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>

void main()
{
int r,i;
int a;
double m=0;
i=0;
printf("请输入你要猜测的数0——100:");
scanf("%d",&a);
while(!kbhit())
{
r=(rand()/1000);
printf(" 随机数:");
printf("%d\b\b\b\n",r);
i++;
system("cls");
if(r==a)break;
}
printf(" 随机数:");
printf("%d\n",r);
m=1/i;
printf(" 你的中奖概率为%f------在运行了%d次\n",m,i);
system("PAUSE");
}

中奖概率是0.00000,我想是超过double型的范围



...全文
164 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-05-21
  • 打赏
  • 举报
回复
常量也有类型!
C++ Floating-Point Constants
Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.

Syntax

floating-constant :

fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt

fractional-constant :

digit-sequenceopt . digit-sequence
digit-sequence .

exponent-part :

e signopt digit-sequence
E signopt digit-sequence

sign : one of

+ –

digit-sequence :

digit
digit-sequence digit

floating-suffix :one of

f l F L

Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example:

18.46
38.

The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example:

18.46e0 // 18.46
18.46e1 // 184.6

If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0.

Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively.

Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like

void func( double );

and

void func( long double );

赵4老师 2012-05-21
  • 打赏
  • 举报
回复
"%.6f",float
"%.15lf",double
W170532934 2012-05-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

m=1/i;你把1改成1.0,这样结果就不会是0了,1/i整型计算的结果为0.
[/Quote]或者改用double进行计算
糊涂凃小白 2012-05-21
  • 打赏
  • 举报
回复
m=1/i;你把1改成1.0,这样结果就不会是0了,1/i整型计算的结果为0.

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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