• 随机生成30道四则运算题NEW


    代码:

     1 #include <iostream>
     2 #include <time.h>
     3 using namespace std;
     4 
     5 void main()
     6 { 
     7     srand((int)time(NULL));     //每次执行种子不同,生成不同的随机数
     8     int a, b, c, d, e, i, mark,opt;
     9     for (i = 0; i < 30; i++)
    10     {
    11         opt = 1 + rand() % 2;//随机生成1或者2来表示整数和真分数
    12         a = 1 + rand() % 100;//将1到100内的数随机赋予a
    13         b = 1 + rand() % 100;//将1到100内的数随机赋予b
    14         c = 1 + rand() % 100;//将1到100内的数随机赋予c
    15         d = 1 + rand() % 100;//将1到100内的数随机赋予d
    16         if (opt == 1)
    17         {
    18             mark = 1 + rand() % 4;//随机生成1-4来分别表示四个运算符
    19             cout << a;
    20             switch (mark)
    21             {
    22             case 1:cout << "+"; break;
    23             case 2:cout << "-"; break;
    24             case 3:cout << "×"; break;
    25             case 4:cout << "÷"; break;
    26             }
    27             cout << b << "=" << endl;
    28         }
    29         if (opt == 2)
    30         {
    31             if (a > b)
    32             {
    33                 e = a;
    34                 a = b;
    35                 b = e;
    36             }
    37             cout << "(" << a << "/" << b <<")";
    38             mark = 1 + rand() % 4;
    39             switch (mark)
    40             {
    41             case 1:cout << "+"; break;
    42             case 2:cout << "-"; break;
    43             case 3:cout << "×"; break;
    44             case 4:cout << "÷"; break;
    45             }
    46             if (c > d)
    47             {
    48                 e = c;
    49                 c = d;
    50                 d = e;
    51             }
    52             cout << "(" << c << "/" << d << ")" <<"="<< endl;
    53         }
    54         
    55     }
    56 }

  • 相关阅读:
    UVa 12174 (滑动窗口) Shuffle
    UVa 1607 (二分) Gates
    CodeForces ZeptoLab Code Rush 2015
    HDU 1525 (博弈) Euclid's Game
    HDU 2147 (博弈) kiki's game
    UVa 11093 Just Finish it up
    UVa 10954 (Huffman 优先队列) Add All
    CodeForces Round #298 Div.2
    UVa 12627 (递归 计数 找规律) Erratic Expansion
    UVa 714 (二分) Copying Books
  • 原文地址:https://www.cnblogs.com/me-tts/p/5269688.html
Copyright © 2020-2023  润新知