求C++程序设计,在123456789中间加入+或-使其等于99

来源:百度知道 编辑:UC知道 时间:2024/06/14 19:56:06

#include <iostream>
using namespace std;

int array[10]={0,1,2,3,4,5,6,7,8,9};
char oparray[10];

void dfs(int pos,char op,int value);

int main(void)
{
dfs (0,'+',0);
return 0;
}

void dfs(int pos,char op,int value)
{
int i;
if (pos==9)
{
if (value!=99)
return ;
int j=1;
for(i=0;i<10;++i)
{
if(oparray[i])
{
while(j<=i)
cout<<array[j++];
cout<<" "<<oparray[i]<<" ";
}
}
while(j<10)
cout<<array[j++];
cout<<" = 99";
cout<<endl;
return ;
}
int num=0;
int p;
if (op=='+')
p=1;
else
p=-1;
for(i=pos+1;i<10;++i)
{
num=num*10+array[i];
if(i==9)
{
dfs(i,'\0',value+num*p);<