课程作业:随机生成四则运算
题目要求:
使用两个运算符,用于运算的数字和答案都必须是100以内的数字,不需要输出答案,且需避免重复,具备可扩展性,可定制输出数量。
#include<bits/stdc++.h> using namespace std; map<int, map<int,map<int,int> > > cnt; const char symbols[5] = {'+','-','*','/'}; void fun() { cnt.clear(); void prin(int total,int Max,int is_multiply_devide,int gap); int total,Max,is_multiply_devide,gap = 0; printf("请输入需要的题目数量:\n"); scanf("%d",&total); printf("请输入数值的最大值:\n"); scanf("%d",&Max); printf("是否需要加入乘除法?输入1仅代表加入乘除法,输入0表示只需加减法\n"); scanf("%d",&is_multiply_devide); printf("请输入每行间隔:"); scanf("%d",&gap); prin(total,Max,is_multiply_devide,gap); } void prin(int total,int Max,int is_multiply_devide,int gap) { int gen(int Max,int gap,int is_multiply_devide); char cal[5] = {'+','-','*','/'}; while(total) { total -= gen(Max,gap,is_multiply_devide); } } int gen(int Max,int gap,int is_multiply_devide) { int cal, numa = rand()%Max, numb = rand()%Max; if(is_multiply_devide) cal = rand()%4; else cal = rand()%2; if(cnt[numa][cal][numb]) return 0; //判断是否有重复出现的题目 cnt[numa][cal][numb] = 1; printf("%d %c %d = \n",numa,symbols[cal],numb); while(gap--) printf("\n"); return 1; } int main(void) { fun(); }