USCAO section 2.3 Zero Sum(dfs)

USACO section 3.4 Closed Fences(计算几何+叉积+二分) Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 i, yi}, i in (1..N). Every pair of adjacent vertices defines a side of the fence. 阅读详情
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


 

解题思路:直接深搜三种状态,最多3^8不超,注意点细节就OK了,纯水

 

/*
ID:nealgav1
PROG:zerosum
LANG:C++
*/
#include<fstream>
#include<cstring>
using namespace std;
const int mm=10000;
char s[mm][25],num;
char _s[25];
ifstream cin("zerosum.in");
ofstream cout("zerosum.out");
void dfs(char now,int x,int end,int sum,char pas)
{
  _s[2*x-3]=now;_s[2*x-2]=x+'0';
  if(now==' '&&pas=='+')
  {sum+=(x-1)*9+x;
   if(x==end)
   { _s[2*x-3]=now;_s[2*x-2]=x+'0';
     _s[0]='0'+1;
    _s[2*x-1]='\0';
    if(sum==0)cout<<_s<<"\n";
    return;
   }
  dfs(' ',x+1,end,sum,now);
  dfs('+',x+1,end,sum,now);
  dfs('-',x+1,end,sum,now);
  }
  else if(now==' '&&pas=='-')
  {
    sum=sum-x-(x-1)*9;
    if(x==end)
   { _s[2*x-3]=now;_s[2*x-2]=x+'0';
     _s[0]='0'+1;
    _s[2*x-1]='\0';
    if(sum==0)cout<<_s<<"\n";
    return;
   }
  dfs(' ',x+1,end,sum,now);
  dfs('+',x+1,end,sum,now);
  dfs('-',x+1,end,sum,now);
  }
  if(now=='+')
  {
    sum+=x;
    if(x==end)
    { _s[2*x-3]=now;_s[2*x-2]=x+'0';
      _s[0]='0'+1;
    _s[2*x-1]='\0';
    if(sum==0)cout<<_s<<"\n";
    return;
    }
  dfs(' ',x+1,end,sum,now);
  dfs('+',x+1,end,sum,now);
  dfs('-',x+1,end,sum,now);
  }
  if(now=='-')
  {sum-=x;
   if(x==end)
   { _s[2*x-3]=now;_s[2*x-2]=x+'0';
     _s[0]='0'+1;
    _s[2*x-1]='\0';
    if(sum==0)cout<<_s<<"\n";
    return;
   }
  dfs(' ',x+1,end,sum,now);
  dfs('+',x+1,end,sum,now);
  dfs('-',x+1,end,sum,now);
  }
}
int main()
{

  int m;
  cin>>m;
  num=0;
  dfs(' ',2,m,1,'+');
  dfs('+',2,m,1,'+');
  dfs('-',2,m,1,'+');
}


 

 

 

 

 

 

USER: Neal Gavin Gavin [nealgav1]
TASK: zerosum
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3580 KB]
   Test 2: TEST OK [0.000 secs, 3580 KB]
   Test 3: TEST OK [0.000 secs, 3580 KB]
   Test 4: TEST OK [0.000 secs, 3580 KB]
   Test 5: TEST OK [0.000 secs, 3580 KB]
   Test 6: TEST OK [0.000 secs, 3580 KB]
   Test 7: TEST OK [0.000 secs, 3580 KB]

All tests OK.

YOUR PROGRAM ('zerosum') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.

Here are the test data inputs:

------- test 1 ----
3
------- test 2 ----
4
------- test 3 ----
5
------- test 4 ----
6
------- test 5 ----
7
------- test 6 ----
8
------- test 7 ----
9
Keep up the good work!


Thanks for your submission!

 

Zero SumRuss Cox

We can use a simple recursive depth first search to generate all the possible strings to be had by putting in a space, plus, or minus sign between each number.

Once we've generated each string, we evaluate it as an arithmetic sum and see if we get zero. If so, we print the string.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

FILE *fout;
int n;

/* evaluate the string s as arithmetic and return the sum */
int
eval(char *s)
{
    int term, sign, sum;
    char *p;

    sign = 1;
    term = 0;
    sum = 0;
    for(p=s; *p; p++) {
        switch(*p){
        case '+':
        case '-':
            sum += sign*term;
            term = 0;
            sign = *p == '+' ? 1 : -1;
            break;
        case ' ':
            break;
        default:    /* digit */
            term = term*10 + *p-'0';
        }
    }
    sum += sign*term;
    return sum;
}

/* 
 * Insert + - or space after each number, and then  
 * test to see if we get zero.  The first k numbers have
 * already been taken care of.
 */
void
search(char *s, int k)
{
    char *p;

    if(k == n-1) {
        if(eval(s) == 0)
            fprintf(fout, "%s\n", s);
        return;
    }

    for(p=" +-"; *p; p++) {
        s[2*k+1] = *p;
        search(s, k+1);
    }
}


void
main(void)
{
    FILE *fin;
    int i;
    char str[30];

    fin = fopen("zerosum.in", "r");
    fout = fopen("zerosum.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d", &n);

    strcpy(str, "1 2 3 4 5 6 7 8 9");
    str[2*n-1] = '\0';    /* chop string to only have first n numbers */

    search(str, 0);

    exit(0);
}


 

库存管理基于Excel的多商品组合订单需求拆解模型:SKU级供需预测与四情景压力测试系统设计 Excel 多商品组合订单的需求拆解|Excel模型与模拟数据 内容概要:本文档围绕Excel中多商品组合订单的基础SKU需求拆解,构建从订单行、组合用量表到仓库周需求、期初可供、计划到货及欠交结转的完整计算链条。通过四种组合、六种SKU、两座仓库和八个业务周的模拟数据,演示如何将组合套数准确转换为SKU件数,防止重复扣减与错误状态纳入,并按周递推供需覆盖与欠交变化。文档提供结构化输入校验、公式体系、守恒检查与四情景复算机制,确保结果可追溯、可核对。全部数据为基于固定随机种子生成的原创模拟记录,配套可编辑Excel工作簿与独立Python程序实现双向验证。 适合人群:适用于负责订单汇总、组合商品管理、库存分析、仓配协同、需求计划及业务分析文档编制的相关人员。日常使用者需掌握Excel基本操作如单元格引用、筛选与求和;具备Python基础者可配合标准库运行程序进行批量验证或数据重生成。业务管理岗可重点阅读结果与风险章节,结合工作表索引追踪数值来源。 使用场景及目标:适用于活动套单、同单含组合与单品、多仓备货、到货周期偏移及需求波动等场景。目标是实现组合订单向基础SKU的精准拆解,按周粒度完成供需匹配与欠交递推,识别输入冲突与窗口外到货影响,支持需求增幅、到货延期等多情景模拟分析,所有结论均可从源订单、BOM、Excel公式或独立程序追溯验证。 其他说明:项目交付包含A4文档、可编辑Excel、完整Python代码、CSV输入表、计算快照、四情景结果、测试记录、README指引及作者验收记录。采用固定随机种子确保数据一致性,强调输入有效性校验与双守恒(供给与需求)原则。Excel工作簿采用三层结构(源输入—公式计算—结果展示),支持手动编辑与自动重算,但扩容需重新生成网格。独立Python程序可用于交叉验证,保障模型可靠性。 立即下载

相关推荐

东汉AD35年世界历史地图矢量文件SHP格式.zip

本资源为《东汉AD35年世界历史地图矢量文件SHP格式》压缩包,文件大小约 29.99 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见JSON 文件、属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。

USACO section 3.1 Contact(AC自动机)

Contact IOI'98 The cows have developed a new interest in scanning the universe outside their farm with radiotelescopes. Recently, they noticed a very curious microwave pulsing emission sent right f

nealgavin的专栏 9384

基于FastAPI Vue3 TypeScript的企业级全栈底座:类型共享、权限完整,三端同源 一套代码交付Web/H5/小程序,AI代码生成 RBAC Docker开箱即用一套开源的快速开发平台

基于FastAPI Vue3 TypeScript的企业级全栈底座:类型共享、权限完整,三端同源。一套完全开源的快速开发平台,提供免费使用。它结合了现代、高性能的技术栈,后端采用Fastapi + SQLAlchemy,前端采用基于 vue3 + typescript + vite + pinia + Element-Plus。

USCAO section 1.3 Calf Flac

Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes

nealgavin的专栏 6656

YOLO26算法室内办公环境二维码目标检测+训练好的模型+199张数据集+pyqt可视化界面.zip

附训练好的 YOLO 权重与 PyQt 可视化检测界面,页面底部含可视化效果预览,毕设/课设开箱即用。 【数据集概况】 · 检测类别(中文):[二维码(QRCode)] · 总计:199 张 · 测试集:20 张 · 验证集:40 张 · 训练集:139 张 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 56 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9921** mAP50-95 | 0.8216 Precision | 0.9731 Recall | 1.0000 train/box_loss | 0.6220 train/cls_loss | 0.6579 val/box_loss | 0.7042 val/cls_loss | 0.7743 【训练过程分析】 从整体上看,模型在训练过程中表现稳定,尽管在某些阶段出现了轻微的波折,但总体上遵循了预设的收敛路径。前几轮中,loss 的急剧下降让人感觉仿佛是在玩“过山车”,尤其是在训练的前 10 轮,loss 从初始值 2.0 逐渐降到 1.0 以下,这表明模型在快速学习数据的基本特征。然而,从第 15 轮开始,loss 值并没有进一步明显下降,而是趋于平稳,这表明模型已经初步收敛。 在训练曲线的中期,从第 20 轮到第 35 轮,loss 并没有出现大幅波动或平台期。这个阶段的曲线相对平滑,loss 值基本维持在 0.6 至 0.7 之间,这表明模型的泛化能力在逐渐提升。尽管如此,这个阶段确实出现了一些细微的震荡,特别是在第... 使用场景:毕业设计、课程设计、深度学习入门、工业场景落地验证。

AI第三篇:ScaBench-用真实审计竞赛数据构建可复现的 AI 审计评测集

AI第三篇:ScaBench-用真实审计竞赛数据构建可复现的 AI 审计评测集

USACO section 1.5 Checker Challenge(dfs深搜)

Checker ChallengeExamine the 6x6 checkerboard below and note that the six checkers are arranged on the board so that one and only one is placed in each row and each column, and there is never more tha

nealgavin的专栏 1924

告别色差!用Python+TCS34725颜色传感器实现精准色彩还原(附完整校准代码)

本文详细介绍了如何利用Python和TCS34725颜色传感器实现专业级色彩还原方案。通过双阶校准法(白光校准和黑电平校准)和进阶的光照自适应技术,解决传感器原始数据不准的问题,并提供完整的校准代码和实战案例,帮助开发者实现ΔE<3的高精度色彩识别。

weixin_30836759的博客 215

USACO section 2.3 Longest Prefix(dp)

Longest Prefix IOI'96 The structure of some biological objects is represented by the sequence of their constituents, where each part is denote by an uppercase letter. Biologists are interested in

nealgavin的专栏 1487

STM32F103模拟SSI时序解析:实现高精度绝对式编码器位置读取

本文详细解析了如何在STM32F103微控制器上通过软件模拟SSI协议时序,实现高精度绝对式编码器的位置读取。文章从项目驱动出发,深入剖析了SSI通信原理、硬件电平转换连接、核心软件模拟代码实现及关键时序参数调试方法,并提供了使用示波器进行波形对齐与优化的实战指南,为成本敏感型工业控制与机器人项目提供了高性价比的解决方案。

weixin_29196315的博客 149

腾讯会议录屏损坏修复:使用 recover_mp4 工具的完整解决方案

腾讯会议录屏损坏修复指南 当腾讯会议录屏因电脑蓝屏等意外导致MP4文件损坏时,可使用recover_mp4工具结合FFMPEG进行修复。操作步骤包括:1)准备相同参数正常视频作为参考;2)分析正常视频生成结构文件;3)分离损坏视频的音视频流;4)使用FFMPEG重新封装。该方法能有效修复因意外中断导致的视频索引损坏问题。修复前需将工具和文件放在同一目录下,并确保参考视频与损坏录屏的参数完全一致。

m0_62498496的博客 2649

USACO section 2.4 Cow Tours(并查集+最短路)

Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pasture

nealgavin的专栏 1406

【Kingbase人大金仓】授权文件更换

本文介绍了金仓数据库授权文件更换的步骤:1.通过ksql或客户端工具查询数据库版本;2.在官网下载对应版本的license.bat文件;3.替换前先备份原授权文件(使用tar命令压缩),再用新文件覆盖原文件。注意事项包括:版本号需严格对应,授权文件通常位于安装目录下,可使用find命令查找具体位置。操作需谨慎,建议先备份原文件以便恢复。

qq_34463511的博客 3526

USACO section3.1 Shaping Regions(漂浮法)

Shaping Regions N opaque rectangles (1 <= N <= 1000) of various colors are placed on a white sheet of paper whose size is A wide by B long. The rectangles are put with their sides parallel to the s

nealgavin的专栏 1052

软件设计师高频考点—套路题

学习交流的总结

秩沅 2552

【WRF-Chem第二期】WRF-Chem有关 namelist 详解

【WRF-Chem第二期】WRF-Chem有关 namelist 详解

WW、forever的博客 2166

USACO section 2.2 Runaround Numbers

Runaround Numbers Runaround numbers are integers with unique digits, none of which is zero (e.g., 81362) that also have an interesting property, exemplified by this demonstration: If you start

nealgavin的专栏 991

【CicadaPlayer】av_rescale_q 学习:转换PTS和Duration

参考 av_rescale_q()理解 大神时间基准的总结: AVPacket下的pts和dts以AVStream->time_base为单位(数值比较大),时间间隔就是AVStream->time_base。 大神:多种转换的总结 av_rescale_q用于计算Packet的PTS 如: if (pkt->pts != AV_NOPTS_VALUE) { pkt->pts = av_rescale_q(pkt->pts, m

突围 852

USACO section 3.1 Stamps(dp)

Stamps Given a set of N stamp values (e.g., {1 cent, 3 cents}) and an upper limit K to the number of stamps that can fit on an envelope, calculate the largest unbroken list of postages from 1 cent

nealgavin的专栏 980

XXL-Job 分布式定时任务

XXL-JOB是一款轻量级分布式定时任务调度框架,采用调度中心与执行器分离架构,解决单机任务宕机、重复执行等问题。核心模块包括调度中心(管理任务配置、触发调度)和执行器(执行任务逻辑),支持集群部署保证高可用。框架提供丰富的任务触发规则、路由策略、阻塞策略及完善的运维能力,适用于多种业务场景如数据同步、定时推送等。安装时需准备MySQL数据库并初始化表结构,通过Docker部署调度中心服务。相比其他定时框架,XXL-JOB具有轻量化、可视化等优势,是中小企业的首选方案。

郭宝的博客 727

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

USCAO section 4.1 Fence Loops(最短路,最小环,5级)

Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only a

nealgavin的专栏 941

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

USACO section 1.5 Superprime Rib(深搜)

Superprime RibButchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John

nealgavin的专栏 938

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

USACO section 2.2 Party Lamps

Party Lamps IOI 98 To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored lamps numbered from 1 to N. The lamps are connected to four buttons: Button 1: Wh

nealgavin的专栏 914

USACO section 3.2 Spinning Wheels(模拟)

Spinning Wheels 1998 ACM NE Regionals Each of five opaque spinning wheels has one or more wedges cut out of its edges. These wedges must be aligned quickly and correctly. Each wheel also has an ali

nealgavin的专栏 875

USCAO section Mother's Milk(搜索)

Mother's Milk Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty

nealgavin的专栏 848

USACO section 3.2 Feed Ratios(高斯定理)

Feed Ratios 1998 ACM Finals, Dan Adkins Farmer John feeds his cows only the finest mixture of cow food, which has three components: Barley, Oats, and Wheat. While he knows the precise mixture of th

nealgavin的专栏 803
上一篇: USACO section 2.3 Longest Prefix(dp)
下一篇: USACO section 2.3 Money Systems(dp)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址