|
发表于 2023-2-17 17:11:45
|
显示全部楼层
本楼为最佳答案
本帖最后由 ExiaGN001 于 2023-2-19 11:10 编辑
我就说下思路。
1.符号用随机数生成,1代表+,0代表-
随机数初始化 srand(time(NULL))调用rand()
2./3.
对于第i次生成,做以下操作:
通过rand()%10得到三个参数的值
通过rand()&1得到两个运算符
根据三个参数和运算符计算表达式,看结果是否满足条件
如果不满足条件,直接continue,i不变
否则遍历答案数组前i-1个个元素,检查是否重复(答案数组是100*6的二维数组,6个位置分别是3个参数,两个运算符和一个(正确的)计算结果)
如果有重复,直接continue,i不变
不然i自增1,将这回的结果计入答案数组
4.
如果导出,首先获取用户名(这个要用到winapi部分的函数)
然后用用户名按照这个格式生成桌面路径(仅限Windows)
C:\Users\[用户名]\Desktop
斜杠不分正反,字母不区分大小写
//如果程序就在桌面不用生成桌面路径,直接fopen([文件名],"r");
利用fopen和fprintf输出上文的答案数组即可
记得fclose
5.预先把答案算出来,和输入值比较即可。
- #include<bits/stdc++.h>
- #include<windows.h>
- #include<shlobj.h>
- using namespace std;
- int ans[105][6];//最后的结果
- int tmp[6];//当前查看的结果
- /*
- ans 和tmp的存储格式
- 0 1 2 3 4 5
- 操作数1 运算符1 操作数2 运算符2 操作数3 结果
- */
- int cnt=0;
- const int maxnum=10,minnum=0;
- inline int create_optr()//生成一个运算符
- {
- return rand()%2;//0代表+ 1代表-
- }
- inline int create_num()//生成一个操作数
- {
- return rand()%(maxnum-minnum)+minnum;//maxnum=10 minnum=0
- }
- string getDesktopPath()//获取桌面位置
- {
- LPITEMIDLIST pidl;
- LPMALLOC pShellMalloc;
- char szDir[200];
- if (SUCCEEDED(SHGetMalloc(&pShellMalloc)))
- {
- if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl))) {
- // 如果成功返回true
- SHGetPathFromIDListA(pidl, szDir);
- pShellMalloc->Free(pidl);
- }
- pShellMalloc->Release();
- }
- return string(szDir);
- }
- int get_ans()//根据随机出来的操作数确定答案
- {
- int tmpans=tmp[0];
- if(tmp[1])tmpans-=tmp[2];
- else tmpans+=tmp[2];
- if(tmp[3])tmpans-=tmp[4];
- else tmpans+=tmp[4];
- return tmpans;
- }
- bool check_dupi()//查重/查合法
- {
- if(tmp[5]<0||tmp[5]>20)return 0;
- int issame;//is_same是关键字,所以没加下划线
- for(int i=0;i<cnt;i++)
- {
- issame=0;
- for(int j=0;j<6;j++)
- {
- issame+=(ans[i][j]==tmp[j]);
- }
- if(issame==6)return 0;//只要有一个匹配就返回0
- }
- return 1;
- }
- void output()//向终端
- {
- for(int i=0;i<100;i++)
- {
- printf("%d %c %d %c %d = %d\n",
- ans[i][0],(ans[i][1]?'-':'+'),
- ans[i][2],(ans[i][3]?'-':'+'),
- ans[i][4],ans[i][5]);
- }
- }
- bool output_desktop()//检查结果
- {
- string pth=getDesktopPath();
- char path[275];//Windows系统路径长度上限260,开275足够
- for(int i=0;i<pth.size();i++)path[i]=pth[i];
- path[pth.size()]='\0';
- strcat(path,"\problem.txt");
- cout<<path<<"\n";
- FILE *fp=fopen(path,"w");
- for(int i=0;i<100;i++)
- {
- fprintf(fp,"%d %c %d %c %d = %d\n",
- ans[i][0],(ans[i][1]?'-':'+'),
- ans[i][2],(ans[i][3]?'-':'+'),
- ans[i][4],ans[i][5]);
- }
- fclose(fp);
- char code[314]="start ";
- strcat(code,path);
- system(code);
- }
- int main()
- {
- srand(time(NULL));
- while(cnt<100)
- {
- tmp[0]=create_num();
- tmp[1]=create_optr();
- tmp[2]=create_num();
- tmp[3]=create_optr();
- tmp[4]=create_num();
- tmp[5]=get_ans();
- if(check_dupi())
- {
- for(int i=0;i<6;i++)
- {
- ans[cnt][i]=tmp[i];
- }
- cnt++;
- }
- }
- //output();
- cout<<"是否导出(Y/N)?";
- char optr;
- cin>>optr;
- if(optr%32+64=='Y')
- output_desktop();
- else
- ;//啥都不干
-
- return 0;
- }
复制代码
|
|