第1个回答 2008-01-05
建议去看看随机数生成的资料,rand生成的伪随机数用time()做种子可以较好的实现随机数生成.
前两天刚编过一个,代码如下:
#include <stdio.h>
#include <time.h>
#define RIGHT 1
#define WRONG 0
void GenerateKey(int *key)
{
int i,j;
srand(time(NULL));
for (i=0;i<=3;i++)
key[i]=rand()%10;
for (i=1;i<=3;i++)
for(j=0;j<i;j++)
if (key[i]==key[j])
{
key[i]=rand()%10;
j=0;
}
return;
}
int CheckAnswer(int *answer,int *key)
{
int i,j,a=0,b=0;
for (i=0;i<4;i++)
{
for (j=0;j<4;j++)
{
if(answer[i]==key[j] && i==j) a++;
if(answer[i]==key[j] && i!=j) b++;
}
}
printf("%dA%dB",a,b);
if (a==4) return(RIGHT);
else return(WRONG);
}
int main()
{
int i,j,t=0,p=0,answer[]={-1,-1,-1,-1},key[4],status=WRONG;
printf("\n\t\t\tData Guess\n");
printf("\t\t\t\tPress ESC to exit game ,others to play...\n");
while(1)
{
GenerateKey(key);
i=1;
status=WRONG;
printf("\nNew Game\n");
while(i<9&&status==WRONG)
{
printf ("Time %d:\t",i);
for (j=0;j<4;j++)
{
while(t==0)
{
answer[j]=getch()-48;
if (answer[j]==-21) exit(0);
if (answer[j]<0 || answer[j]>9) t=0;
else
{
while(p<j)
{
if (answer[p]==answer[j]) p=j+10;
else p++;
}
if (p==j+10) t=0;
if (p==j) t=1;
}
p=0;
}
printf("%d",answer[j]);
t=0;
}
printf ("\t");
status=CheckAnswer(answer,key);
i++;
printf("\n");
}
if (status==RIGHT)
printf("\n\nCongratulations! You've got the key!\n");
else
printf("\n\nSorry , you lose. The right answer is:%d%d%d%d\n\n",key[0],key[1],key[2],key[3]);
}
return (0);
}
第2个回答 2012-12-05
这里假设当用户输入的都是负数时,仍然显示0:
#include <stdio.h>
int main()
{
int i, n = 3;
double d, max = 0;
for(i = 0; i < 3; i ++){
printf("Enter a number:");
if(scanf("%lf", &d) != 1) return -1 ;
if(d > max) max = d;
}
printf("The largest number entered was %g\n", max);
return 0;
}
第3个回答 2017-11-09
//这是一版适合自己在数组中自己添加数据的,我初学者,要是显得很冗余的话别笑话我哈,呵呵
#include
int main(void)
{
int a[10];
int n;
n=1;
//像10个元素中录入数据
printf("请输入10个数字\n");
for(int s = 0; s < 10; s++)
{
scanf("%d", &a[s]);
}
//原始数组元素输出
for(int i = 0; i <= 9; i++)
{
printf("a[%d] = %d\t", i, a[i]);
}
printf("\n");
printf("请输入n的数值\n");
scanf("%d", &n);
int temp[10];
//这里是将数组的前n个元素取出来,存放在临时数组中
for(int j=0; j < n; j++)
{
temp[j] = a[j];
}
printf("\n");
//从第n+1个元素开始向前位移n个元素
for(int r = 0; r <= 9-n; r++)
{
a[r] = a[r+n];
}
//将末n个元素进行赋值
for(int t = 0; t < n; t++)
{
a[t+r] = temp[t];
}
//改变后数组的输出
for(int k = 0; k <= 9; k++)
{
printf("a[%d] = %d\t", k, a[k]);
}
return 0;
}
第4个回答 2015-11-25
#include<stdio.h>
#include<math.h>
void main()
{
long int num;
int a,b,c,d,e,place;
scanf("%ld",&num);
if(num>=10000)
place=5;
else if(num>=1000)
place=4;
else if(num>=100)
place=3;
else if(num>=10)
place=2;
else
place=1;
printf("%d\n",place);
e=num/10000;
d=(int)(num-e*10000)/1000;
c=(int)(num-e*10000-d*1000)/100;
b=(int)(num-e*10000-d*1000-c*100)/10;
a=(int)(num-e*10000-d*1000-c*100-b*10);
switch(place){
case 5:
printf("%d%d%d%d%d\n",a,b,c,d,e);
break;
case 4:
printf("%d%d%d%d\n",a,b,c,d);
break;
case 3:
printf("%d%d%d\n",a,b,c);
break;
case 2:
printf("%d%d\n",a,b);
break;
case 1:
printf("%d\n",a);
break;
}
}
第5个回答 2012-12-05
#include <Stdio.h>
int main(void)
{
float number;
float max=0;
while(1)
{
printf("Enter a number:\t");
scanf("%f",&number);
if(number>max)
{
max=number;
}
if(number<=0)
{
break;
}
}
printf("The largest number entered was %.2f\n",max);
return 0;
}