PAT甲级1071,1073解题报告

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

1071 Speech Patterns (25 point(s))

People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker's identity, which is useful when validating, for example, whether it's still the same person behind an online avatar.

Now given a paragraph of text sampled from someone's speech, can you find the person's most commonly used word?

Input Specification:

Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return \n. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

Output Specification:

For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

Sample Input:

Can1: "Can a can can a can?  It can!"

Sample Output:

can 5

题目大意:给一行字符串,输出其中出现最频繁的单词,如果有出现次数一样的,那就输出字典序最小的单词。小写输出。还有这里的单词不是我们理解的单词,而是连续的数字字母的组合。也就是说被不是数字字母的符号隔开。

解题思路:水题,拿个map存一下,每次模拟构造一个单词,然后map这个单词加1,最后遍历一下map就好了。

#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;
map<string, int> a;
int main() {
	string tmp;
	while (cin >> tmp) {
		string res;
		for (int i = 0; i < tmp.size(); i++) {
			
			if (tmp[i] >= 'A'&&tmp[i] <= 'Z') {
				tmp[i] += 32;
				res += tmp[i];
		    }
			else if (tmp[i] >= 'a'&&tmp[i] <= 'z') {
				res += tmp[i];
			}
			else if (tmp[i] >= '0'&&tmp[i] <= '9') {
				res += tmp[i];
			}
			else {
				if (!res.empty()) {
					a[res] += 1;
					res.clear();
				}
			}
		}
		if (!res.empty()) {
			a[res] += 1;
			res.clear();
		}
		if (getchar() == '\n')
			break;
	}
	int max=0;
	string ress;
	for (auto q = a.begin(); q != a.end(); q++) {
		if (q->second > max) {
			ress = q->first;
			max = q->second;
		}
		if (q->second == max) {
			if (q->first < ress) {
				ress = q->first;
			}
		}
	}
	cout << ress << " " << max << endl;
	return 0;
}

1073 Scientific Notation (20 point(s))

Scientific notation is the way that scientists easily handle very large numbers or very small numbers. The notation matches the regular expression [+-][1-9].[0-9]+E[+-][0-9]+ which means that the integer portion has exactly one digit, there is at least one digit in the fractional portion, and the number and its exponent's signs are always provided even when they are positive.

Now given a real number A in scientific notation, you are supposed to print A in the conventional notation while keeping all the significant figures.

Input Specification:

Each input contains one test case. For each case, there is one line containing the real number A in scientific notation. The number is no more than 9999 bytes in length and the exponent's absolute value is no more than 9999.

Output Specification:

For each test case, print in one line the input number A in the conventional notation, with all the significant figures kept, including trailing zeros.

Sample Input 1:

+1.23400E-03

Sample Output 1:

0.00123400

Sample Input 2:

-1.2E+10

Sample Output 2:

-12000000000

题目大意:科学计数法表示给你,给出实际的数字。

解题思路:用java是最简单的。。然后就因为pat对java的很不友好(加载虚拟机就需要100-200ms,当然你不用scanner也行,但这是不太可能的)。。所以还是乖乖用c系列模拟吧,就是找到E和小数点的位置,E把字符串分成两份,前面是数字部分,后面是指数部分,然后根据符号不同讨论一下,也不是很难。

#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;
int main() {
	string cur, pre, bef;
	cin >> cur;
	int indexpoint = -1;
	for (int i = 0; i < cur.size(); i++) {
		if (cur[i] == 'E') {
			pre = cur.substr(0, i);
			bef = cur.substr(i + 1, cur.size());
	}
		if (cur[i] == '.')
			indexpoint = i;
	}
	int n = atoi(bef.substr(1,bef.size()).c_str());
	string num = pre.substr(1, indexpoint - 1)+pre.substr(indexpoint+1);
	if (cur[0] == '-')cout << "-";
	if (bef[0]== '+') {
		if (n < num.size() - 1) {
			for (int i = 0; i < num.size(); i++) {
				cout << num[i];
				if (i == n) {
					cout << '.';
				}
			}
			cout << endl;
		}
		else if (n == num.size()) {
			cout << num << endl;
		}
		else {
			cout << num;
			for (int i = 0; i < n - num.size() + 1; i++) {
				cout << 0;
			}
			cout << endl;
		}
	}
	else {
		if (n == 1) {
			cout << "0." << num<< endl;
		}
		else {
			cout << "0.";
			for (int i = 0; i < n - 1; i++) {
				cout << 0;
			}
			cout << num<< endl;
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值