爆打小组,四则运算,算法实现

组长严一格在讨论中说明了算法的需求,先实现了分5级难度,简单随机生成题目的算法.

 

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<ctime>
 4 #include<iostream>
 5 using namespace std;
 6 class Random{
 7 public:
 8     void init(){
 9         srand(time(0));
10     }
11     ///return number in [x,y]
12     int random_number_in(int x,int y){
13         return x+rand()%(y-x+1);
14     }
15 }myrandom;
16 class Number{
17     int cutoff[8]={10,20,50,100,500};
18     char buffer[16];
19 public:
20     string get(int n){
21         int x=myrandom.random_number_in(1,cutoff[n-1]);
22         sprintf(buffer,"%d",x);
23         return (string)buffer;
24     }
25 }mynumber;
26 class Operator{
27     char op[8]="+-*/";
28 public:
29     char get(int n){
30         if(n==5) n--;
31         int x=myrandom.random_number_in(0,n-1);
32         return op[x];
33     }
34 }myoperator;
35 class Solver{
36     string result;
37 public:
38     string solve(int n){
39         if(n<1||n>5){
40             result="error";
41             return result;
42         }
43         myrandom.init();
44         result=mynumber.get(n);
45         for(int i=0;i<n;i++){
46             result+=myoperator.get(n);
47             result+=mynumber.get(n);
48         }
49         return result;
50     }
51 }mysolver;
52 int main(){
53     int n;
54     while(cin>>n){
55         cout<<mysolver.solve(n)<<endl;
56     }
57     return 0;
58 }
View Code

 

 

接下去将再该算法的基础上,进一步优化,添加功能点.具体做法将在小组讨论中确定.

posted on 2016-03-29 21:17  gaolzzxin  阅读(320)  评论(3编辑  收藏  举报