读书人

1+2-3=0,12+3-4-5-6=0.解决方法

发布时间: 2013-10-05 17:54:48 作者: rapoo

1+2-3=0,12+3-4-5-6=0.....
#include <iostream>

#include <stdio.h>

#include <string.h>

#include <string>

#include <map>

#include <vector>

#include <set>

#include <algorithm>

#include <vector>

#include <stack>

#include <math.h>

#include <stdlib.h>

#define Max(a,b) ((a)>(b)?(a):(b))

#define Min(a,b) ((a)<(b)?(a):(b))

using namespace std;

typedef long long LL ;

const int size=18 ;

struct Me{

int N ;

int choose[size] ;

Me(){} ;

Me(int n):N(n){};

LL ans(){

// 1 ' '

// 2 '+'

// 3 '-'

LL sum ,now ;

int type=2 ;

sum=0 ;

now=1 ;

choose[N]=2 ;

for(int i=1;i<=N;i++){

if(choose[i]==2||choose[i]==3){

if(type==2)

sum+=now ;

else

sum-=now ;

type=choose[i] ;

now=i+1 ;

}

else{

if(i+1<=9)

now=now*10+i+1 ;

else

now=now*100+i+1 ;

}

}

return sum ;

}

void dfs(int id){

if(id==N){

if(ans()==0){

printf("%d",1) ;

for(int i=1;i<N;i++){

if(choose[i]==1)

putchar(' ') ;

else if(choose[i]==2)

putchar('+') ;

else

putchar('-') ;

printf("%d",i+1) ;

}

puts("") ;

}

return ;

}

for(int i=1;i<=3;i++){

choose[id]=i ;

dfs(id+1) ;

}

}

};

int main(){

int n ;

while(scanf("%d",&n)!=EOF){


Me me(n) ;

me.dfs(1) ;

}

return 0 ;

} 好像有问题,表示不懂
[解决办法]
是不是不打印出那个多余的空格:如果是的话,修改这里:
if(choose[i]==1)

putchar(' ') ;

else if(choose[i]==2)

putchar('+') ;
改为:
if(choose[i]==1)

{//putchar(' ') ;}

else if(choose[i]==2)

putchar('+') ;


[解决办法]
是改成这样:

if(choose[i]==1)

{}//putchar(' ') ;

else if(choose[i]==2)

putchar('+') ;

读书人网 >C++

热点推荐