PAT甲级1085,1088解题报告

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

1085 Perfect Sequence (25 point(s))

Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and mare the maximum and minimum numbers in the sequence, respectively.

Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤10​5​​) is the number of integers in the sequence, and p (≤10​9​​) is the parameter. In the second line there are N positive integers, each is no greater than 10​9​​.

Output Specification:

For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.

Sample Input:

10 8
2 3 20 4 5 1 6 7 8 9

Sample Output:

8

题目大意:给一个数组和一个p,尽可能的选里面的数组成一个新数组,让新数组里面的最大的数小于等于最小的数乘以p。

解题思路:遍历数组,每次二分查找比p*最小的数大的数。

#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;
long long int a[100005];
int main() {
	int N, M;
	scanf("%d %d", &N, &M);
	for (int i = 0; i < N; i++) {
		scanf("%lld", &a[i]);
	}
	sort(a, a + N);
	int maxc = 0;
	//bool flag = false;
	for (int i = 0; i < N; i++) {
		int pos= upper_bound(a, a + N, a[i] * M) - a;
		if ((pos - i) > maxc)maxc = pos - i;
	}
	printf("%d\n", maxc);
	return 0;
}

1088 Rational Arithmetic (20 point(s))

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.

Input Specification:

Each input file contains one test case, which gives in one line the two rational numbers in the format a1/b1 a2/b2. The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in front of the numerator. The denominators are guaranteed to be non-zero numbers.

Output Specification:

For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each line is number1 operator number2 = result. Notice that all the rational numbers must be in their simplest form k a/b, where k is the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the denominator in the division is zero, output Inf as the result. It is guaranteed that all the output integers are in the range of long int.

Sample Input 1:

2/3 -4/2

Sample Output 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

Sample Input 2:

5/3 0/6

Sample Output 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf

题目大意:给两个分数,输出加减乘除运算。

解题思路:简单模拟。。但是有点烦,我直接网上找了代码算了。虽然变量名取的很那啥。。但是意思表达的很清楚。

​


​#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;
long int gcd(long int beichushu, long int chushu)
{
	return chushu == 0 ? beichushu : gcd(chushu, beichushu%chushu);
}
void GaiBianXingShi(long int &a, long int &b)
{
	int G = gcd(b, a);
	a /= G;
	b /= G;
}
void shuchuyige(long int a, long int b)
{
	if (b == 0)cout << "Inf";
	else
	{
		if (b < 0) { a = -a; b = -b; }
		bool parentheses = false;
		if (a < 0)parentheses = true;
		if (parentheses)cout << "(";
		if (a%b == 0)cout << a / b;
		else
		{
			if (a / b != 0)
			{
				cout << a / b << " ";
				if (a%b < 0)a = -a%b;
			}
			cout << a%b << "/" << b;
		}
		if (parentheses)cout << ")";
	}
}
void JiaFa(long int*a, long int *b, long int G)
{
	b[2] = b[1] * b[0] / G;
	a[2] = a[0] * b[1] / G + a[1] * b[0] / G;
	G = gcd(b[2], a[2]);
	a[2] /= G;
	b[2] /= G;
	shuchuyige(a[0], b[0]);
	cout << " + ";
	shuchuyige(a[1], b[1]);
	cout << " = ";
	shuchuyige(a[2], b[2]);
	cout << endl;
}
void JianFa(long int*a, long int *b, long int G)
{
	b[2] = b[1] * b[0] / G;
	a[2] = a[0] * b[1] / G - a[1] * b[0] / G;
	G = gcd(b[2], a[2]);
	a[2] /= G;
	b[2] /= G;
	shuchuyige(a[0], b[0]);
	cout << " - ";
	shuchuyige(a[1], b[1]);
	cout << " = ";
	shuchuyige(a[2], b[2]);
	cout << endl;
}
void ChengFa(long int*a, long int *b, long int G)
{
	b[2] = b[1] * b[0];
	a[2] = a[0] * a[1];
	G = gcd(b[2], a[2]);
	a[2] /= G;
	b[2] /= G;
	shuchuyige(a[0], b[0]);
	cout << " * ";
	shuchuyige(a[1], b[1]);
	cout << " = ";
	shuchuyige(a[2], b[2]);
	cout << endl;
}
void ChuFa(long int*a, long int *b, long int G)
{
	b[2] = b[0] * a[1];
	a[2] = b[1] * a[0];
	G = gcd(b[2], a[2]);
	a[2] /= G;
	b[2] /= G;
	shuchuyige(a[0], b[0]);
	cout << " / ";
	shuchuyige(a[1], b[1]);
	cout << " = ";
	shuchuyige(a[2], b[2]);
	cout << endl;
}
int main()
{
	long int a[3], b[3], Gmax;
	char ctemp;
	cin >> a[0] >> ctemp >> b[0] >> a[1] >> ctemp >> b[1];
	if (0 == b[0] || 0 == b[1])cout << "Inf" << endl;
	else
	{
		GaiBianXingShi(a[0], b[0]);
		GaiBianXingShi(a[1], b[1]);
		Gmax = gcd(b[0], b[1]);
		JiaFa(a, b, Gmax);
		JianFa(a, b, Gmax);
		ChengFa(a, b, Gmax);
		ChuFa(a, b, Gmax);
	}
	system("pause");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值