?? alg_24.h
字號(hào):
/*錯(cuò)誤文件,利用遞歸對(duì)算術(shù)的“全運(yùn)算”,對(duì)計(jì)算符號(hào)的控制和中間變量的控制比較麻煩;
主要是對(duì)對(duì)遞歸的不熟練,更多心得在另一個(gè)頭文件中*/
/*#include<iostream>
#include<vector>
#include<string>
using namespace std;
const int Ans = 24;
/*遞歸的控制思考
對(duì)于要在回溯過程中利用的返回變量,應(yīng)該在程序中設(shè)置一些中間變量如a,b,c,利用它們可以很
好地返回到遞歸的上一層。
而對(duì)于要在全過程中利用的或者輸出的變量,則考慮將其設(shè)置為形式參數(shù),并在程序中自己改變其結(jié)果
如下面程序中的Exp,Number,這兩個(gè)變量分別都需要在全過程中利用到
*/
/*bool Calu_24(vector<double>Number,string Exp)
{
if(Number.size() == 1)
{
if(Number[0] == Ans)
{
cout<<"Success...."<<endl;
cout<<Exp<<endl;
return true;
}
else
{
return false;
}
}
else
{
double a,b,c;
char ch_a,ch_b;
a = Number[Number.size()-1];
ch_a = a+48;
Number.pop_back();
b = Number[Number.size()-1];
ch_b = b+48;
Number.pop_back();
c = a*b;
Number.push_back(c);
if(Number.size() == 3)
{
Exp = ch_a;
}
Exp = '('+Exp+'*'+ch_b+')';
if(Calu_24(Number,Exp)) return true;
c = a+b;
Number.pop_back();
Number.push_back(c);
Exp = Exp.erase(0,1);
Exp = Exp.erase(Exp.size()-3,3);
if(Number.size() == 3)
{
Exp = ch_a;
}
Exp = '('+Exp+'+'+ch_b+')';
if(Calu_24(Number,Exp)) return true;
if(Number.size() == 3)
{
Exp = ch_a;
}
if(a>b)
{
c = a-b;
}
else
{
c = b-a;
}
Exp = Exp.erase(0,1);
Exp = Exp.erase(Exp.size()-3,3);
Exp = '('+Exp+'-'+ch_b+')';
Number.pop_back();
Number.push_back(c);
if(Calu_24(Number,Exp)) return true;
c = a/b;
Number.pop_back();
Number.push_back(c);
Exp = Exp.erase(0,1);
Exp = Exp.erase(Exp.size()-3,3);
if(Number.size() == 3)
{
Exp = ch_a;
}
Exp = '('+Exp+'/'+ch_b+')';
if(Calu_24(Number,Exp)) return true;
else
Number.pop_back();
Exp = " ";
return false;
}
}
*/
/*void Insert_Char(char c1,string &str,int i)
{
if(i == 0)
{
str = c1 + str;
}
else
{
string Temp_str = str.substr(i);
str.erase(i,str.size()-i);
str = str + c1 +Temp_str;
}
}
void Rotate(vector<int>Number,string Exp)
{
int count = Number.size();
vector<string>temp;
vector<string>store;
store.push_back("0");
string T_Str;
char ch_i;
for(int i = 1;i<count;i++)
{
for(int j = 0;j<store.size();j++)
{
T_Str = store[j];
for(int t = 0;t<=store[j].size();t++)
{
ch_i = i+48;
Insert_Char(ch_i,T_Str,t);
temp.push_back(T_Str);
T_Str = store[j];
}
}
while(!store.empty())
{
store.pop_back();
}
store = temp;
while(!temp.empty())
{
temp.pop_back();
}
}
//以上得出下標(biāo)排列
vector<int>Sort_Number;
int T_int;
bool test = false;
for(i = 0;i<store.size();i++)
{
for(int j = 0;j<Number.size();j++)
{
T_int = store[i][j];
T_int = T_int-48;
Sort_Number.push_back(Number[T_int]);
}
if(Calu_24(Sort_Number,Exp))
{
test = true;
break;
}
}
if(!test)
cout<<"Fail To Compute..."<<endl;
else
cout<<"Success!!"<<endl;
}
*/
/*
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const double PRECISION = 1E-6;
const int COUNT_OF_NUMBER = 4;
const int NUMBER_TO_CAL = 24;
double number[COUNT_OF_NUMBER];
string expression[COUNT_OF_NUMBER];
bool Search(int n)
{
if (n == 1) {
if ( fabs(number[0] - NUMBER_TO_CAL) < PRECISION ) {
cout << expression[0] << endl;
return true;
} else {
return false;
}
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double a, b;
string expa, expb;
a = number[i];
b = number[j];
number[j] = number[n - 1];
expa = expression[i];
expb = expression[j];
expression[j] = expression[n - 1];
expression[i] = '(' + expa + '+' + expb + ')';
number[i] = a + b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '-' + expb + ')';
number[i] = a - b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expb + '-' + expa + ')';
number[i] = b - a;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '*' + expb + ')';
number[i] = a * b;
if ( Search(n - 1) ) return true;
if (b != 0) {
expression[i] = '(' + expa + '/' + expb + ')';
number[i] = a / b;
if ( Search(n - 1) ) return true;
}
if (a != 0) {
expression[i] = '(' + expb + '/' + expa + ')';
number[i] = b / a;
if ( Search(n - 1) ) return true;
}
number[i] = a;
number[j] = b;
expression[i] = expa;
expression[j] = expb;
}
}
return false;
}
void main()
{
for (int i = 0; i < COUNT_OF_NUMBER; i++) {
char buffer[20];
int x;
cin >> x;
number[i] = x;
itoa(x, buffer, 10);
expression[i] = buffer;
}
if ( Search(COUNT_OF_NUMBER) ) {
cout << "Success." << endl;
} else {
cout << "Fail." << endl;
}
}
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -