返回列表 發帖

[隨堂測驗] 超級金頭腦 (一)

設計遊戲 "超級金頭腦",
讓使用者計算兩個範圍介於51~99之隨機亂數的和.





本帖隱藏的內容需要回復才可以瀏覽

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     int n1, n2, ans, ansc;
  8.     float start, t1, t2;
  9.     srand(time(NULL));
  10.     n1=rand()%49+51;
  11.     n2=rand()%49+51;
  12.     ansc=n1+n2;
  13.     start=clock();
  14.     cout<<n1<<" + "<<n2<<" = ";
  15.     t1=clock()-start;
  16.     cin>>ans;
  17.     if(ans==ansc)
  18.         cout<<"答對了! 本題花費"<<clock()-t1<<"毫秒思考!"<<endl;
  19.     else
  20.         cout<<"答錯了! 本題花費"<<clock()-t1<<"毫秒思考!"<<endl;
  21.     system("pause");
  22.     return 0;
  23. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main(){
  6.     float t1,t2,pass;
  7.     int x,y,answer,answer_c;
  8.     srand(time(NULL));
  9. re:
  10. while(true){
  11.     x=rand()%48+51;
  12.     y=rand()%48+51;
  13.     answer_c=x+y;
  14.     cout<<x<<"+"<<y<<"=";
  15.     t1=clock();
  16.     cin>>answer;
  17.     t2=clock();
  18.     pass=(t2-t1)/1000;
  19.         if(answer==answer_c){
  20.             cout<<"答對了!本題花"<<pass<<"秒思考"<<endl;
  21.             break;
  22.         }
  23.         else {
  24.             cout<<"答錯了!本題花"<<pass<<"秒思考"<<endl;
  25.             break;
  26.         }


  27.   }

  28.     system ("pause");
  29.     goto re;
  30.     return 0;

  31. }
複製代碼

TOP

本帖最後由 李偈睿 於 2024-8-17 15:04 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     srand(time(NULL));
  8.     int x, y, ans, ans_c;
  9.     long t1,t2,pass;
  10.     while(true)
  11.     {
  12.         x=rand()%49+51;   
  13.         y=rand()%49+51;
  14.         ans_c=x+y;
  15.         cout<<x<<" + "<<y<<" = ";
  16.         t1=clock();
  17.         cin>>ans;
  18.         t2=clock();
  19.         pass=t2-t1;
  20.         if(ans==ans_c)
  21.             cout<<"答對了! 本題花了"<<pass<<"毫秒思考!"<<endl;
  22.         else
  23.             cout<<"答錯了! 本題花了"<<pass<<"毫秒思考!"<<endl;  
  24.         cout<<endl;
  25.     }
  26.     system("pause");
  27.     return 0;
  28. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. #include<cmath>
  5. using namespace std;
  6. int main()
  7. {
  8.     float a,b,c;
  9.     srand(time(NULL));
  10.     int n[2];
  11.     re:
  12.     for(int i=0; i<2; i++)
  13.         {
  14.              n[i]=rand()%49+51;

  15.         }
  16.     cout<<n[0]<<"+"<<n[1]<<"=?";
  17.     cin>>a;
  18.     b=clock()-c;
  19.     if(a==n[0]+n[1]){

  20.         cout<<"答對了! 本題花了"<<b<<"毫秒思考"<<endl;


  21.      }else
  22.     {
  23.         cout<<"答錯了! 本題花了"<<b<<"毫秒思考"<<endl;
  24.     }
  25.     c=clock();


  26.     goto re;
  27.     system("pause");
  28.     return 0;
  29. }
複製代碼

TOP

本帖最後由 李唯銘 於 2024-8-17 14:54 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     srand(time(NULL));
  8.     int n[2], ans;
  9.     for(int j=0; j<2; j++)
  10.     {
  11.         n[j]=rand()%49+51;
  12.         for(int k=0; k<j; k++)
  13.         {
  14.             if(n[j]==n[k])
  15.             {
  16.                j--;
  17.                break;
  18.             }
  19.         }
  20.     }
  21.     cout<<n[0]<<"+";
  22.     cout<<n[1]<<"=";
  23.     cin>>ans;
  24.     if(ans==n[0]+n[1])
  25.         cout<<"答對了,用了"<<clock()<<"毫秒"<<endl;
  26.     else
  27.         cout<<"答錯了用了"<<clock()<<"毫秒"<<endl;
  28.     system("pause");
  29.     return 0;

  30. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.    int a,b,c;
  8.    float t,t1=0;
  9.    re:
  10.    system("cls");
  11.    a=rand()%49+51;
  12.    b=rand()%49+51;
  13.    cout<<a<<" + "<<b<<" = ";
  14.    cin>>c;
  15.    t=clock()-t1;
  16.    if(c==a+b)
  17.    {
  18.        cout<<"答對了! 本題花了"<<t<<"毫秒思考"<<endl;
  19.    }
  20.    else
  21.    {
  22.        cout<<"答錯了! 本題花了"<<t<<"毫秒思考"<<endl;
  23.    }
  24.    t1=clock();
  25.    system("pause");
  26.    goto re;
  27.    return 0;
  28. }
複製代碼

TOP

返回列表