PTA 团体程序设计天梯赛-练习集答案 整理

这篇博客整理了PTA团体程序设计天梯赛的多个练习题,包括Hello World、打印沙漏、个位数统计、计算摄氏温度等题目,涵盖了不同的难度和算法,适合初学者进行编程训练。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的整数值。

输入样例:


                
### PTA 团体程序设计天梯赛练习 Python 答案示例 #### 打印沙漏形状的解题思路 为了实现将给定数量的特定字符按照沙漏形状打印的功能,可以采用逐层构建的方式。具体来说: - 需要计算每一层所需的字符数并逐步减少直到中心最小层数再逐渐增加。 - 使用两个循环分别处理上半部分和下半部分。 以下是具体的Python代码实现[^1]: ```python def print_sandglass(char, total_chars): # 计算最大宽度(即最外层星星的数量) width = int((total_chars + 1) ** 0.5) current_width = width used_chars = 0 while True: line = char * min(current_width, total_chars - used_chars) if not line.strip(): break print(line.center(width)) used_chars += len(line.replace(' ', '')) if current_width == 1 or used_chars >= total_chars: break current_width -= 2 if current_width > 1 else 0 current_width += 2 while current_width <= width and used_chars < total_chars: line = char * min(current_width, total_chars - used_chars) if not line.strip(): break print(line.center(width)) used_chars += len(line.replace(' ', '')) current_width += 2 n = int(input().strip()) print_sandglass('*', n) ``` 此段代码实现了基于输入整数值`n`来创建相应大小的沙漏图案,并使用指定字符填充该结构。注意这里假设输入的是奇数个星号以便形成完美的上下对称图形;对于偶数情况可能需要额外逻辑调整以适应题目要求。 #### 处理性别配对问题的解题方法 针对另一个关于性别匹配的问题,则可以通过遍历列表找到符合条件的对象来进行输出操作。下面给出了一种解决方案[^2]: ```python data = [] for _ in range(int(input())): info = input() gender = int(info[0]) person_name = info[2:] data.append([gender, person_name]) paired = set() for idx, item in enumerate(data): if idx in paired: continue target_gender = 1 - item[0] for other_idx, other_item in reversed(list(enumerate(data))): if other_item[0] == target_gender and other_idx not in paired: print(f"{item[1]} {other_item[1]}") paired.update({idx, other_idx}) break ``` 这段脚本读取多行字符串作为数据源,每行的第一个字符代表性别(男或女),后面跟着的名字则是参与者姓名。通过两次迭代完成异性之间的两两组合输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒜蓉蒸大虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值