USACO 2.3.3 Zero Sum 解题报告

Zero Sum

Consider the sequence of digits from 1 through N (where N=9) in increasing order: 1 2 3 ... N.

Now insert either a `+' for addition or a `-' for subtraction or a ` ' [blank] to run the digits together between each pair of digits (not in front of the first digit). Calculate the result that of the expression and see if you get zero.

Write a program that will find all sequences of length N that produce a zero sum.

PROGRAM NAME: zerosum
INPUT FORMAT

A single line with the integer N (3 <= N <= 9).

SAMPLE INPUT (file zerosum.in)
7
OUTPUT FORMAT

In ASCII order, show each sequence that can create 0 sum with a `+', `-', or ` ' between each pair of numbers.

SAMPLE OUTPUT (file zerosum.out)
1+2-3+4-5-6+7
1+2-3-4+5+6-7
1-2 3+4+5+6+7
1-2 3-4 5+6 7
1-2+3+4-5+6-7
1-2-3-4-5+6+7
题目意思很简单个,就是在序列1…N中每两个连续的数字间指定一个操作符(+、-或空格),使得最后的结果为0 输出这样的结果(有多个就按ASCII 码的顺序输出)
这题可以用DFS直接求解,通过在每两个连续的数间指定可能的运算符(按空格,+,-的顺序指定)。当运算到最后一个数时如果结果为0,则输出
参考代码
/* 
ID:shiryuw1 
PROG:zerosum 
LANG:C++ 
*/ 
#include<iostream> 
#include<cstdlib> 
#include<cstdio> 
#include<cstring> 
#include<algorithm> 
#include<cmath> 
using namespace std; 
int n; 
struct prog{ 
    char c[10]; 
    int pre; 
    int next; 
    char op; 
    int k; 
}; 
int add (int a,int b,char c) 
{ 
    switch(c) 
    { 
    case 0:return a;break; 
    case '+':return a+b;break; 
    case '-':return a-b;break; 
    } 
} 
void DFS(prog ans) 
{ 
    int i; 
    if(ans.k==n) 
    { 
        if(add(ans.pre,ans.next,ans.op)==0) 
        { 
            for(i=1;i<=n;i++) 
            { 
                if(i!=1) 
                    cout<<ans.c[i]; 
                cout<<i; 
            } 
            cout<<endl; 
        } 
        return ; 
    } 
    ans.k++; 
    prog tmp; 
    if(ans.op==0) 
    { 
        tmp=ans; 
        tmp.c[tmp.k]=' '; 
        tmp.pre=tmp.pre*10+tmp.k; 
        DFS(tmp);


        tmp=ans; 
        tmp.c[tmp.k]='+'; 
        tmp.next=tmp.k; 
        tmp.op='+'; 
        DFS(tmp); 
        tmp=ans; 
        tmp.c[tmp.k]='-'; 
        tmp.next=tmp.k; 
        tmp.op='-'; 
        DFS(tmp); 
    } 
    else 
    { 
        tmp=ans; 
        tmp.c[tmp.k]=' '; 
        tmp.next=tmp.next*10+tmp.k; 
        DFS(tmp);

        tmp=ans; 
        tmp.c[tmp.k]='+'; 
        tmp.pre=add(tmp.pre,tmp.next,tmp.op); 
        tmp.next=tmp.k; 
        tmp.op='+'; 
        DFS(tmp);

        tmp=ans; 
        tmp.c[tmp.k]='-'; 
        tmp.pre=add(tmp.pre,tmp.next,tmp.op); 
        tmp.next=ans.k; 
        tmp.op='-'; 
        DFS(tmp); 
    } 
} 
int main() 
{ 
    freopen("zerosum.in","r",stdin); 
    freopen("zerosum.out","w",stdout); 
    cin>>n; 
    prog ans; 
    ans.k=1; 
    ans.pre=1; 
    ans.op=0; 
    ans.next=0; 
    ans.c[0]=0; 
    DFS(ans); 
    return 0; 
}

转载于:https://www.cnblogs.com/ACShiryu/archive/2011/07/17/2108641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值