PAT 1069 1070 1071 1072
pat 1069 The Black Hole of Numbers
水题,代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std; bool isSame(char buf[])
{
int i = ;
while(buf[i] != '\0')
{
if(buf[i] != buf[])return false;
else i++;
}
return true;
} int main()
{
int num;
scanf("%d", &num);
char buf[];
sprintf(buf, "%04d", num);
if(isSame(buf))
{
printf("%04d - %04d = 0000", num, num);
return ;
}
int len = strlen(buf);
do
{
sort(buf, buf+len, greater<char>());
int decre = atoi(buf);
sort(buf, buf+len);
int incre = atoi(buf);
num = decre - incre;
sprintf(buf, "%04d", num);
printf("%04d - %04d = %04d\n", decre, incre, num);
}while(num != );
return ;
}
水题,按照单价贪心选择即可。注意的是题目中的月饼的存货量用double类型,用int第三个数据通不过。代码如下:
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std; struct UnitPrice
{
double val;
int index;
}; bool comp(UnitPrice a, UnitPrice b)
{
return a.val > b.val;
} int main()
{
//freopen("input.txt","r",stdin);
int N,D;
scanf("%d%d", &N, &D);
double *amount = new double[N];
double *price = new double[N];
UnitPrice *unitPrice = new UnitPrice[N];
for(int i = ; i < N; i++)
scanf("%lf", &amount[i]);
for(int i = ; i < N; i++)
{
scanf("%lf", &price[i]);
unitPrice[i].val = price[i] / amount[i];
unitPrice[i].index = i;
}
sort(&unitPrice[], &unitPrice[N], comp);
double profit = 0.0;
int j = ;
while(D > && j < N)
{
int k = unitPrice[j++].index;
if(D >= amount[k])
{
profit += price[k];
D -= amount[k];
}
else
{
profit += (D*1.0/amount[k])*price[k];
D = ;
}
}
printf("%.2f\n" ,profit);
return ;
}
水题,遍历字符串,统计每个单词出现次数即可。代码如下:
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std; bool isValid(char c)
{
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '' && c <= ''))
return true;
else return false;
} int main()
{
string buf, maxWord;
getline(cin, buf);
transform(buf.begin(),buf.end(),buf.begin(), ::tolower);
map<string,int> words;
int i = , times = ;
while( i < buf.length() )
{
while( i < buf.length() && isValid(buf[i]) == false) i++;
if(i < buf.length())
{
int start = i++;
while( i < buf.length() && isValid(buf[i]) == true) i++;
if( i <= buf.length())// 这里注意要<=,不能是<
{
string word = buf.substr(start, i-start);
if(words.find(word) == words.end())
{
words[word] = ;
if(times < ){times = ; maxWord = word;}
}
else
{
words[word] ++;
if(times < words[word] ||
(times == words[word] && word < maxWord))
{
times = words[word];
maxWord = word;
}
}
}
}
}
cout<<maxWord<<" "<<times;
return ;
}
题目的意思是选出一个加油站,求出加油站到几个house的距离的最小值,从这些最小值中选出一个最小的记为k(即距离station最近的的house),选择的加油站要使k最大。理解题目意思后就很简单了,只要依次以每个候选加油站为起点计算单源最短路径,然后计算k,选择k最大的加油站即可。代码如下:
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<climits>
using namespace std;
const int INF = INT_MAX; int index(char buf[], int houseNum)
{
if(buf[] == 'G')
return atoi(buf+) + houseNum - ;
else
return atoi(buf) - ;
} //以src为起点的单源最短路径
void Dijkstra(int src, int dist[], int **graph, int n)
{
bool *used = new bool[n];
for(int i = ; i < n; i++)
{dist[i] = graph[src][i]; used[i] = false;}
for(int i = ; i < n; i++)
{
int tmin = INF,k;
for(int j = ; j < n; j++)
if(!used[j] && tmin > dist[j])
{
tmin = dist[j];
k = j;
}
used[k] = true;
for(int j = ; j < n; j++)
if(dist[k] != INF && graph[k][j] != INF &&
dist[k] + graph[k][j] < dist[j])
{
dist[j] = dist[k] + graph[k][j];
}
}
delete used;
}
int main()
{
//freopen("input.txt", "r", stdin);
int houseNum, stationNum,roadNum,range;
scanf("%d%d%d%d", &houseNum, &stationNum, &roadNum, &range);
const int nodeNum = houseNum + stationNum;
int **graph = new int*[nodeNum];
for(int i = ; i < nodeNum; i++)
{
graph[i] = new int[nodeNum];
for(int j = ; j < nodeNum; j++)
graph[i][j] = (i != j ? INF:);
}
for(int i = ; i < roadNum; i++)
{
char buf[];
scanf("%s", buf); int a = index(buf, houseNum);
scanf("%s", buf); int b = index(buf, houseNum);
scanf("%d", &graph[a][b]);
graph[b][a] = graph[a][b];
} double minDistance = -1.0, averageDistance = INF;
int Selectstation = -;
int dist[nodeNum];
for(int i = houseNum; i < nodeNum; i++)
{
Dijkstra(i, dist, graph, nodeNum);
double mindis = INF, averdis = 0.0;
bool flag = true;
for(int j = ; j < houseNum; j++)
{
if(dist[j] > range){flag = false; break;}
averdis += dist[j];
if(dist[j] < mindis)mindis = dist[j];
}
if(flag == false)continue;
averdis /= houseNum;
if(mindis > minDistance)
{
minDistance = mindis;
averageDistance = averdis;
Selectstation = i;
}
else if(mindis == minDistance)
{
if(averdis < averageDistance)
{
averageDistance = averdis;
Selectstation = i;
}
}
}
if(Selectstation != -)
printf("G%d\n%.1f %.1f\n", Selectstation+-houseNum, minDistance,
averageDistance);
else printf("No Solution\n");
return ;
}
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3405252.html
PAT 1069 1070 1071 1072的更多相关文章
- PAT 1069 微博转发抽奖(20)(代码+思路+测试点4)
1069 微博转发抽奖(20 分) 小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行 ...
- PAT——1069. 微博转发抽奖
小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...
- PAT 1069. The Black Hole of Numbers (20)
For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in ...
- PAT 1069 The Black Hole of Numbers
1069 The Black Hole of Numbers (20 分) For any 4-digit integer except the ones with all the digits ...
- PAT 乙级 1070 结绳(25) C++版
1070. 结绳(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一段一段的绳子,你需要把它们串成一条 ...
- PAT 1069 微博转发抽奖
https://pintia.cn/problem-sets/994805260223102976/problems/994805265159798784 小明 PAT 考了满分,高兴之余决定发起微博 ...
- PAT 1069 The Black Hole of Numbers[简单]
1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...
- PAT 1069. 微博转发抽奖(20)
小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...
- PAT Basic 1070
1070 结绳 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原 ...
随机推荐
- 报错:System.NotSupportedException: LINQ to Entities does not recognize the method
报错:System.NotSupportedException: LINQ to Entities does not recognize the method ...... get_Item(Int3 ...
- python测试开发django-19.admin后台自定义显示
前言 django的admin后台默认显示的内容很少,只显示了表的相关信息,查看字段内容需点开详情才能查看,不是很直观. 在admin.py文件里面是可以自定义相关的展示内容的,也可以添加搜索框,快速 ...
- Cannot subclass final class class com.sun.proxy.$Proxy
背景 这个错误是我在使用AOP动态切换数据库,实现数据库的读写分离的时候出现的问题,使用到的系统环境是: <spring.version>3.2.6.RELEASE</spring. ...
- JAVA8 List排序
先定义一个实体类 @Data @AllArgsConstructor @NoArgsConstructor public class Human { private String name; priv ...
- spring4 quartz2 集群动态任务
实现定时任务的执行,而且要求定时周期是不固定的.测试地址:http://sms.reyo.cn 生产环境:nginx+tomcat+quartz2.2.1+spring4.2.1 集群. 实现功能:可 ...
- java List集合记录 ArrayList和LinkedList的区别
一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问 ...
- SharePoint 2013 workflows stop working (Failed on started.)
前言 最近,使用工作流的时候碰到了一个问题,突然间所有工作流都无法启动,报错Failed on started. 同时,工作流内部报错,工作流被系统账号取消了. 查了很久,发现系统打了windows ...
- 傲骨贤妻第一季/全集The Good Wife迅雷下载
第一季 The Good Wife Season 1 (2009)看点:在经受丈夫Peter的背叛以及因此而带来的公众羞辱后,Alicia Florrick选择重新继续自己原来的事业,一名辩护律师,以 ...
- IOS应用提交所需的ICON
如果提交的ipa包中,未包含必要的Icon就会收到类似的通知,为什么偏偏是Icon-76呢? 因为我们开发的游戏,默认是支持iphone以及ipad的,根据官方提供的参考 Icon-76.png是必须 ...
- [Web 前端] CSS篇之2. 清除浮动,什么时候需要清除浮动,清除浮动都有哪些方法
cp: https://blog.csdn.net/zengyonglan/article/details/53304487 2. 清除浮动,什么时候需要清除浮动,清除浮动都有哪些方法 ? 一.什么时 ...