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);
}


 

CrewAI 多 Agent 实战包 v1(Mock免Key跑通+通义千问可选+流水线样例) 本资源是 CrewAI 风格多 Agent 可运行工程包:研究→写作→审校等流水线样例。无 API Key 时自动降级 Mock 引擎跑通;有 DASHSCOPE_API_KEY 时可接真实 CrewAI + 通义千问。含中文 README、samples、smoke 留档、单元测试。本包不含密钥。关联博客:https://blog.csdn.net/mtl1994 立即下载

相关推荐

市场销售总监年终汇报必备PPT.pptx

市场销售总监年终汇报必备PPT

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

半桥LLC谐振变换器开环+闭环仿真设计资料(Simulink仿真实现)

内容概要:本文档系统性地介绍了半桥LLC谐振变换器的开环与闭环仿真设计方法,基于Simulink平台构建完整的仿真模型,涵盖系统建模、关键参数设计、控制策略实现及动态性能验证全过程。重点阐述了LLC谐振变换器的工作原理、谐振腔参数(Lr、Cr、Lm)的优化设计方法、零电压开关(ZVS)与零电流开关(ZCS)等软开关技术的实现机制,并深入探讨电压/电流双闭环控制策略,特别是恒压恒流双竞争控制逻辑的切换机制与稳定性保障。资料配套提供完整的仿真模型、详细说明文档及参考文献,有助于全面掌握其在启停过程、负载突变等工况下的动态响应特性与系统鲁棒性调控方法。; 适合人群:具备电力电子技术基础理论知识和Simulink仿真操作能力的高校研究生、科研机构研究人员,以及从事高频高效开关电源、新能源汽车车载充电机(OBC)、充电桩电源、数据中心供电等领域的工程技术人员。; 使用场景及目标:① 掌握LLC谐振变换器在实际工程中的精确建模与仿真分析流程;② 深入理解并独立实现从开环频率调制到闭环反馈控制的平滑切换逻辑;③ 应用于高效率DC-DC变换器、电动汽车充电系统、可再生能源并网电源等场景下的控制系统设计与性能优化; 阅读建议:建议结合所提供的仿真模型进行分步调试与参数扫描实验,重点关注控制器带宽设计、谐振元件参数匹配对效率的影响以及系统上电过程中的软启动策略设计,同时推荐对比学习全桥LLC、CLLC等衍生拓扑的相关资料,以建立完整的谐振变换器控制体系认知。

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

SOP 配电网故障重构方法研究【IEEE33节点】(Matlab代码实现)

内容概要:本文围绕基于SOP(Soft Open Point)的配电网故障重构方法展开研究,采用IEEE33节点标准配电系统作为仿真测试平台,利用Matlab进行代码实现与仿真验证。研究重点在于通过SOP这一柔性电力电子装置实现故障后网络拓扑的快速重构,提升供电恢复能力与系统可靠性。文中详细阐述了故障重构的数学建模过程,结合优化算法实现负荷转供与孤岛划分的协同决策,在保证辐射状运行约束的前提下,最大限度恢复失电负荷,并降低网络损耗。该方法充分体现了SOP在主动配电网中的灵活调控优势,为现代智能配电网的自愈控制提供了有效技术路径。; 适合人群:电气工程及相关专业的高校研究生、从事配电网自动化与智能电网研究的科研人员,以及具备一定Matlab编程基础和电力系统分析背景的研发技术人员。; 使用场景及目标:① 掌握基于SOP的配电网故障恢复机制与重构策略设计;② 学习如何在Matlab环境下构建IEEE33节点系统模型并实现故障重构算法;③ 深入理解含分布式电源与柔性装置的主动配电网优化运行方法; 阅读建议:建议结合电力系统分析基础知识,重点关注SOP的建模方式、故障重构的优化目标与约束条件设定,并动手实践Matlab代码以加深对算法流程和仿真结果的理解。

一份外盘期货历史高频tick数据,涵盖能源、贵金属、股指等主要品种的逐笔成交与盘口快照 每条记录包含精确至毫秒的时间戳、合约代码、成交价格、成交量以及主动买卖方向标识 同时提供该时刻的最优申买价、申卖

一份外盘期货历史高频tick数据,涵盖能源、贵金属、股指等主要品种的逐笔成交与盘口快照。每条记录包含精确至毫秒的时间戳、合约代码、成交价格、成交量以及主动买卖方向标识。同时提供该时刻的最优申买价、申卖价及对应挂单量,用于还原盘口深度变化,数据时间覆盖连续的多个交易日。 数据源:CMES金融数据库

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的专栏 1922

从BUCK-BOOST到反激:一个‘拆电感’的视角,轻松理解多路输出交叉调整

本文从BUCK-BOOST电路到反激变换器的演变入手,通过‘拆解电感’的独特视角解析多路输出交叉调整机制。深入探讨了反激电路的能量暂存方式、传递路径变化及电压关系,并结合具体参数对比揭示其特性。针对多路输出中的电压平衡问题,提供了绕组排布优化、元器件选型等实战技巧,帮助工程师有效提升电源系统的稳定性与效率。

weixin_33738578的博客 437

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的专栏 1486

IST——In-System-Test

安全性是自动驾驶平台的关键特性之一,而这些架构中使用的半导体芯片必须保证ISO 26262标准所要求的功能安全方面。为了监控由于现场缺陷导致的故障,在启动和/或关闭期间会自动运行系统内结构测试。当系统内测试(IST,In-System-Test)架构检测到任何永久性缺陷时,驾驶平台会做出响应,以实现系统的故障安全状态。汽车IC采用高质量测试方法进行筛选,以实现接近零的DPPM(每百万缺陷部件)。即使在这样的高测试标准下,由于环境或操作条件的影响,在系统现场运行过程中仍可能出现可靠性缺陷。

weixin_50518899的博客 3206

如何利用DeepSeek自动生成PLC程序?

在工业自动化领域,PLC是核心控制设备,其编程效率和质量直接影响生产线的运行效果。 然而,传统PLC编程依赖工程师手动编写代码,不仅耗时耗力,还容易出错。通过自动生成PLC程序,DeepSeek不仅大幅提升了编程效率,还优化了代码质量。 那么DeepSeek到底能否自动生成PLC程序呢? 有位电气工程师就尝试让DeepSeek设计一套五层电梯控制系统的PLC程序,下面就让我们看看吧。 技术原理:DeepSeek如何实现自动编程DeepSeek利用自然语言处理技术,将工程师输入的文本指令转换为PLC代码。例

Qinmingyue13的博客 6176

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的专栏 1405

STM32与HT7038的高精度电能计量系统设计与实现

本文详细介绍了基于STM32微控制器与HT7038三相电能计量芯片的高精度电能计量系统设计与实现。文章从项目选型出发,深入剖析了硬件电路设计、SPI驱动开发、芯片校准及系统集成等关键环节,为智能电表、能源监控等应用提供了高性价比且稳定的国产化解决方案。

pytorch8learner的博客 389

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

倍福--TwinSAFE功能块的使用

在安全模块的程序设计中,输入变量取反,或者把PLC的变量连接到安全的功能块程序中, 操作流程 1.1. 功能块输出取反 例如修改IN,右击属性,然后可在Single-Channel选项中选择是NO还是NC的输入信号。 1.2. PLC标准变量作为安全程序的输入 1.2.1. 建立两个PLC变量 1.2.2. 安全程序区创建变量 1.2.3. 功能块变量链接 首先,PLC变量不能直接连到Estop功能块的输入变量中,否则会报错,但PLC可以连接到SafeAnd这些功能块,然后再转出来,如下所示

weixin_41883890的博客 5311

【精品5万字图文详解】特征平台建设方案与最佳实践

关键词:特征工程、特征平台、特征存储、特征服务、机器学习、数据工程摘要:本文全面介绍了特征平台的建设方案与最佳实践,从特征工程的基本概念出发,深入剖析了特征平台的核心组件、架构设计和实现方法。文章通过生动的类比和实例,使读者能够轻松理解特征工程的重要性及特征平台如何解决企业在机器学习过程中面临的数据管理挑战。同时,详细探讨了主流开源特征平台的对比分析、系统设计要点、实际落地案例以及未来发展趋势,为企业构建高效特征平台提供了全面的指导和参考。想象一下,小明是一位热爱烹饪的小学生。有一天,他想做一道美味的菜肴送

AI天才研究院 952

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的专栏 989

【气候模式降尺度】分位数增量映射(QDM)原理及MATLAB代码实现

简介QDM原理及其MATLAB代码实现

WW、forever的博客 5861

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

YOLOv8性能突破:DySample超轻量动态上采样算子的创新应用与实现【附保姆级代码】

YOLOv8(You Only Look Once Version 8)作为YOLO系列模型的最新版本,延续了高效、准确的目标检测优势。其基础结构包含了骨干网络(Backbone)、检测头(Head)、以及特征融合层(Neck)。在特征融合阶段,YOLOv8需要对不同尺度的特征图进行上采样,以便将低分辨率的特征与高分辨率的特征进行融合。传统的上采样操作多使用插值(如双线性插值)或固定卷积,而这些方法可能无法很好地适应复杂的上下文信息。

一键难忘的博客 3120

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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的专栏 937

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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的专栏 913

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的专栏 847

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、付费专栏及课程。

余额充值