1.21 贪心入门上午PAT例题题解
1.B1023
#include<cstdio> int a[10]; int main()
{
for(int i=0;i<=9;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=9;i++)
{
if(a[i])
{
printf("%d",i);
a[i]--;
break;
}
}
for(int i=0;i<=9;i++)
{
while(a[i]--)
{
printf("%d",i);
}
}
}
2.B1020
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxv=1011; struct node
{
double price,sum,rate;
}cake[maxv]; bool cmp(node a,node b)
{
return a.rate>b.rate;
} int n;
double d; int main()
{
scanf("%d %lf",&n,&d);
for(int i=1;i<=n;i++)
{
scanf("%lf",&cake[i].sum);
}
for(int i=1;i<=n;i++)
{
scanf("%lf",&cake[i].price);
cake[i].rate=cake[i].price/cake[i].sum;
}
sort(cake+1,cake+1+n,cmp);
int cnt=1;
double ans=0;
while(d>0&&cnt<=n)
{
if(d<=cake[cnt].sum)
{
ans+=(d*cake[cnt].rate);
d=0;
}
else
{
ans+=cake[cnt].price;
d-=cake[cnt].sum;
}
cnt++;
}
printf("%.2lf\n",ans);
return 0;
}
3.A1037
#include<cstdio>
#include<algorithm>
using namespace std; const int maxv=1e5+5; int n,m;
int a[maxv],b[maxv]; int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d",&b[i]);
sort(a+1,a+1+n);
sort(b+1,b+1+m);
int ans=0;
int p=1;
while(p<=n&&a[p]<0&&b[p]<0)
{
ans+=a[p]*b[p];
p++;
}
int p1=n;
int p2=m;
while(p1>=1&&p2>=1&&a[p1]>0&&b[p2]>0)
{
ans+=a[p1]*b[p2];
p1--;
p2--;
}
printf("%d\n",ans);
}
4.A1038
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std; const int maxv=1e4+4;
string s[maxv];
bool cmp(string a,string b)
{
return a+b<b+a;
} int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>s[i];
sort(s+1,s+1+n,cmp);
string ans="";
for(int i=1;i<=n;i++)
ans+=s[i];
while(ans.length()>0&&ans[0]=='0')
ans.erase(ans.begin());
if(ans.length()==0)
cout<<0<<endl;
else
cout<<ans<<endl;
return 0;
}
1.21 贪心入门上午PAT例题题解的更多相关文章
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- POJ3614 Sunscreen 贪心入门
题目大意 给出一些区间和一些点,一个点如果在一个区间内,那么此两者可以匹配.问匹配数最大是多少. 题解 这样的题我们一般都是站在区间上去找与其配对的点.我们可以得到如下性质: 对于一段区间\([l_1 ...
- PAT 1031-1040 题解
早期部分代码用 Java 实现.由于 PAT 虽然支持各种语言,但只有 C/C++标程来限定时间,许多题目用 Java 读入数据就已经超时,后来转投 C/C++.浏览全部代码:请戳 本文谨代表个人思路 ...
- pat甲级题解(更新到1013)
1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...
- hdu2037 经典贪心入门
今年暑假不AC Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 浙大pat 1035题解
1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...
- 浙大pat 1025题解
1025. PAT Ranking (25) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...
- 浙大 pat 1007题解
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 浙江大学Pat 1036 题解
1036. Boys vs Girls (25) This time you are asked to tell the difference between the lowest grade of ...
随机推荐
- Ubuntu 12.04常用快捷键
===== 桌面 ===== ALT + F1: 聚焦到桌面左侧任务导航栏,可按上下键导航. ALT + F2: 运行命令 ALT + F4: 关闭窗口 ALT + TAB: 切换程序窗口 ALT + ...
- chrome开发者工具那点事
Elements:查找网页源代码HTML中的任一元素,手动修改任一元素的属性和样式且能实时在浏览器里面得到反馈. Console:记录开发者开发过程中的日志信息,且可以作为与JS进行交互的命令行She ...
- CSS3的新增边框属性
一.CSS3 新增的边框属性 属性 版本 简介 border-image CSS3 设置或检索对象的边框使用图像来填充 border-image-source CSS3 设置或检索对象的边框是否用图像 ...
- linux查看主板型号及内存硬件信息,及硬盘测速
查看主板型号: sudo dmidecode |grep -A16 "System Information$" 内存槽及内存条: sudo dmidecode |grep -A ...
- 使用Yarn+Webpack+Babel6搭建React.js环境
使用Yarn+Webpack+Babel6搭建React.js环境 Facebook开源的React.js已经改变了世人对前端UI的思考方式.这种基于组件方式的优势之一,就是使View更加的简单,因为 ...
- Ansible--01
一.ansible是什么: 类似puppet之类的运维自动化工具 二.为什么选择ansible: 1. ansible是python语言开发的,python语言进入门槛低,方便基于pytnon对ans ...
- Hadoop学习之路(四)Hadoop集群搭建和简单应用
概念了解 主从结构:在一个集群中,会有部分节点充当主服务器的角色,其他服务器都是从服务器的角色,当前这种架构模式叫做主从结构. 主从结构分类: 1.一主多从 2.多主多从 Hadoop中的HDFS和Y ...
- spring boot集成dubbo
spring-boot-start-dubbo spring-boot-start-dubbo,让你可以使用spring-boot的方式开发dubbo程序.使dubbo开发变得如此简单. 如何使用 1 ...
- Kafka设计解析(四)Kafka Consumer设计解析
转载自 技术世界,原文链接 Kafka设计解析(四)- Kafka Consumer设计解析 目录 一.High Level Consumer 1. Consumer Group 2. High Le ...
- linux iSCSI target/initiator配置
linux iSCSI target配置全过程一:Install iSCSI target for Linux1,操作系统:[root@rac2 ~]# cat /etc/issueEnterpris ...