1. 程式人生 > >PAT甲級1071,1073解題報告

PAT甲級1071,1073解題報告

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;
}