在表达式“123456789=100”左边的适当位置插入运算符“+”、“-”,如何使等式成立?
例:123+45-67+8-9=100
8 个解决方案
#1
有同学提出设置字符数组存储左边的表达式,并给该字符数组赋初值,在1-9之间任意插入“ ”、“+”、“-”,然后再扫描字符串,进行匹配计算,不知是否可行?
请高手指点一二!
请高手指点一二!
#2
穷举法一个一个地试
#3
从1-9遍历
#4
遍历
#5
大概需要10^5数量级的试探,如果用穷举的话
#6
大概是3^8
#7
#include "iostream.h"
#include "string.h"
void PrintResult();
int GetValue(char *p);
const char* RemoveSpace(const char* p);
int main(int argc, char* argv[])
{
PrintResult();
return 0;
}
//设表达式为(x1)1(x2)2...(x9)9,其中x1,x2,...,x9为{'+','-',' '}中的一个,
//列出所有可能的情况,求相应的值就可以了。
void PrintResult()
{
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
char operators[] ={'+','-',' '};
char expression[19];
int i,operator_count = 3,number_count = 9;
int result = 100;
for(i=0;i<number_count;i++)//填入表达式中的数字
expression[2*i +1] = i+1+'0';
expression[18]='\0';//字符串结尾
for(k1=1;k1<operator_count;k1++)//k1为+和为空格一样,所以假设k1不能为+
{
expression[0]=operators[k1];
for(k2=0;k2<operator_count;k2++)
{
expression[2]=operators[k2];
for(k3=0;k3<operator_count;k3++)
{
expression[4]=operators[k3];
for(k4=0;k4<operator_count;k4++)
{
expression[6]=operators[k4];
for(k5=0;k5<operator_count;k5++)
{
expression[8]=operators[k5];
for(k6=0;k6<operator_count;k6++)
{
expression[10]=operators[k6];
for(k7=0;k7<operator_count;k7++)
{
expression[12]=operators[k7];
for(k8=0;k8<operator_count;k8++)
{
expression[14]=operators[k8];
for(k9=0;k9<operator_count;k9++)
{
expression[16]=operators[k9];
if(GetValue(expression)==result)
cout << RemoveSpace(expression) << endl;
}
}
}
}
}
}
}
}
}
}
//求表达式(x1)1(x2)2...(x9)9的值
int GetValue(char *p)
{
int result =0;
int i=0;
while(p[i])
{
switch(p[i])
{
case '+':
return result + GetValue(p+i+1);
case '-':
return result - GetValue(p+i+1);
case ' ':
result = result*10 + p[i+1]-'0';
i+=2;
break;
default://如果是数字
result = result*10 + p[i]-'0';
i++;
break;
}
}
return result;
}
const char* RemoveSpace(const char* p)
{
int i=0,j=0;
static char buff[19];
while(p[i])
{
if(p[i]!=' ')
{
buff[j]=p[i];
j++;
}
i++;
}
buff[j]='\0';
return buff;
}
#include "string.h"
void PrintResult();
int GetValue(char *p);
const char* RemoveSpace(const char* p);
int main(int argc, char* argv[])
{
PrintResult();
return 0;
}
//设表达式为(x1)1(x2)2...(x9)9,其中x1,x2,...,x9为{'+','-',' '}中的一个,
//列出所有可能的情况,求相应的值就可以了。
void PrintResult()
{
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
char operators[] ={'+','-',' '};
char expression[19];
int i,operator_count = 3,number_count = 9;
int result = 100;
for(i=0;i<number_count;i++)//填入表达式中的数字
expression[2*i +1] = i+1+'0';
expression[18]='\0';//字符串结尾
for(k1=1;k1<operator_count;k1++)//k1为+和为空格一样,所以假设k1不能为+
{
expression[0]=operators[k1];
for(k2=0;k2<operator_count;k2++)
{
expression[2]=operators[k2];
for(k3=0;k3<operator_count;k3++)
{
expression[4]=operators[k3];
for(k4=0;k4<operator_count;k4++)
{
expression[6]=operators[k4];
for(k5=0;k5<operator_count;k5++)
{
expression[8]=operators[k5];
for(k6=0;k6<operator_count;k6++)
{
expression[10]=operators[k6];
for(k7=0;k7<operator_count;k7++)
{
expression[12]=operators[k7];
for(k8=0;k8<operator_count;k8++)
{
expression[14]=operators[k8];
for(k9=0;k9<operator_count;k9++)
{
expression[16]=operators[k9];
if(GetValue(expression)==result)
cout << RemoveSpace(expression) << endl;
}
}
}
}
}
}
}
}
}
}
//求表达式(x1)1(x2)2...(x9)9的值
int GetValue(char *p)
{
int result =0;
int i=0;
while(p[i])
{
switch(p[i])
{
case '+':
return result + GetValue(p+i+1);
case '-':
return result - GetValue(p+i+1);
case ' ':
result = result*10 + p[i+1]-'0';
i+=2;
break;
default://如果是数字
result = result*10 + p[i]-'0';
i++;
break;
}
}
return result;
}
const char* RemoveSpace(const char* p)
{
int i=0,j=0;
static char buff[19];
while(p[i])
{
if(p[i]!=' ')
{
buff[j]=p[i];
j++;
}
i++;
}
buff[j]='\0';
return buff;
}
#8
#include "iostream.h"
#include "string.h"
void PrintResult();
int GetValue(char *p);
const char* RemoveSpace(const char* p);
int main(int argc, char* argv[])
{
PrintResult();
return 0;
}
//设表达式为(x1)1(x2)2...(x9)9,其中x1,x2,...,x9为{'+','-',' '}中的一个,
//列出所有可能的情况,求相应的值就可以了。
void PrintResult()
{
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
char operators[] ={'+','-',' '};
char expression[19];
int i,operator_count = 3,number_count = 9;
int result = 100;
for(i=0;i<number_count;i++)//填入表达式中的数字
expression[2*i +1] = i+1+'0';
expression[18]='\0';//字符串结尾
for(k1=1;k1<operator_count;k1++)//k1为+和为空格一样,所以假设k1不能为+
{
expression[0]=operators[k1];
for(k2=0;k2<operator_count;k2++)
{
expression[2]=operators[k2];
for(k3=0;k3<operator_count;k3++)
{
expression[4]=operators[k3];
for(k4=0;k4<operator_count;k4++)
{
expression[6]=operators[k4];
for(k5=0;k5<operator_count;k5++)
{
expression[8]=operators[k5];
for(k6=0;k6<operator_count;k6++)
{
expression[10]=operators[k6];
for(k7=0;k7<operator_count;k7++)
{
expression[12]=operators[k7];
for(k8=0;k8<operator_count;k8++)
{
expression[14]=operators[k8];
for(k9=0;k9<operator_count;k9++)
{
expression[16]=operators[k9];
if(GetValue(expression)==result)
cout << RemoveSpace(expression) << endl;
}
}
}
}
}
}
}
}
}
}
//求表达式(x1)1(x2)2...(x9)9的值
int GetValue(char *p)
{
int result =0;
int i=0;
while(p[i])
{
switch(p[i])
{
case '+':
return result + GetValue(p+i+1);
case '-':
return result - GetValue(p+i+1);
case ' ':
result = result*10 + p[i+1]-'0';
i+=2;
break;
default://如果是数字
result = result*10 + p[i]-'0';
i++;
break;
}
}
return result;
}
const char* RemoveSpace(const char* p)
{
int i=0,j=0;
static char buff[19];
while(p[i])
{
if(p[i]!=' ')
{
buff[j]=p[i];
j++;
}
i++;
}
buff[j]='\0';
return buff;
}
#include "string.h"
void PrintResult();
int GetValue(char *p);
const char* RemoveSpace(const char* p);
int main(int argc, char* argv[])
{
PrintResult();
return 0;
}
//设表达式为(x1)1(x2)2...(x9)9,其中x1,x2,...,x9为{'+','-',' '}中的一个,
//列出所有可能的情况,求相应的值就可以了。
void PrintResult()
{
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
char operators[] ={'+','-',' '};
char expression[19];
int i,operator_count = 3,number_count = 9;
int result = 100;
for(i=0;i<number_count;i++)//填入表达式中的数字
expression[2*i +1] = i+1+'0';
expression[18]='\0';//字符串结尾
for(k1=1;k1<operator_count;k1++)//k1为+和为空格一样,所以假设k1不能为+
{
expression[0]=operators[k1];
for(k2=0;k2<operator_count;k2++)
{
expression[2]=operators[k2];
for(k3=0;k3<operator_count;k3++)
{
expression[4]=operators[k3];
for(k4=0;k4<operator_count;k4++)
{
expression[6]=operators[k4];
for(k5=0;k5<operator_count;k5++)
{
expression[8]=operators[k5];
for(k6=0;k6<operator_count;k6++)
{
expression[10]=operators[k6];
for(k7=0;k7<operator_count;k7++)
{
expression[12]=operators[k7];
for(k8=0;k8<operator_count;k8++)
{
expression[14]=operators[k8];
for(k9=0;k9<operator_count;k9++)
{
expression[16]=operators[k9];
if(GetValue(expression)==result)
cout << RemoveSpace(expression) << endl;
}
}
}
}
}
}
}
}
}
}
//求表达式(x1)1(x2)2...(x9)9的值
int GetValue(char *p)
{
int result =0;
int i=0;
while(p[i])
{
switch(p[i])
{
case '+':
return result + GetValue(p+i+1);
case '-':
return result - GetValue(p+i+1);
case ' ':
result = result*10 + p[i+1]-'0';
i+=2;
break;
default://如果是数字
result = result*10 + p[i]-'0';
i++;
break;
}
}
return result;
}
const char* RemoveSpace(const char* p)
{
int i=0,j=0;
static char buff[19];
while(p[i])
{
if(p[i]!=' ')
{
buff[j]=p[i];
j++;
}
i++;
}
buff[j]='\0';
return buff;
}
#1
有同学提出设置字符数组存储左边的表达式,并给该字符数组赋初值,在1-9之间任意插入“ ”、“+”、“-”,然后再扫描字符串,进行匹配计算,不知是否可行?
请高手指点一二!
请高手指点一二!
#2
穷举法一个一个地试
#3
从1-9遍历
#4
遍历
#5
大概需要10^5数量级的试探,如果用穷举的话
#6
大概是3^8
#7
#include "iostream.h"
#include "string.h"
void PrintResult();
int GetValue(char *p);
const char* RemoveSpace(const char* p);
int main(int argc, char* argv[])
{
PrintResult();
return 0;
}
//设表达式为(x1)1(x2)2...(x9)9,其中x1,x2,...,x9为{'+','-',' '}中的一个,
//列出所有可能的情况,求相应的值就可以了。
void PrintResult()
{
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
char operators[] ={'+','-',' '};
char expression[19];
int i,operator_count = 3,number_count = 9;
int result = 100;
for(i=0;i<number_count;i++)//填入表达式中的数字
expression[2*i +1] = i+1+'0';
expression[18]='\0';//字符串结尾
for(k1=1;k1<operator_count;k1++)//k1为+和为空格一样,所以假设k1不能为+
{
expression[0]=operators[k1];
for(k2=0;k2<operator_count;k2++)
{
expression[2]=operators[k2];
for(k3=0;k3<operator_count;k3++)
{
expression[4]=operators[k3];
for(k4=0;k4<operator_count;k4++)
{
expression[6]=operators[k4];
for(k5=0;k5<operator_count;k5++)
{
expression[8]=operators[k5];
for(k6=0;k6<operator_count;k6++)
{
expression[10]=operators[k6];
for(k7=0;k7<operator_count;k7++)
{
expression[12]=operators[k7];
for(k8=0;k8<operator_count;k8++)
{
expression[14]=operators[k8];
for(k9=0;k9<operator_count;k9++)
{
expression[16]=operators[k9];
if(GetValue(expression)==result)
cout << RemoveSpace(expression) << endl;
}
}
}
}
}
}
}
}
}
}
//求表达式(x1)1(x2)2...(x9)9的值
int GetValue(char *p)
{
int result =0;
int i=0;
while(p[i])
{
switch(p[i])
{
case '+':
return result + GetValue(p+i+1);
case '-':
return result - GetValue(p+i+1);
case ' ':
result = result*10 + p[i+1]-'0';
i+=2;
break;
default://如果是数字
result = result*10 + p[i]-'0';
i++;
break;
}
}
return result;
}
const char* RemoveSpace(const char* p)
{
int i=0,j=0;
static char buff[19];
while(p[i])
{
if(p[i]!=' ')
{
buff[j]=p[i];
j++;
}
i++;
}
buff[j]='\0';
return buff;
}
#include "string.h"
void PrintResult();
int GetValue(char *p);
const char* RemoveSpace(const char* p);
int main(int argc, char* argv[])
{
PrintResult();
return 0;
}
//设表达式为(x1)1(x2)2...(x9)9,其中x1,x2,...,x9为{'+','-',' '}中的一个,
//列出所有可能的情况,求相应的值就可以了。
void PrintResult()
{
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
char operators[] ={'+','-',' '};
char expression[19];
int i,operator_count = 3,number_count = 9;
int result = 100;
for(i=0;i<number_count;i++)//填入表达式中的数字
expression[2*i +1] = i+1+'0';
expression[18]='\0';//字符串结尾
for(k1=1;k1<operator_count;k1++)//k1为+和为空格一样,所以假设k1不能为+
{
expression[0]=operators[k1];
for(k2=0;k2<operator_count;k2++)
{
expression[2]=operators[k2];
for(k3=0;k3<operator_count;k3++)
{
expression[4]=operators[k3];
for(k4=0;k4<operator_count;k4++)
{
expression[6]=operators[k4];
for(k5=0;k5<operator_count;k5++)
{
expression[8]=operators[k5];
for(k6=0;k6<operator_count;k6++)
{
expression[10]=operators[k6];
for(k7=0;k7<operator_count;k7++)
{
expression[12]=operators[k7];
for(k8=0;k8<operator_count;k8++)
{
expression[14]=operators[k8];
for(k9=0;k9<operator_count;k9++)
{
expression[16]=operators[k9];
if(GetValue(expression)==result)
cout << RemoveSpace(expression) << endl;
}
}
}
}
}
}
}
}
}
}
//求表达式(x1)1(x2)2...(x9)9的值
int GetValue(char *p)
{
int result =0;
int i=0;
while(p[i])
{
switch(p[i])
{
case '+':
return result + GetValue(p+i+1);
case '-':
return result - GetValue(p+i+1);
case ' ':
result = result*10 + p[i+1]-'0';
i+=2;
break;
default://如果是数字
result = result*10 + p[i]-'0';
i++;
break;
}
}
return result;
}
const char* RemoveSpace(const char* p)
{
int i=0,j=0;
static char buff[19];
while(p[i])
{
if(p[i]!=' ')
{
buff[j]=p[i];
j++;
}
i++;
}
buff[j]='\0';
return buff;
}
#8
#include "iostream.h"
#include "string.h"
void PrintResult();
int GetValue(char *p);
const char* RemoveSpace(const char* p);
int main(int argc, char* argv[])
{
PrintResult();
return 0;
}
//设表达式为(x1)1(x2)2...(x9)9,其中x1,x2,...,x9为{'+','-',' '}中的一个,
//列出所有可能的情况,求相应的值就可以了。
void PrintResult()
{
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
char operators[] ={'+','-',' '};
char expression[19];
int i,operator_count = 3,number_count = 9;
int result = 100;
for(i=0;i<number_count;i++)//填入表达式中的数字
expression[2*i +1] = i+1+'0';
expression[18]='\0';//字符串结尾
for(k1=1;k1<operator_count;k1++)//k1为+和为空格一样,所以假设k1不能为+
{
expression[0]=operators[k1];
for(k2=0;k2<operator_count;k2++)
{
expression[2]=operators[k2];
for(k3=0;k3<operator_count;k3++)
{
expression[4]=operators[k3];
for(k4=0;k4<operator_count;k4++)
{
expression[6]=operators[k4];
for(k5=0;k5<operator_count;k5++)
{
expression[8]=operators[k5];
for(k6=0;k6<operator_count;k6++)
{
expression[10]=operators[k6];
for(k7=0;k7<operator_count;k7++)
{
expression[12]=operators[k7];
for(k8=0;k8<operator_count;k8++)
{
expression[14]=operators[k8];
for(k9=0;k9<operator_count;k9++)
{
expression[16]=operators[k9];
if(GetValue(expression)==result)
cout << RemoveSpace(expression) << endl;
}
}
}
}
}
}
}
}
}
}
//求表达式(x1)1(x2)2...(x9)9的值
int GetValue(char *p)
{
int result =0;
int i=0;
while(p[i])
{
switch(p[i])
{
case '+':
return result + GetValue(p+i+1);
case '-':
return result - GetValue(p+i+1);
case ' ':
result = result*10 + p[i+1]-'0';
i+=2;
break;
default://如果是数字
result = result*10 + p[i]-'0';
i++;
break;
}
}
return result;
}
const char* RemoveSpace(const char* p)
{
int i=0,j=0;
static char buff[19];
while(p[i])
{
if(p[i]!=' ')
{
buff[j]=p[i];
j++;
}
i++;
}
buff[j]='\0';
return buff;
}
#include "string.h"
void PrintResult();
int GetValue(char *p);
const char* RemoveSpace(const char* p);
int main(int argc, char* argv[])
{
PrintResult();
return 0;
}
//设表达式为(x1)1(x2)2...(x9)9,其中x1,x2,...,x9为{'+','-',' '}中的一个,
//列出所有可能的情况,求相应的值就可以了。
void PrintResult()
{
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
char operators[] ={'+','-',' '};
char expression[19];
int i,operator_count = 3,number_count = 9;
int result = 100;
for(i=0;i<number_count;i++)//填入表达式中的数字
expression[2*i +1] = i+1+'0';
expression[18]='\0';//字符串结尾
for(k1=1;k1<operator_count;k1++)//k1为+和为空格一样,所以假设k1不能为+
{
expression[0]=operators[k1];
for(k2=0;k2<operator_count;k2++)
{
expression[2]=operators[k2];
for(k3=0;k3<operator_count;k3++)
{
expression[4]=operators[k3];
for(k4=0;k4<operator_count;k4++)
{
expression[6]=operators[k4];
for(k5=0;k5<operator_count;k5++)
{
expression[8]=operators[k5];
for(k6=0;k6<operator_count;k6++)
{
expression[10]=operators[k6];
for(k7=0;k7<operator_count;k7++)
{
expression[12]=operators[k7];
for(k8=0;k8<operator_count;k8++)
{
expression[14]=operators[k8];
for(k9=0;k9<operator_count;k9++)
{
expression[16]=operators[k9];
if(GetValue(expression)==result)
cout << RemoveSpace(expression) << endl;
}
}
}
}
}
}
}
}
}
}
//求表达式(x1)1(x2)2...(x9)9的值
int GetValue(char *p)
{
int result =0;
int i=0;
while(p[i])
{
switch(p[i])
{
case '+':
return result + GetValue(p+i+1);
case '-':
return result - GetValue(p+i+1);
case ' ':
result = result*10 + p[i+1]-'0';
i+=2;
break;
default://如果是数字
result = result*10 + p[i]-'0';
i++;
break;
}
}
return result;
}
const char* RemoveSpace(const char* p)
{
int i=0,j=0;
static char buff[19];
while(p[i])
{
if(p[i]!=' ')
{
buff[j]=p[i];
j++;
}
i++;
}
buff[j]='\0';
return buff;
}