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
题意:
给出 N(3 ~ 9),代表有N个数,有3种操作符,分别是 + 加法,- 减法,” “ 连续(空格说明相邻的这两个数合在一起,比如 2 3 代表23,1 2 3代表123),找到适合的等式使结果和为0。最后按字典序输出。
思路:
DFS。先把符号存起来当为 N - 1的时候再继续判断等式时候和为0,注意处理空格符号。
AC:
/* TASK:zerosum LANG:C++ ID:sum-g1 */ #include<stdio.h> #include<string.h> #include<stdlib.h> char way[5],fin[10]; int n; void dfs(int ans,char ope) { fin[ans] = ope; if(ans == n - 1) { int num[10],sum = 0; for(int i = 1;i <= n;i++) num[i] = i; for(int i = 1;i <= n - 1;i++) { int k = i,t = num[i]; while(fin[k] == ' ') { t = t * 10 + num[k + 1]; k++; } if(t != num[i]) num[i] = t; } sum += num[1]; for(int i = 1;i <= n - 1;i++) { if(fin[i] == ' ') continue; if(fin[i] == '+') sum += num[i + 1]; if(fin[i] == '-') sum -= num[i + 1]; } if(!sum) { printf("%d",1); for(int i = 1;i <= n - 1;i++) printf("%c%d",fin[i],i + 1); printf("\n"); } return; } for(int i = 1;i <= 3;i++) dfs(ans + 1,way[i]); } int main() { freopen("zerosum.in","r",stdin); freopen("zerosum.out","w",stdout); way[1] = ' ';way[2] = '+';way[3] = '-'; scanf("%d",&n); for(int i = 1;i <= 3;i++) dfs(1,way[i]); return 0; }
相关推荐
ZEROSUM鳞波X4ZEROSUMチヌX4PWZEROSUM鳞-宇崎日新.pdf
这个程序解决的是将N个数之间随意的加入+-或空格,其和为零的所有情况种类,算法实质是穷举,但是由于问题规模很小,因此算法也很快
零和游戏求解器Java库是开源Java库,用于解决2人有限零和游戏。 该程序通过线性编程解决零和游戏。
多重零和博弈中的信息溢出_Information Spillover in Multiple Zero-sum Games.pdf
不含短零和子序列的序列,曾祥能,袁平之,令~$G$~为有限阿贝尔群。众所周知,$G$~上长度不小于~$|G|$~的序列~$S$~都包含有一个长度不大于~$mathsf{h}(S)$~的零和子序列。研究相应的逆问�
此算法解决如下问题:给定数据数均为n(1≦n≦4000)的四张数据表A、B、C、D,数据类型为整型,试从每张表中选出一个数,使得四个数的和为0,问这样的组合共有多少?
matlab开发-ZeroSumGame。解零和矩阵游戏。
OrangePi_zero_H2_用户手册_v0.1 香橙派Zero是一款开源的单板电脑,新一代的arm开发板,它可以运行Android4.4、Ubuntu、Debian等操作系统。香橙派Zero使用全志H2系统级芯片,同时拥有256MB/512MB DDR3 内存(256MB为...
ASP.NET ZERO 带补丁亲测。 利用ABP框架搭建的模板项目,它会提供预建的页面及强大的基础设施架构。利用它提供的基础框架代码能让你快速的开发你的应用层。 基于Abp开发的aspnet-zero-core-9.0.0最新版本,测试可用...
The authors analyse the security of KECCAK (the winner in SHA-3 competition) by focusing on the zero-sum distinguishers of its underlying permutation (named KECCAK-f). The authors’ analyses are ...
ASP NET ZERO Core 10.1.0 带补丁亲测。 利用ABP框架搭建的模板项目,它会提供预建的页面及强大的基础设施架构。利用它提供的基础框架代码能让你快速的开发你的应用层。 基于Abp开发的ASP NET ZERO Core 10.1.0最新...
基于 AlphaZero 的博弈游戏 AI 研究与实现之实现一个 AlphaZero 框架
go-zero-master
Zero是一个功能丰富的C++数值计算核心库,不仅可取代clapack或者cblas, 还具备求根,拟合,插值,积分,随机数,统计,全局和局部的约束以及无约束优化,常微分方程求解等多项功能。 在线性计算方面: 可取代clapack或者cblas...
ORANGE_PI-ZERO-V1_1.pdf ORANGE_PI-ZERO线路接口信息 香橙派Zero是一款开源的单板电脑,新一代的arm开发板,它可以运行Android4.4、Ubuntu、Debian等操作系统。香橙派Zero使用全志H2系统级芯片,同时拥有256MB/512...
VC版Zero远控
AspNetZero_Renamer_2.11(asp.net zero用以重命名项目)
Xtreme-Firmware是 Flipper Zero 固件的强化版。它为 Flipper Zero 带来了更多的功能和自由度。让您的 Flipper 获得真正的力量和自由。不要犹豫,立即切换到唯一真正的大师!如果您拥有 Flipper Zero,这个固件将为...
zero_imager
零和游戏求解器JavaScript库