子串和
来源:互联网 发布:小米44g网络怎样设置 编辑:程序博客网 时间:2024/06/05 14:26
<div class="problem-display" style="font-size: 14px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><h2 style="margin: 0px; padding: 0px; font-size: 18px; text-align: center; color: rgb(113, 32, 21); font-family: 微软雅黑, 黑体;">子串和</h2><div class="problem-ins" style="text-align: center;">时间限制:<span class="editable highlight" id="problem[time_limit]" style="color: rgb(113, 32, 21);">5000</span> ms | 内存限制:<span class="editable highlight" id="problem[memory_limit]" style="color: rgb(113, 32, 21);">65535</span> KB</div><div class="problem-ins" style="text-align: center;">难度:<span class="editable highlight" style="color: rgb(113, 32, 21);">3</span></div></div><div class="clr" style="clear: both; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun; font-size: 12.800000190734863px; line-height: 15.199999809265137px;"></div><dl class="problem-display" style="margin: 0px; padding: 0px; color: rgb(70, 70, 70); font-family: Tahoma, Arial, sans-serif, simsun;"><dt style="font-size: 16px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-weight: bold;">描述</dt><dd style="font-size: 14px; margin: 0px; padding: 0px;"><span style="color: rgb(18, 28, 70); font-family: Verdana, 宋体;">给定一整型数列{a<sub>1</sub>,a<sub>2</sub>...,a<sub>n</sub>},找出连续非空子串{a<sub>x</sub>,a<sub>x+1</sub>,...,a<sub>y</sub>},使得该子序列的和最大,其中,1<=x<=y<=n。</span></dd><div class="clr" style="font-size: 14px; clear: both;"></div><dl class="others" style="margin: 0px; padding: 0px;"><dt style="font-size: 16px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-weight: bold;">输入</dt><dd style="font-size: 14px; margin: 0px; padding: 0px;">第一行是一个整数N(N<=10)表示测试数据的组数)每组测试数据的第一行是一个整数n表示序列中共有n个整数,随后的一行里有n个整数I(-100=<I<=100),表示数列中的所有元素。(0<n<=1000000)</dd><dt style="font-size: 16px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-weight: bold;">输出</dt><dd style="font-size: 14px; margin: 0px; padding: 0px;">对于每组测试数据输出和最大的连续子串的和。</dd><dt style="font-size: 16px; margin: 1em 0px 0.2em; padding: 0px; color: rgb(113, 32, 21); font-weight: bold;">样例输入</dt><dd style="font-size: 14px; margin: 0px; padding: 0px;"><pre id="sample_input" style="margin-top: 0px; margin-bottom: 0px; padding: 5px 10px; font-family: Consolas, 'Courier New', 'DejaVu Sans Mono', 'Droid Sans Mono', monospace; background-color: rgb(239, 239, 239); border: 1px solid rgb(204, 204, 204); min-height: 20px; line-height: 1.5em;">151 2 -1 3 -2
5
#include<iostream>using namespace std;int main(){int N,i,num,sum,max;cin>>N;while(N--){ max=-200; sum=0; int n; cin>>n; for(i=0;i<n;i++) { cin>>num; sum=sum+num;// 1 2 -1 3 -2 if(sum>max) //-5 3 3 -9 2 2 24 { max=sum; } if(sum<0) { sum=0; } } cout<<max<<endl;}return 0;}
<span style="font-size:24px;">下面start 和end 是最大字串和的起末位置。。</span>
#include<iostream>using namespace std;int main(){int N,i,num,sum,max,start,end,k;cin>>N;while(N--){ max=-200; sum=0; k=start=end=1; int n; cin>>n; for(i=0;i<n;i++) { cin>>num; sum=sum+num;// 1 2 -1 3 -2 if(sum>max) //-5 3 3 -9 2 2 24 { max=sum; start=k; end=i+1; } if(sum<0) { sum=0; k=i+2; } } cout<<max<<endl;}return 0;}
0 0
- NYOJ44 子串和
- 子串和
- 子串和
- 最大子串和
- 子串和
- 44子串和
- NYOJ 子串和
- 最大和子串
- 子串和
- 子串最大和
- 最大子串和
- 44 子串和
- 子串和
- 子串和
- NYOJ44,子串和
- 最大子串和
- 子串和
- 子串和
- 【LeetCode从零单排】No112Path Sum
- Oracle EBS 已核销AP Invoice取消不能创建会计科目
- 中断请求、分页内存和非分页内存
- cocos2dx里访问调用Android函数
- Eclipse上GIT插件EGIT使用手册
- 子串和
- iframe子页面获取父页面元素的方法
- 一张图记住常用 Linux 系统性能诊断工具
- android小功能实现之xml文件解析(Pull)
- uploadify struts2实现图片上传
- 各种浏览器UserAgent一览表(桌面+移动)
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- 应用POI,word2007转html
- 约瑟夫问题*