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

pat 1070 Mooncake

水题,按照单价贪心选择即可。注意的是题目中的月饼的存货量用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 ;
}

pat 1071 Speech Patterns                                                  本文地址

水题,遍历字符串,统计每个单词出现次数即可。代码如下:

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

pat 1072 Gas Station                                                         本文地址

题目的意思是选出一个加油站,求出加油站到几个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的更多相关文章

  1. PAT 1069 微博转发抽奖(20)(代码+思路+测试点4)

    1069 微博转发抽奖(20 分) 小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行 ...

  2. PAT——1069. 微博转发抽奖

    小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...

  3. 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 ...

  4. 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 ...

  5. PAT 乙级 1070 结绳(25) C++版

    1070. 结绳(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一段一段的绳子,你需要把它们串成一条 ...

  6. PAT 1069 微博转发抽奖

    https://pintia.cn/problem-sets/994805260223102976/problems/994805265159798784 小明 PAT 考了满分,高兴之余决定发起微博 ...

  7. 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 ...

  8. PAT 1069. 微博转发抽奖(20)

    小明PAT考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔N个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数M(<= 1000).N ...

  9. PAT Basic 1070

    1070 结绳 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原 ...

随机推荐

  1. 使用jQuery和Bootstrap实现多层、自适应模态窗口

    本篇实践一个多层模态窗口,而且是自适应的. 点击页面上的一个按钮,弹出第一层自适应模态窗口. 在第一层模态窗口内包含一个按钮,点击该按钮弹出第二层模态窗口,弹出的第二层模态窗口会挡住第一层模态窗口,即 ...

  2. Java Deadlock Example and How to analyze deadlock situation

    source:http://www.journaldev.com/1058/java-deadlock-example-and-how-to-analyze-deadlock-situation De ...

  3. Ubuntu64位下使用eclipse闪退的解决

    解决办法: 删除文件 [workspace]/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi

  4. localhost与127.0.0.1及本机ip的区别

    很多人会接触到这个ip地址127.0.0.1.也许你会问127.0.0.1是什么地址?其实127.0.0.1是一个回送地址,指本地机,一般用来测试使用.大家常用来ping 127.0.0.1来看本地i ...

  5. 都市侠盗第五季/全集Leverage迅雷下载

    第五季 Leverage Season 5 (2012)看点:TNT电视网砍掉了<都市侠盗>(Leverage),这部已经播出至第5季的团队盗窃现代罗宾汉剧集将在今年完结,这样的决定对&l ...

  6. CURLcode的定义

    经常性遇到libcurl的问题,而且都特别奇怪,记录一下CURLcode的定义: http://curl.haxx.se/libcurl/c/libcurl-errors.html   #includ ...

  7. whl文件(python)安装方法

    https://blog.csdn.net/fhl812432059/article/details/51745226 windows7 python2.7 1.用管理员方式打开cmd 2.首先通过p ...

  8. java将XML文档转换成json格式数据

    功能 将xml文档转换成json格式数据 说明 依赖包:1. jdom-2.0.2.jar : xml解析工具包;2. fastjson-1.1.36.jar : 阿里巴巴研发的高性能json工具包 ...

  9. 【转】 Qt如何设置自动补全快捷键

    原文:https://blog.csdn.net/u014597198/article/details/52797435 在用Qt编程的时,它默认是以“CTRL+空格”来作为自动补全的快捷键的,但是这 ...

  10. 附9 elasticsearch-curator + Linux定时任务

    官网教程入口:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/index.html 一.下载安装 下载:sud ...