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. 自定义一个可以被序列化的泛型Dictionary<TKey,TValue>集合

    Dictionary是一个键值类型的集合.它有点像数组,但Dictionary的键可以是任何类型,内部使用Hash Table存储键和值.本篇自定义一个类型安全的泛型Dictionary<TKe ...

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

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

  3. ASIHTTPRequest学习笔记

    1.creating requestsrequest分为同步和异步两种.不同之处在于开始request的函数:[request startSynchronous];[request startAsyn ...

  4. C#编程(三十八)----------运算符

    原文链接: http://blog.csdn.net/shanyongxu/article/details/46877353 运算符 类别 运算符 算术运算符 + - * / 逻辑运算符 &  ...

  5. C#编程(十四)----------构造函数

    原文链接:http://blog.csdn.net/shanyongxu/article/details/46501683 构造函数 所谓的构造函数就是和类名重名的且没有返回值的方法. class P ...

  6. spring 5.0.1.RELEASE官方任然不支持velocity(平台升级)

    官方说明: Dear Spring community, It is my pleasure to announce that Spring Framework 5.0.1 is available ...

  7. transitionFromView方法的使用

    transitionFromView方法的使用 效果 源码 // // ViewController.m // TransitionFromView // // Created by YouXianM ...

  8. Android之针对webview的缓存

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  9. Android源码中final关键字的用法及final,finally,finalize的区别

    Android开发的学习流程 final,finally,finalize的区别 Android的发展越来越快,Android开发人员越来越多,当两种情况碰撞,在诸多开发者中跟紧Android步伐脱颖 ...

  10. Java(C#)基础差异-数组

    1.填充数组 Java 数组填充替换方法Arrays.fill() 举例如下: import java.util.Arrays; public class FillDemo { public stat ...