Money Systems

请考虑一个由1到N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N。现在请在数列中插入“+”表示加,或者“-”表示减,“ ”表示空白(例如1-2 3就等于1-23),来将每一对数字组合在一起(请不在第一个数字前插入符号)。计算该表达式的结果并判断其值是否为0。请你写一个程序找出所有产生和为零的长度为N的数列。

E.X:

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

怎么做呢。。直接模拟但是怎么写更加优美呢。膜拜大神代码。简单明了0_0_0_0_0_0_这样的先把字符串模板摆好后,神奇的dfs一下就OK了。应为填‘ ’的时候需要把上一个数(正or负)*10再(加or减)当前数字,所以我们需要存储一下last 表示最后一个未加入sum(总和)的数字是多少。

其他的,不懂见代码。模拟一下就OK。代码:
/*
ID:Andy Chen
LANG:C++
PROG:zerosum
*/
#include<cstdio>
#include<iostream>
using namespace std;
int N;
char str[20];
void dfs(int k,int sum,int last){
    if(k==N){
        if(sum+last==0) cout<<str<<endl;
        return ;
    }
    str[k*2-1]=‘ ‘;
    dfs(k+1,sum,last>0?last*10+k+1:last*10-k-1);
    str[k*2-1]=‘+‘;
    dfs(k+1,sum+last,k+1);
    str[k*2-1]=‘-‘;
    dfs(k+1,sum+last,-k-1);
}
int main()
{
    freopen("zerosum.in","r",stdin);
    freopen("zerosum.out","w",stdout);
    scanf("%d",&N);
    for(int i=0;i<N;++i)
        str[i*2]=char(i+‘1‘);
    dfs(1,0,1);
    return 0;
}
时间: 2024-07-30 01:05:02

Money Systems的相关文章

General protection fault Exceptions in Linux/IA32 Systems

Computer Systems A Programmer's Perspective Second Edition Exception number Description Exception class0 Divide error Fault13 General protection fault Fault14 Page fault Fault18 Machine check Abort32–127 OS-de?ned exceptions Interrupt or trap128 (0x8

computer systems work from a programmer&#39;s perspective

2. typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, size_t len) { int i; for (i=0;i<len;i++) printf(" %.2x",start[i]; printf("\n"); } void inplace_swap(int *x,int *y) { *y = *x^*y; *x = *x^*y; *y = *x^*y; } c

Error 56: The Cisco Systems, Inc. VPN Service has not been started(Cisco VPN在Vista下出现Error 56的解决办法)

Error 56: The Cisco Systems, Inc. VPN Service has not been started(Cisco VPN在Vista下出现Error 56的解决办法) 似乎,自从我的Vista自动升级安装了SP1后,我的Cisco VPN就出现问题了,根本就无法运行.总是提示Error 56: The Cisco Systems, Inc. VPN Service has not been started. Please start this service an

好用大全OLI Systems 2010

全模块好用DNV sesam suite 2013 Full 1CD OLI Systems 2010(OLI Analyzer v3.1.3 + OLI ScaleChem v4.0.3)   KBC Petro-SIM Suite v4.0 SP2   QQ:16264558      TEL:13963782271   REFORM-3PC.V7.0   化学软件完整版ChemOffice Ultra 2008 V11.01

Toward Scalable Systems for Big Data Analytics: A Technology Tutorial (I - III)

ABSTRACT Recent technological advancement have led to a deluge of data from distinctive domains (e.g., health care and scientific sensors, user-generated data, Internet and financial companies, and supply chain systems) over the past two decades. The

Memory Leak Detection in Embedded Systems

One of the problems with developing embedded systems is the detection of memory leaks; I've found three tools that are useful for this. These tools are used to detect application program errors, not kernel memory leaks. Two of these tools (mtrace and

Methods and systems for sharing common job information

Apparatus and methods are provided for utilizing a plurality of processing units. A method comprises selecting a pending job from a plurality of unassigned jobs based on a plurality of assigned jobs for the plurality of processing units and assigning

Methods and systems to control virtual machines

Methods and systems are provided to control the execution of a virtual machine (VM). A VM Monitor (VMM) accesses VM Control Structures (VMCS) indirectly through access instructions passed to a processor. In one embodiment, the access instructions inc

A GUIDE TO UNDERSTANDINGDISCRETIONARY ACCESS CONTROL INTRUSTED SYSTEMS

1. INTRODUCTION ? The main goal of the National Computer Security Center is to encourage the widespread availability of trusted computer systems. In support of that goal a metric was created, the Department of Defense Trusted Computer System Evaluati

全方位的作业Lindo.Systems.Lingo.v9.0.Incl.Patch+COMSOL.Multiphysics.V5.2高级数值

全方位的作业Lindo.Systems.Lingo.v9.0.Incl.Patch+COMSOL.Multiphysics.V5.2高级数值  Lindo.Systems.Lingo.v9.0.Incl.Patch(全方位的作业研究软件)GraphPad.Prism.v4.03(著名的数据处理 软件,用来进行生物学统计.曲线拟合以及作图) Genstat.v8.2.SP1-ISO 1CD(国际常用统计软件) Lindo.Systems.Lingo.v9.0.Incl.Patch(全方位的作业研究