刷小白书时的实验题,用rand函数来实现随机,但并不是0-255的全部ASCII码。顺便用了clock()函数来计时。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAXN 100000+10
char s[MAXN];
int main(void)
{
srand((unsigned)time(NULL));
int t,tot=0,i=0;
for(i=0;i<MAXN;i++)
{
t=rand()%90+33; //用rand函数随机生成33-122之间的整数
s[i]=(char)t; //将ascii整数转化为字符
}
//printf("%s\n",s);
for(i=0;i<strlen(s);i++)
if(s[i]=='1') tot++;
printf("There are %d character(s) '1' in the string.\n",tot);
printf("There are %lu chars in string s.\n",strlen(s));
printf("Time used=%.8lf\n",(double)clock()/CLOCKS_PER_SEC);
return 0;
}