随机函数rand()
magic=rand();
产生[0,RAND_MAX]间的随机数
RAND_MAX在stdlib中定义,不大于双字节整数的最大值32767
使用rand()时,要添加头文件#include <stdlib.h>
srand(seed)(seed为参数)为rand()设置随机种子,使产生的随机数“随机化”
或改用系统时间作为随机种子更好
用time()获得系统时间
两种方法——通过函数参数或函数返回值来获得系统时间
用NULL作为函数参数,使其仅能从返回值取得系统时间,便于将函数写到表达式中 srand(time(NULL))
scanf()按指定格式读取缓冲区中的数据,若读取失败,则缓冲区中的非数字字符不会被读走,因不等而一直处于判断、读取、判断、读取、…(死机)
scanf()返回值为正确读入的数据项数
while(getchar()!=’\n’)
{
;//空语句
}
while(getchar()!=’\n’)清除输入缓冲区中的残留数据,然后提示重新输入
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num = 1;
int a = 1;
int b = 0;
int c = 0;
for(num = 1; num <= 100; num++)
{
if(num == 1)
{
printf("%d", num);
c++;
}
else
{
for(a = 1; a <= num; a++)
{
if(num % a == 0)
{
b++;
}
}
}
if(b == 2)
{
printf("%d\t", num);
c++;
}
if(c % 5 == 0)
{
printf("\n");
}
b = 0;
}
return 0;
}