PAT甲级1001,1002,1005,1006解题报告

1001.

1001 A+B Format (20 分)

Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits).

Input Specification:

Each input file contains one test case. Each case contains a pair of integers a and b where −10​6​​≤a,b≤10​6​​. The numbers are separated by a space.

Output Specification:

For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.

Sample Input:

-1000000 9

Sample Output:

 

-999,991

题目大意:输入两个数,输出两个数的和。本来是挺简单的。。但是要求要标准的格式化输出,就是银行账单一样每三位数字夹一个逗号。

解题思路:简单的把字符串处理一下就好了。先算出位数对3的余数,然后输出那几位加个逗号,剩下的除了最后一组全部输出多一个逗号即可。实现代码如下。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	long long int a, b;
	cin >> a >> b;
	long long int c = a + b;
	string res = to_string(c);
	int len;
	if (c >= 0) {
		len = res.length();
	}
	else {
		len = res.length() - 1;
	}
	if (len < 4) {
		cout << res << endl;
	}
	else {
		if (len % 3 == 0) {
			if (c > 0) {
				for (int i = 0; i < len / 3; i++) {
					if (i != len / 3 - 1)
						cout << res.substr(i * 3, 3) + ",";
					else
						cout << res.substr(i * 3, 3) << endl;
				}
			}
			else {
				cout << "-";
				for (int i = 0; i < len / 3; i++) {
					if (i!=len/3-1)
					{
						cout << res.substr(i * 3+1, 3) + ",";
					}
					else
						cout << res.substr(i * 3+1, 3) << endl;
				}
			}
		}
		else {
			if (c > 0) {
				cout << res.substr(0, len % 3)+",";
				res = res.substr(len % 3 , len - len%3);
				for (int i = 0; i < len/3; i++)
				{
					if(i!=len/3-1)
					cout << res.substr(i*3, 3)+",";
					else
					{
						cout << res.substr(i * 3, 3) << endl;
					}
				}
			}
			else {
				cout << "-" + res.substr(1,len%3)+",";
				res = res.substr(1 + len % 3 , len - len % 3);
				for (int i = 0; i < len / 3; i++) {
					if (i != len / 3 - 1) {
						cout << res.substr(i * 3, 3)+",";
					}
					else {
						cout << res.substr(i * 3, 3) << endl;
					}
				}
			}
		}
	}
	return 0;
}

1002.

1002 A+B for Polynomials (25 分)

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 2 1.5 1 2.9 0 3.2

题目大意:模拟一下多项式相加,每一位的系数要保留一位小数。

解题思路:一种是链表,一种是直接用一维数组用下标表示项数的次数,为了熟悉一下stl,我用了vector容器,将两个多项式的项依次读入,每次检查容器里有没有次数相同,如果相同就相加一下,如果没有就重新加入容器。需要注意的是,相加之后可能会等于0,这时候就要erase这个项。最后容器按次数排序输出就可

代码如下

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
struct poly {
	int count;
	double num;
};
bool comp(poly a,poly b){
	return a.count >= b.count;
}
int main()
{
	vector<poly> res;
	int k, p;
	cin >> k;
	for (int i = 0; i < k; i++) {
		poly a;
		cin >> a.count >> a.num;
		res.push_back(a);
	}
	cin >> p;
	for (int i = 0; i < p; i++) {
		poly a;
		cin >> a.count >> a.num;
		bool flag = false;
		for (auto j = res.begin(); j != res.end(); j++) {
			if ((*j).count==a.count)
			{
				flag = true;
				double tmp = (*j).num + a.num;
				if (tmp != 0)
					(*j).num = tmp;
				else
					res.erase(j);
				break;
			}
		}
		if (!flag) {
			res.push_back(a);
		}
	}
	sort(res.begin(), res.end(), comp);
	
	if (res.size() != 0) {
		cout << res.size() << " ";
		for (auto iter = res.begin(); iter != res.end(); iter++) {
			if (iter != res.end() - 1)
				cout << (*iter).count << " " << setiosflags(ios::fixed)
				<< setprecision(1)<<(*iter).num << " ";
			else
				cout << (*iter).count << " " << setiosflags(ios::fixed)
				<< setprecision(1)<<(*iter).num << endl;
		}
	}
	else
		cout << 0 << endl;
	return 0;
}

1005.

1005 Spell It Right (20 分)

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (≤10​100​​).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

题目大意:输入一个数字,输出这个数字各位的和,本来是挺简单的,但它要用英文输出这个数字的各位和。

解题思路:很简单粗暴的string读进来,然后遍历这个string把每个字符变成数字后相加,得到了各位和,然后把这个和通过to_string全局函数变成string,再次遍历,把每一位用英文单词输出一下就好了。

代码如下

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
	string cur;
	cin >> cur;
	int sum = 0;
	for (int i = 0; i < cur.size(); i++) {
		sum = sum + (cur[i] - '0');
	}
	string res = to_string(sum);
	for (int i = 0; i < res.size(); i++) {
		switch (res[i])
		{
		case '1': cout << "one"; break;
		case '2':cout << "two"; break;
		case '3':cout << "three"; break;
		case '4':cout << "four"; break;
		case '5':cout << "five"; break;
		case '6':cout << "six"; break;
		case '7':cout << "seven"; break;
		case '8':cout << "eight"; break;
		case '9':cout << "nine"; break;
		case '0':cout << "zero"; break;
		default:
			break;
		}
		if (i != res.size() - 1)cout << " ";
		else
			cout << endl;
	}
	return 0;
}

1006:

1006 Sign In and Sign Out (25 分)

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time

where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output:

SC3021234 CS301133

题目大意:大概就是记录了每个人签到和签退时间,每次最早到的人是开门的,最晚走的是锁门的,那么输出开门的人和锁门的人。

解题思路:其实问题的核心在于比较两个时间的先后,我觉得用java的话可能比较方便,因为提供了时间先后的比较函数,C++我不知道有没有,可能有吧,因为不知道吗,所以用字符串解析也行,就是依次比较两位数字的先后即可。按时分秒的顺序比较下去。有个atoi的库函数可以把string转换成int,当然用流实现也可以。

代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
using namespace std;
struct sign{
	string id;
	string starttime;
	string endtime;
};
bool cmp(string a,string b) {
	int ahour = atoi(a.substr(0, 2).c_str());
	int amin = atoi(a.substr(3, 2).c_str());
	int asec = atoi(a.substr(6, 2).c_str());
	int bhour = atoi(b.substr(0, 2).c_str());
	int bmin = atoi(b.substr(3, 2).c_str());
	int bsec = atoi(b.substr(6, 2).c_str());
	if (ahour > bhour) {
		return true;
	}
	else if (ahour < bhour) {
		return false;
	}
	else {
		if (amin > bmin) {
			return true;
		}
		else if (amin < bmin) {
			return false;
		}
		else {
			if (asec > bsec) {
				return true;
			}
			else if (asec <= bsec) {
				return false;
			}
		}
	}
}
sign exam[100005];
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> exam[i].id >> exam[i].starttime >> exam[i].endtime;
	}
	string unlock=exam[0].starttime;
	string lock= exam[0].endtime;
	string resunlock = exam[0].id;
	string reslock = exam[0].id;
	for (int i = 0; i < n; i++) {
		if (cmp(unlock, exam[i].starttime)) {
			unlock = exam[i].starttime;
			resunlock = exam[i].id;
		}
		if (!cmp(lock, exam[i].endtime)) {
			lock = exam[i].endtime;
			reslock = exam[i].id;
		}
	}
	cout << resunlock << " " << reslock << endl;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值