PAT甲级1069,1070解题报告

49 篇文章 0 订阅
47 篇文章 0 订阅

1069 The Black Hole of Numbers (20 point(s))

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 -- the black hole of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we'll get:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
... ...

Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range (0,10​4​​).

Output Specification:

If all the 4 digits of N are the same, print in one line the equation N - N = 0000. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

Sample Input 1:

6767

Sample Output 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

Sample Input 2:

2222

Sample Output 2:

2222 - 2222 = 0000

题目大意:给定一个4位的字符序列,按从大到小排列得到的数-从小到大得到的数得到一个数,然后继续这样的操作,一直到等于0或者6174为止,每次输出中间的计算过程。

解题思路:用string存一下模拟一下就好了,有一个坑是他一开始输入的数并没有说是四位的。所以一开始要补全前置0变成四位。代码如下

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
bool cmp(char a, char b) {
	return a > b;
}
int getA(string s) {
	sort(s.begin(), s.end(), cmp);
	return atoi(s.c_str());
}
int getB(string s2) {
  sort(s2.begin(), s2.end());
  return atoi(s2.c_str());
}
int main() {
	string s;
	cin >> s;
	string q="";
	if (s.size() < 4) {
		for (int i = 0; i < 4 - s.size(); i++)q += "0";
	}
	s = q + s;
	int A;
	int B;
	while (true) {
		A = getA(s);
		B = getB(s);
		int res = A - B;
		printf("%04d - %04d = %04d\n", A, B, res);
		if (res == 0 || res == 6174) {
			break;
		}
		else {
			string tmp = to_string(res);
			if (tmp.size() < 4) {
				string r = "";
				for (int i = 0; i < 4 - tmp.size(); i++) {
					r += "0";
				}
				r = r + tmp;
				s = r;
			}
			else {
				s = tmp;
			}
		}
	}
	return 0;
}

1070 Mooncake (25 point(s))

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (≤1000), the number of different kinds of mooncakes, and D (≤500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:

3 200
180 150 100
7.5 7.2 4.5

Sample Output:

9.45

题目大意:一开始理解错了,索性改正了,意思是给月饼的存货和加格,然后去买到对应目标的月饼,求能够得到的最大数目。解题思路:本来以为是个dp。。后面看了一下题目,是个贪心,然后我又把它当成weight和value了,除反了,但最后还是一个样例没过,又仔细看了题目,他好像没说存货输入一定是整数,其实可以是double。

代码如下

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
struct thing{
	double weight;
	double value;
};
bool cmp(thing a, thing b) {
	return a.value / a.weight > b.value / b.weight;
}
thing cur[1005];
int main() {
	int N;
	double M;
	scanf("%d %lf", &N, &M);
	for (int i = 0; i < N; i++) {
		scanf("%lf", &cur[i].weight);
	}
	for (int i = 0; i < N; i++) {
		scanf("%lf",&cur[i].value);
	}
	sort(cur, cur + N, cmp);
	double res = 0;
	int l = 0;
	for (int i = 0; i < N; i++) {
		if (l + cur[i].weight < M) {
			l = l + cur[i].weight;
			res = res + cur[i].value;
		}
		else {
			res += ((cur[i].value*1.00 / cur[i].weight)*(M - l));
			break;
		}
	}
	printf("%.2lf\n", res);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值