资源简介
c++ 计算m点问题,希望对大家有帮助!!!

代码片段和文件信息
#include
using namespace std;
void Backtrack(int t);
void HasSolution();
int n;
int m;
int value;
bool hasFind;
bool *use; // 记录工具已使用的情况
char *operatorr;
int *process;
int *datas;
void Backtrack(int t)
{
if (t >= n)
{
if (value == m)
{
hasFind = true;
}
}
else
{
for (int i = 0; i < n; ++i)
{
if (t == 0)
{
if (!use[i])
{
process[0] = datas[i];
value = datas[i];
use[i] = true;
Backtrack(t + 1);
use[i] = false;
}
}
else
{
if (!use[i])
{
process[t] = datas[i];
use[i] = true;
int temp = value;
if (datas[i] != 0)
{
operatorr[t - 1] = ‘/‘;
value = temp / datas[i];
Backtrack(t + 1);
if (hasFind)
{
return;
}
}
operatorr[t - 1] = ‘*‘;
value = temp * datas[i];
Backtrack(t + 1);
if (hasFind)
{
return;
}
operatorr[t - 1] = ‘-‘;
value = temp - datas[i];
Backtrack(t + 1);
if (hasFind)
{
return;
}
operatorr[t - 1] = ‘+‘;
value = temp + datas[i];
Backtrack(t + 1);
if (hasFind)
{
return;
}
value = temp;
use[i] = false;
}
}
}
}
}
void HasSolution()
{
FILE *file;
if ((file = fopen(“input.txt“ “r“)) == NULL)
{
cout << “File ‘input.txt‘ could not be opened!“ << endl;
}
else
{
fscanf (file “%d“ &n);
fscanf (file “%d“ &m);
cout << n << “ “ << m << endl;
hasFind = false;
use = new bool[n];
datas = new int[n];
process = new int[n];
operatorr = new char[n - 1];
for (int i = 0; i < n; ++i)
{
use[i] = false;
fscanf (file “%d“ &datas[i]);
cout << datas[i] << “ “;
}
cout << endl << endl;
fclose(file);
Backtrack(0);
if ((file = fopen(“output.txt“ “w“)) == NULL)
{
cout << “Could not create file ‘output.txt‘!“ << endl;
}
else
{
if (hasFind == false)
{
cout << “No Solution!“ << endl;
fprintf(file “No Solution!“);
}
else
{
for (i = 0; i < n - 1; ++i)
{
cout << process[i] << operatorr[i] << process[i + 1];
fprintf (file “%d%c%d“ process[i] operatorr[i] process[i+1]);
switch (operatorr[i])
{
case ‘/‘:
process[i + 1] = process[i] / process[i + 1];
break;
case ‘*‘:
process[i + 1] = process[i] * process[i + 1];
break;
case ‘-‘:
process[i + 1] = process[i] - process[i + 1];
break;
case ‘+‘:
process[i + 1] = process[i] + process[i + 1];
break;
default:
break;
}
cout << “=“ << process[i + 1] << “; “;
fprintf (file “=%d; “ process[i + 1]);
}
cout << endl;
}
fclose(file);
}
}
}
void main()
{
HasSolution();
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 518 2008-12-17 19:57 5-19 算m点问题\5to19\5to19.dsw
文件 33792 2008-12-17 20:03 5-19 算m点问题\5to19\5to19.ncb
文件 661 2008-12-17 20:03 5-19 算m点问题\5to19\5to19.plg
文件 2928 2008-12-17 20:02 5-19 算m点问题\5to19\m.cpp
文件 48640 2008-12-17 20:03 5-19 算m点问题\5to19\5to19.opt
文件 4268 2008-12-17 20:03 5-19 算m点问题\5to19\5to19.dsp
文件 82944 2008-12-17 20:04 5-19 算m点问题\5to19\Debug\vc60.idb
文件 110592 2008-12-17 20:04 5-19 算m点问题\5to19\Debug\vc60.pdb
文件 2001516 2008-12-17 20:02 5-19 算m点问题\5to19\Debug\5to19.pch
文件 2002596 2008-12-17 20:04 5-19 算m点问题\5to19\Debug\m.pch
文件 157981 2008-12-17 20:04 5-19 算m点问题\5to19\Debug\m.obj
文件 790236 2008-12-17 20:04 5-19 算m点问题\5to19\Debug\m.ilk
文件 553032 2008-12-17 20:04 5-19 算m点问题\5to19\Debug\m.exe
文件 1115136 2008-12-17 20:04 5-19 算m点问题\5to19\Debug\m.pdb
文件 17 2008-12-17 20:04 5-19 算m点问题\5to19\input.txt
文件 3341 2008-12-17 20:04 5-19 算m点问题\5to19\m.dsp
文件 33792 2008-12-17 20:08 5-19 算m点问题\5to19\m.ncb
文件 721 2008-12-17 20:04 5-19 算m点问题\5to19\m.plg
文件 41 2008-12-17 20:04 5-19 算m点问题\5to19\output.txt
文件 48640 2008-12-17 20:08 5-19 算m点问题\5to19\m.opt
文件 510 2008-12-17 20:08 5-19 算m点问题\5to19\m.dsw
目录 0 2008-12-17 19:57 5-19 算m点问题\5to19\Debug
目录 0 2008-12-17 20:03 5-19 算m点问题\5to19
目录 0 2008-12-17 19:56 5-19 算m点问题
----------- --------- ---------- ----- ----
6991902 24
- 上一篇:MFC音乐播放器
- 下一篇:vc Teechart8
相关资源
- 国际象棋的qt源代码
- C++中头文件与源文件的作用详解
- C++多线程网络编程Socket
- VC++ 多线程文件读写操作
- 利用C++哈希表的方法实现电话号码查
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- VC++MFC小游戏实例教程(实例)+MFC类库
- 连铸温度场计算程序(C++)
- 6自由度机器人运动学正反解C++程序
- Em算法(使用C++编写)
- libstdc++-4.4.7-4.el6.i686.rpm
- VC++实现CMD命令执行与获得返回信息
- 白话C++(全)
- C++标准库第1、2
- 大数类c++大数类
- C++语言编写串口调试助手
- c++素数筛选法
- C++ mqtt 用法
- 商品库存管理系统 C++ MFC
- c++ 多功能计算器
- C++17 In Detail
- 嵌入式QtC++编程课件
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
评论
共有 条评论