> 文章列表 > PTA:L1-022 奇偶分家、L1-023 输出GPLT、L1-024 后天(C++)

PTA:L1-022 奇偶分家、L1-023 输出GPLT、L1-024 后天(C++)

PTA:L1-022 奇偶分家、L1-023 输出GPLT、L1-024 后天(C++)

目录

L1-022 奇偶分家

问题描述:

L1-023 输出GPLT

问题描述:

实现代码:

L1-024 后天

问题描述:

实现代码:


        简单题,没写题解,看代码就能看懂

L1-022 奇偶分家

问题描述:

        给定N个正整数,请统计奇数和偶数各有多少个?

输入格式

输入第一行给出一个正整N(≤1000);第2行给出N个非负整数,以空格分隔。

输出格式:

在一行中先后输出奇数的个数、偶数的个数。中间以1个空格分隔。

输入样例

9
88 74 101 26 15 0 34 22 77

输出样例:

3 6

实现代码:

#include <iostream>using namespace std;const int N = 1010;int main()
{int n;int dou = 0;//偶数int sim = 0;//单数cin >> n;while (n--){int m;cin >> m;if (m % 2 == 0){dou++;}else{sim++;}}cout << sim << " " << dou << endl;
}

L1-023 输出GPLT

问题描述:

给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT的顺序打印,直到所有字符都被输出。

输入格式:

输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。

输出格式:

在一行中按题目要求输出排序后的字符串。题目保证输出非空。

输入样例:

pcTclnGloRgLrtLhgljkLhGFauPewSKgt

输出样例:

GPLTGPLTGLTGLGLL

实现代码:

#include <iostream>
#include <string>
using namespace std;int al[4];int main()
{char hash[4] = { 'G','P','L','T' };string s;cin >> s;for (int i = 0; i < s.size(); i++){if (s[i] == 'g' || s[i] == 'G'){al[0]++;}if (s[i] == 'p' || s[i] == 'P'){al[1]++;}if (s[i] == 'l' || s[i] == 'L'){al[2]++;}if (s[i] == 't' || s[i] == 'T'){al[3]++;}}while (al[0] > 0 || al[1] > 0 || al[2] > 0 || al[3] > 0){for (int i = 0; i < 4; i++){if (al[i] > 0){cout << hash[i];al[i]--;}}}
}

L1-024 后天

问题描述:

        如果今天是星期三,后天就是星期五;如果今天是星期六,后天就是星期一。我们用数字1到7对应星期一到星期日。给定某一天,请你输出那天的“后天”是星期几。

输入格式:

输入第一行给出一个正整数D(1 ≤ D ≤ 7),代表星期里的某一天。

输出格式:

在一行中输出D天的后天是星期几。

输入样例:

3

输出样例:

5

实现代码:

#include<iostream>using namespace std;int main()
{int n;cin >> n;int t = (n + 2) % 7;if(t == 0) t = 7;cout << t << endl;
}