这里用到了随机数生成器以及ctime
1 #include <cstdio> 2 #include <cstdlib> 3 #include <ctime> 4 #include <windows.h> 5 using namespace std; 6 int main() 7 { 8 int mo; 9 printf("Please write the upper limit:"); 10 scanf("%d",&mo); 11 srand((unsigned)(time(NULL))); 12 printf("Guess!\n"); 13 while(1) 14 { 15 int n=(rand()%mo)+1; 16 int m,k=0; 17 while(scanf("%d",&m)) 18 { 19 k++; 20 if(m==n) 21 { 22 printf("Right!You have guessed %d times,too young,too simple,sometimes naive!\n",k); 23 break; 24 } 25 if(m>n) printf("Too large!\n"); 26 else printf("Too small!\n"); 27 } 28 printf("\nAgain!\n"); 29 } 30 return 0; 31 }