PAT (Basic) 1001~1005

1001. 害死人不偿命的(3n+1)猜想 (15)

#include <iostream>

using namespace std;

int main()
{
	int n;
	cin >> n;
	int tot = 0;
	while (n != 1)
	{
		if (n % 2 == 0)
			n /= 2;
		else
			n = (3 * n + 1) / 2;
		tot++;
	}
	cout << tot << endl;

	return 0;
}


1002. 写出这个数 (20)

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	string str;
	cin >> str;
	int tot = 0;
	for (int i = 0; i < str.length(); i++)
		tot += str[i] - '0';
	vector<string> vs;
	while (tot)
	{
		switch (tot % 10)
		{
		case 0:
			vs.push_back("ling");
			break;
		case 1:
			vs.push_back("yi");
			break;
		case 2:
			vs.push_back("er");
			break;
		case 3:
			vs.push_back("san");
			break;
		case 4:
			vs.push_back("si");
			break;
		case 5:
			vs.push_back("wu");
			break;
		case 6:
			vs.push_back("liu");
			break;
		case 7:
			vs.push_back("qi");
			break;
		case 8:
			vs.push_back("ba");
			break;
		case 9:
			vs.push_back("jiu");
			break;
		}
		tot /= 10;
	}
	vector<string> ::reverse_iterator it = vs.rbegin();
	cout << *it;
	++it;
	for (; it != vs.rend(); ++it)
		cout << " " << *it;
	cout << endl;

	return 0;
}


1003. 我要通过!(20)

说明:
1、把可以通过字符串以aPbTc表示,a,c分别表示A*(A的闭包),其中b表示A+(A的飞空闭包),则a,b,c中A的个数满足关系len(a) * len(b) = len(c)
(参考:http://blog.csdn.net/liygcheng/article/details/10590585);
2、string::npos,表示没有找到,no position,值跟平台有关,一般等于-1;
3、之前浙大2010年考研复试机试题题ZOJ问题,跟这个如出一辙。
#include <iostream>
#include <string>

using namespace std;

bool judge(string s)
{
	int p = s.find_first_not_of('A');
	if ((p == string::npos) || (s[p] != 'P'))
		return false;

	int t = s.find_first_not_of('A', p + 1);
	if ((t == string::npos) || (t == p + 1) || (s[t] != 'T'))
		return false;

	int n = s.find_first_not_of('A', t + 1);
	if (n != string::npos)
		return false;

	if ((s.length() - t - 1) == p * (t - p - 1))
		return true;
	else
		return false;

}

int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		string s;
		cin >> s;
		if (judge(s))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}

	return 0;
}


1004. 成绩排名 (20)

#include <iostream>
#include <string>

using namespace std;

int main()
{
	int n;
	cin >> n;
	string max_name, max_id, min_name, min_id, name, id;
	int max_grade, min_grade, grade;

	cin >> name >> id >> grade;
	max_name = min_name = name;
	max_id = min_id = id;
	max_grade = min_grade = grade;
	for (int i = 0; i < n - 1; i++)
	{
		cin >> name >> id >> grade;
		if (grade > max_grade)
		{
			max_name = name;
			max_id = id;
			max_grade = grade;
		}
		if (grade < min_grade)
		{
			min_name = name;
			min_id = id;
			max_grade = grade;
		}
	}
	cout << max_name << " " << max_id << endl;
	cout << min_name << " " << min_id << endl;

	return 0;
}


1005. 继续(3n+1)猜想 (25)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;

vector<bool> vec;

int main()
{
//	freopen("in.txt", "r", stdin);
	int K;
	cin >> K;
	vec.resize(101);
	int n;
	for (int i = 0; i < K; i++)
	{
		cin >> n;
		vec[n] = true;
	}
	for (int i = 2; i < 101; i++)
	{
		if (vec[i])
		{
			int tmp = i;
			while (tmp != 1)
			{
				if (tmp % 2 == 0)
					tmp /= 2;
				else
					tmp = (tmp * 3 + 1) / 2;
				vec[tmp] = false;
			}
		}
	}
	int first = true;
	for (int i = 100; i >= 2; i--)
	{
		if (first && vec[i])
		{
			cout << i;
			first = false;
			continue;
		}
		if (vec[i])
		{
			cout << " " << i;
		}
	}
	cout << endl;

	return 0;
}



你可能感兴趣的:(PAT (Basic) 1001~1005)