PTA 团体程序设计天梯赛-练习集 整理
L1-001 Hello World (5分)
这道超级简单的题目没有任何输入。
你只需要在一行中输出著名短句“Hello World!”就可以了。
输入样例:
无
输出样例:
Hello World!
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
L1-002 打印沙漏 (20分)
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
*****
***
*
***
*****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
输入格式:
输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。
输出格式:
首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例:
19 *
输出样例:
*****
***
*
***
*****
2
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int key = 1;
vector<int> result;
int temp = key;
while(key <= 2000)
{
result.push_back(key);
temp += 2;
key = 2 * temp + key;
}
// for(auto i : result)
// {
// cout << i << " ";
// }
int count = 0;
int n = 0;
int outi = 0;
cin >> n;
for (int i = 0; i < result.size(); ++i)
{
if(result[i] > n)
{
count = i;
break;
}
}
outi = n - result[count - 1];
// for(auto i : result)
// {
// cout << i << " ";
// }
// cout << endl;
int tempkey = 0;
string s1;
cin >> s1;
for (int i = count; i > 0; --i)
{
for (int j = 0; j < tempkey; ++j)
{
cout << " ";
}
for (int j = 2 * i - 1; j > 0; --j)
{
cout << s1;
}
// for (int j = 0; j < tempkey; ++j)
// {
// cout << " ";
// }
tempkey++;
cout << endl;
}
tempkey -= 2;
for (int i = 2; i <= count; ++i)
{
for (int j = 0; j < tempkey; ++j)
{
cout << " ";
}
for (int j = 2 * i - 1; j > 0; --j)
{
cout << s1;
}
// for (int j = 0; j < tempkey; ++j)
// {
// cout << " ";
// }
tempkey--;
cout << endl;
}
cout << outi << endl;
return 0;
}
L1-003 个位数统计 (15分)
给定一个 k 位整数 N=d**k−110k−1+⋯+d1101+d0 (0≤d**i≤9, i=0,⋯,k−1, d**k−1>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 N=100311,则有 2 个 0,3 个 1,和 1 个 3。
输入格式:
每个输入包含 1 个测试用例,即一个不超过 1000 位的正整数 N。
输出格式:
对 N 中每一种不同的个位数字,以 D:M
的格式在一行中输出该位数字 D
及其在 N 中出现的次数 M
。要求按 D
的升序输出。
输入样例:
100311
输出样例:
0:2
1:3
3:1
#include <iostream>
#include <vector>
using namespace std;
void solve(vector<int>& result , string N)
{
for(int i = 0; i < N.size(); ++i)
{
int key = N[i] - '0';
//cout << key<< endl;
result[key]++;
}
}
int main()
{
vector<int> result(10 , -1);
string N;
cin >> N;
solve(result , N);
for(int i = 0; i < 10; ++i)
{
if(result[i] != -1)
{
cout << i << ":" << result[i] + 1 << endl;
}
}
return 0;
}
L1-004 计算摄氏温度 (5分)
给定一个华氏温度F,本题要求编写程序,计算对应的摄氏温度C。计算公式:C=5×(F−32)/9。题目保证输入与输出均在整型范围内。
输入格式:
输入在一行中给出一个华氏温度。
输出格式:
在一行中按照格式“Celsius = C”输出对应的摄氏温度C的整数值。