程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> void main() { int i,j,k,tem, num,a[4]; int b[4] = {0}, count = 0; srand(time(0)); num = rand() % 10000; //printf("%d ", num); printf("请输入你猜的四个数:"); for (i = 0; i < 4; i++) { scanf("%d",&a[i]); } for (i = 0; i < 4; i++) { tem = num % 10; num = num / 10; for (j = 0; j < 4; j++) { if (a[j] == tem) { for (k = 0; k < count; k++) { if (b[k] == tem)//相同的数字不统计 { break; } } if (k == count) { b[count] = tem; count++; } } } } printf("你输入的数中有%d个数字正确,正确的数是:\n", count); for (i = 0; i < count; i++) { printf("%d ", b[i]); } }