当前位置:编程学习 > C/C++ >>

C语言编程问题

请大家帮我编个程序吧,用C,不是C++,而且要在tuberC里面能通过。
文曲星猜数游戏:
模拟文曲星上的猜数游戏,先由计算机随机生成一个各位相异的4位数字,由用户来猜,根据用户猜测的结果给出提示:xAyB
其中,A前面的数字表示有几位数字不仅数字猜对了,而且位置也正确,B前面的数字表示有几位数字猜对了,但是位置不正确。
最多允许用户猜的次数由用户从键盘输入。如果猜对,则提示“Congratulations!”;如果在规定次数以内仍然猜不对,则给出提示“Sorry, you haven’t guess the right number!”。程序结束之前,在屏幕上显示这个正确的数字。
[提示:用数组a存储计算机随机生成的4位数,用数组b存储用户猜的4位数,对a和b中相同位置的元素进行比较,得到A前面待显示的数字,对a和b的不同位置的元素进行比较,得到B前面待显示的数字。]

不需要太精简,初学者的水品就好,如果好的话我会再加分的
答案:
#include <stdio.h> #include <time.h> #include <string.h> #include <conio.h> void Creat(char* num); int Check(char* s); void Compare(char* num, char* s, int* A, int* B); void menu(void); void main() { int count,A,B,N; char num[5],s[5]; printf("Welcome!\n"); while(1) { Creat(num); printf("How many times will you try:",num); scanf("%d",&N); for (count=N;count>0;count--) { printf("\nYou have %d chances to guess:\n",count); scanf("%s",s); if( Check(s) < 0 ) { printf("Error!\nPlease Input Four Different Integers!\n"); count++; continue; } else { Compare(num,s,&A,&B); if (A==4) { printf("Congratulations!\n"); menu(); break; } else { printf("%dA%dB\n",A,B); if(count<=1) { printf("Sorry,you haven't guess the right number!\n"); printf("Answer Is : %s !\n",num); menu(); break; } } } } } } void Creat(char* num) { srand(time(NULL)); num[0]= rand()%10+'0'; do { num[1]= rand()%10+'0'; } while ( num[1]==num[0] ); do { num[2]= rand()%10+'0'; } while ( num[2]==num[1] || num[2]==num[0] ); do { num[3]= rand()%10+'0'; } while ( num[3]==num[2] || num[3]==num[1] || num[3]==num[0] ); num[4]='\0'; } int Check(char* s) { int i; if (strlen(s)!=4) { return -1; } for ( i=0; i<4; i++) { if( s[i]<'0' || s[i]>'9' ) { return -1; } } if(s[0]==s[1] || s[0]==s[2] || s[0]==s[3] || s[1]==s[2] || s[1]==s[3] || s[2]==s[3] ) { return -1; } return 1; } void Compare(char* num, char* s, int* A, int* B) { int i,j; *A=*B=0; for ( i=0; i<4; i++) { for ( j=0; j<4; j++) { if ( num[i]==s[j]) { if ( i==j ) { (*A)++; } else { (*B)++; } break; } } } } void menu(void) { char c; printf("1.Try Again\n2.Exit\n"); while(c=getch()) { if( c=='1' ) { clrscr(); return; } else if ( c=='2' ) { exit(0); } } } 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int k,i,j,a[4],b[4],count,d,n;

time_t t;
srand((unsigned) time(&t));
b[0]= rand()%9;
for(i=1;i<4;i++)
{
rsm:k= rand()%9;
for(j=0;j<i;j++)
if(k==b[j])
goto rsm;
b[i]=k;
}

printf("\n请输入猜的次数: ");

scanf("%d",&n);
while(n--)
{
count=0;d=0;
for(i=0;i<4;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<4;i++)
{
if(b[i]==a[i])
count++;
}
printf("%dA",count);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(a[i]==b[j])
d++;
}
printf("%dB\n",d-count);
if(count==4)
{
printf("\nCongratulations!\n");

system("pause");
return 0;
}
}
printf("Sorry, you haven't guess the right number.\n");
printf("随机产生的数:");
for(i=0;i<4;i++)
{
printf("%d ",b[i]);
}
system("pause");
}



#include <stdio.h>#include <stdlib.h>#include <time.h>  int main(){  int k,i,j,a[4],b[4],count,d,n;    time_t t;      srand((unsigned) time(&t));      b[0]= rand()%9;      for(i=1;i<4;i++)      {        rsm:k= rand()%9;for(j=0;j<i;j++)if(k==b[j])goto rsm;b[i]=k;  }   printf("\n请输入猜的次数: ");    scanf("%d",&n);  while(n--)  {   count=0;d=0;       for(i=0;i<4;i++)  {scanf("%d",&a[i]);}  for(i=0;i<4;i++) {if(b[i]==a[i])  count++;}  printf("%dA",count);  for(i=0;i<4;i++)  for(j=0;j<4;j++)  {if(a[i]==b[j])d++;  }  printf("%dB\n",d-count);  if(count==4)  {printf("\nCongratulations!\n");system("pause");return 0;   }  }  printf("Sorry, you haven't guess the right number.\n");  printf("随机产生的数:");   for(i=0;i<4;i++)  {   printf("%d ",b[i]);  }  system("pause");}

上一个:c语言模拟简单atm
下一个:C语言如何自学

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,