uva11059
除法(Division,uva725)
输入整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,其中a~j恰好为数字0~9的一个排列(可以有前导0),2<=n<=79。
输入:输入若干组数据,以文件结束符结束。
输出:For each test case you must print the message: Case #M: The maximum product is P., where M is the number of the test case, starting from 1, and P is the value of the maximum product. After each test case you must print a blank line.
样例输入:
62
样例输出:
79546/01283=62
94736/01528=62
方法1:利用3重循环枚举起点和终点。
错误程序:
#include<iostream>
#include<cstdio>
using namespace std;
int a[30];
int main(){
int n,m=0;
while (cin>>n){
m++;
long long maxc=0,s;
for(int i=1;i<=n;i++) cin>>a[i];
for (int i=1;i<=n;i++){
for (int j=i+1;j<=n;j++){//程序错误:考虑不全,当单个数为最大值时,(更没有考虑到,当第n个数为最大值的情况),不能出正确结果
s=a[i];
for (int k=i+1;k<=j;k++)s=s*a[k];
if (s>maxc) maxc=s;
}
}
cout<<"Case #"<<m<<": The maximum product is "<<maxc<<"."<<endl<<endl;
}
return 0;
}
修改1:
for (int i=1;i<=n;i++){
for (int j=i;j<=n;j++){
s=a[i];
if (s>maxc) maxc=s;
for (int k=i+1;k<=j;k++)s=s*a[k];
if (s>maxc) maxc=s;
}
}
继续修改:
for (int i=1;i<=n;i++){
for (int j=i;j<=n;j++){
s=1;
for (int k=i;k<=j;k++)s=s*a[k];
if (s>maxc) maxc=s;
}
}
简化:由三重循环修改为二重
for (int i=1;i<=n;i++){
s=1;
for (int j=i;j<=n;j++){
s=s*a[j];//累乘即可
if (s>maxc) maxc=s;
}
}
继续简化:能不能用一维实现
思路,动态规划,模拟加法的最长连续子序列 f[i]=max(f[i-1],a[i]),则有:
因为本题是乘法,需要如果只单纯记录最大值是不妥当的,因为负数的最小值*负数结果也可能最大。所以有:
f[i]=max(max(f[i-1]*a[i],g[i-1]*a[i]),a[i]);
g[i]=min(min(f[i-1]*a[i],g[i-1]*a[i]),a[i]);
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long f[30],g[30],a[30];
int main(){
int n,m=0;
while (cin>>n){
m++;
long long maxc=0,s;
for(int i=1;i<=n;i++) cin>>a[i];
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
for (int i=1;i<=n;i++){
f[i]=max(max(f[i-1]*a[i],g[i-1]*a[i]),a[i]);
g[i]=min(min(f[i-1]*a[i],g[i-1]*a[i]),a[i]);
}
for(int i=1;i<=n;i++)if (maxc<f[i]) maxc=f[i];
cout<<"Case #"<<m<<": The maximum product is "<<maxc<<"."<<endl<<endl;
}
return 0;
}
符:算法竞赛入门经典例题7—2
uva11059的更多相关文章
- uva11059(最大乘积)
Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...
- UVA11059 - Maximum Product
1.题目名称 Maximum Product 2.题目地址 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- 7_2 最大乘积(UVa11059)<枚举连续子序列>
给一个数字集合{ S1,S2,…,Sn },请从这个数字集合里找出一段连续数字,使他们的乘积是最大的.以Case 1为例子,2 x 4 = 8为这个集合的最大乘积:而Case 2则为2 x 5 x(– ...
- 枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)
//Uva725 #include <iostream> #include <cstring> #include <cstdlib> #include <cs ...
随机推荐
- php面向对象Object
1.创建类 class 类名{ private 私有变量 只能本类的内部使用 protected 受保护的变量 本类和子类的内部 public 公开的变量 都可以使用 一般属性都设为私有 一般函数都是 ...
- JS控制css float属性的用法经验总结
JavaScript与CSS属性的控制网上很常见,因此来说用js操作css属性是有一定规律的. 1.对于没有中划线的css属性一般直接使用style.属性名即可. 如:obj.style.margin ...
- 《javascript高级程序设计》 第23章 离线应用与客户端存储
23.1 离线检测23.2 应用缓存23.3 数据存储 23.3.1 Cookie 23.3.2 IE 用户数据 23.3.3 Web 存储机制 23.3.4 IndexedDB 23.1 离线检 ...
- hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 张艾迪(创始人):拥抱单身与自由的Eidyzhang
拥抱单身与自由(Single+Freedom) 拥抱伟大的梦想与理想.年轻一代的张扬与自信 拥抱AOOOiA.Global.224C的一切是我对这个世界的态度 +AOOOiA.Global.224C创 ...
- cms3.0——收获(1)
或许是由于各个公司的情况不同,使得每次写后台管理系统就沿用之前的nodejs中的thinkjs来写后台管理系统,也是因为这样后期维护起来更加方便吧?不过最早之前的项目,却有一个使用的是nodejs 中 ...
- go——beego的数据库增删改查
一直都不理解使用go语言的时候,为什么还要自己去装beego,以为使用go便可以解决所有的问题,结果在朋友的点拨下,才意识到: go与beego的关系就好比是nodejs与thinkjs的关系,因此也 ...
- 关于Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的问题
找不到mysql socket的问题,我最近碰到了好多次重装系统以前,我的mysql,apache,php都是自己编译安装的,当时并没有碰到这些问题,重装系统以后,我的mysql是通过yum安装的,a ...
- sqlserver 2008 左补齐字符串
SQLServer:right函数 语法 Right(string, length) Right 函数的语法具有下面的命名参数: 部分 说明 string 必要参数.字符串表达式,从中最右边的 ...
- SQL语句的用法
1.增加字段 alter table docdsp add dspcodechar(200)2.删除字段 ALTER TABLE table_NAME DROP COLUMNc ...