pat甲级1077,1081题解

本文解析了两道算法竞赛题目,包括寻找最长公共后缀和计算分数之和的问题,提供了详细的解题思路和C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1077 Kuchiguse (20 point(s))

The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often exaggerated artistically in Anime and Manga. For example, the artificial sentence ending particle "nyan~" is often used as a stereotype for characters with a cat-like personality:

  • Itai nyan~ (It hurts, nyan~)

  • Ninjin wa iyada nyan~ (I hate carrots, nyan~)

Now given a few lines spoken by the same character, can you find her Kuchiguse?

Input Specification:

Each input file contains one test case. For each case, the first line is an integer N (2≤N≤100). Following are N file lines of 0~256 (inclusive) characters in length, each representing a character's spoken line. The spoken lines are case sensitive.

Output Specification:

For each test case, print in one line the kuchiguse of the character, i.e., the longest common suffix of all N lines. If there is no such suffix, write nai.

Sample Input 1:

3
Itai nyan~
Ninjin wa iyadanyan~
uhhh nyan~

Sample Output 1:

nyan~

Sample Input 2:

3
Itai!
Ninjinnwaiyada T_T
T_T

Sample Output 2:

nai

题目大意:输出最长后缀码。

解题思路:反转一下判断最长前缀码,得出结果再反转一下就是结果了。用getline的话注意用getchar吃一下回车。

#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;
string a[105];
int main() {
	int N;
	cin >> N;
	int minl = 99999;
	getchar();
	for (int i = 0; i < N; i++) {
		getline(cin, a[i]);
		int len = a[i].size();
		minl = min(len, minl);
		reverse(a[i].begin(), a[i].end());
	}
	int index = 0;
	for (int i = 0; i < minl; i++) {
		char c = a[0][i];
		bool flag = true;
		for (int j = 1; j < N; j++) {
			if (a[j][i] != c) {
				flag = false;
				break;
			}
		}
		if (flag == false) {
			break;
		}
		else {
			index++;
		}
	}
	if (index == 0)cout << "nai" << endl;
	else {
		string res = a[0].substr(0, index);
		reverse(res.begin(), res.end());
		cout << res << endl;
	}
	return 0;
}

1081 Rational Sum (20 point(s))

Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum.

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (≤100), followed in the next line N rational numbers a1/b1 a2/b2 ... where all the numerators and denominators are in the range of long int. If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:

For each test case, output the sum in the simplest form integer numerator/denominator where integer is the integer part of the sum, numerator <denominator, and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1:

5
2/5 4/15 1/30 -2/60 8/3

Sample Output 1:

3 1/3

Sample Input 2:

2
4/3 2/3

Sample Output 2:

2

Sample Input 3:

3
1/3 -1/6 1/8

Sample Output 3:

7/24

题目大意:给若干个分数,按标准格式输出这些分数的和。

解题思路:模拟一下分数就行了,注意输出的格式讨论即可

#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 ff{
	long long int num;
	long long int den;
};
ff a[105];
long long int gcd(long long int a, long long int b) {
	while (b != 0) {
		long long int t = b;
		b = a%b;
		a = t;
	}
	return a;
}
ff sum(ff a, ff b) {
	ff res;
	res.den = a.den*b.den;
	res.num = a.den*b.num + a.num*b.den;
	long long int g = gcd(res.num, res.den);
	res.den /= g;
	res.num /= g;
	return res;
}
int main() {
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		scanf("%lld/%lld", &a[i].num, &a[i].den);
	}
	ff ress = a[0];
	
	//cout << ress.den << " " << ress.num << endl;
	for (int i = 1; i < N; i++) {
		ress = sum(ress,a[i]);
		//cout << ress.den << " " << ress.num << endl;
	}
	if (ress.num / ress.den >= 1) {
		if (ress.num%ress.den == 0) {
			printf("%lld\n",ress.num/ress.den);
		}
		else {
			printf("%lld %lld/%lld\n",ress.num/ress.den,ress.num%ress.den,ress.den);
		}
	}
	else {
		if (ress.num == 0)printf("0\n");
		else {
			printf("%lld/%lld\n", ress.num, ress.den);
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值