欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > cf<contest/1950>练习-python版

cf<contest/1950>练习-python版

2025/2/8 0:25:41 来源:https://blog.csdn.net/2403_87580523/article/details/145289048  浏览:    关键词:cf<contest/1950>练习-python版

https://codeforces.com/contest/1950

前四题python题解(cf翻译代码符号可能变中文)

1.Stair, Peak, or Neither?

题面翻译

T T T 组数据。每组数据给定 3 3 3 个整数 a , b , c a, b, c a,b,c,若满足 a < b < c a < b < c a<b<c 则输出 STAIR;若满足 a < b > c a < b > c a<b>c 则输出 PEAK;若都不满足则输出 NONE

题目描述

You are given three digits $ a $ , $ b $ , and $ c $ . Determine whether they form a stair, a peak, or neither.

  • A stair satisfies the condition $ a<b<c $ .
  • A peak satisfies the condition $ ac $ .
输入格式

The first line contains a single integer $ t $ ( $ 1 \leq t \leq 1000 $ ) — the number of test cases.

The only line of each test case contains three digits $ a $ , $ b $ , $ c $ ( $ 0 \leq a $ , $ b $ , $ c \leq 9 $ ).

输出格式

For each test case, output “STAIR” if the digits form a stair, “PEAK” if the digits form a peak, and “NONE” otherwise (output the strings without quotes).

样例 #1
样例输入 #1
7
1 2 3
3 2 1
1 5 3
3 4 1
0 0 0
4 1 7
4 5 7
样例输出 #1
STAIR
NONE
PEAK
PEAK
NONE
NONE
STAIR

题目思路:

1.如果a,b,c从小到大排列就输出STAIR

2.如果中间最大就输出PEAK

3.其他情况输出NONE

考察:if-elif-else语句的使用

代码情况如下:

n=int(input())
for i in range(n):a,b,c=map(int,input().split())if a<b<c:print('STAIR')elif b>a and b>c:print('PEAK')else:print('NONE')

2.Upscaling

题面翻译
题目描述

给你一个整数 n n n,输出一个 2 n × 2 n 2n \times 2n 2n×2n 的棋盘,由 2 × 2 2 \times 2 2×2# 2 × 2 2 \times 2 2×2. 构成,最左上角的单元格为 2 × 2 2 \times 2 2×2# 构成。

输入格式

1 1 1 行为一个整数 t t t,表示询问的次数。

接下来 t t t 行,每行一个整数 n n n

输出格式

对于每个测试用例,输出 2 n 2n 2n 行,每行包含 2 n 2n 2n 个没有空格的字符——如语句中所述的棋盘。不要在每个测试用例之间输出空行。

translate by sqrt404
题目描述

You are given an integer $ n $ . Output a $ 2n \times 2n $ checkerboard made of $ 2 \times 2 $ squares alternating ’ $ \texttt{#} $ ’ and ’ $ \texttt{.} $ ', with the top-left cell being ’ $ \texttt{#} $ '.

The picture above shows the answers for $ n=1,2,3,4 $ .

输入格式

The first line contains an integer $ t $ ( $ 1 \leq t \leq 20 $ ) — the number of test cases.

The only line of each test case contains a single integer $ n $ ( $ 1 \leq n \leq 20 $ ) — it means you need to output a checkerboard of side length $ 2n $ .

输出格式

For each test case, output $ 2n $ lines, each containing $ 2n $ characters without spaces — the checkerboard, as described in the statement. Do not output empty lines between test cases.

样例 #1
样例输入 #1
4
1
2
3
4
样例输出 #1
##
##
##..
##..
..##
..##
##..##
##..##
..##..
..##..
##..##
##..##
##..##..
##..##..
..##..##
..##..##
##..##..
##..##..
..##..##
..##..##

题目思路:

错误思路1:

一开始作者小白不清楚要考察的内容以为是只会有1,2,3,4种图形情况

n=int(input())
for i in range(n):m=int(input(())if m==1:print('##\n##')elif m==2:print('##..\n##..\n..##\n..##')elif M==3:print('##..##\n##..##\n..##..\n..##..\n##..##\n##..##')elif m==4:                                               print        ('##..##..\n##..##..\n..##..##\n..##..##\n##..##..\n##..##..\n..##..##\n..##..##')

发现测试2报错到达了20的图形情况,所以判断为规律题

我们把四个格子看成一个格子的话,就是判断一下横纵坐标之和的奇偶性。

就是横纵坐标之和奇的为一种类型,横纵坐标之和偶的为一种类型

T = int(input())
while T > 0:n =int(input())n =2*nfor i in range(n):for j in range(n):if ((i // 2 + j // 2) % 2 == 1):print('.', end='')else:print('#', end='')print() T -= 1

3.Clock Conversion

题面翻译
题目描述

给出 24 小时制的时间,输出 12 小时制的相应时间。

  • 24 小时格式: 将一天分为 24 小时,从 00 00 00 23 23 23 ,每个小时有 60 分钟,从 00 00 00 59 59 59
  • 12小时格式: 将一天分为两半:上半部分是 A M \mathrm{AM} AM ,下半部分是 P M \mathrm{PM} PM 。在每一半中,小时按 12 , 01 , 02 , 03 , … , 11 12, 01, 02, 03, \dots, 11 12,01,02,03,,11 的顺序编号。每个小时有 60 分钟,从 00 00 00 59 59 59

输入

第一行包含一个整数 t t t ( 1 ≤ t ≤ 1440 1 \leq t \leq 1440 1t1440 ) - 测试用例数。

hh 表示从 00 00 00 23 23 23 的小时,mm 表示从 00 00 00 59 59 59 的分钟。
输入的时间将始终是有效的 24 小时制时间。

输出

对于每个测试用例,输出两个用空格隔开的字符串("hh:mm AM "或 “hh:mm PM”),这两个字符串相当于测试用例中提供的 12 小时时间(不带引号)。
输出的时间应与指定的时间完全一致;特别是不要去掉前导零。

题目描述

Given the time in 24-hour format, output the equivalent time in 12-hour format.

  • 24-hour format divides the day into 24 hours from $ 00 $ to $ 23 $ , each of which has 60 minutes from $ 00 $ to $ 59 $ .
  • 12-hour format divides the day into two halves: the first half is $ \mathrm{AM} $ , and the second half is $ \mathrm{PM} $ . In each half, the hours are numbered in the order $ 12, 01, 02, 03, \dots, 11 $ . Each hour has 60 minutes numbered from $ 00 $ to $ 59 $ .
输入格式

The first line contains a single integer $ t $ ( $ 1 \leq t \leq 1440 $ ) — the number of test cases.

The only line of each test case contains a string $ s $ of length $ 5 $ with format hh:mm representing a valid time in the 24-hour format. hh represents the hour from $ 00 $ to $ 23 $ , and mm represents the minute from $ 00 $ to $ 59 $ .

The input will always be a valid time in 24-hour format.

输出格式

For each test case, output two strings separated by a space (“hh:mm AM” or “hh:mm PM”), which are the 12-hour equivalent to the time provided in the test case (without quotes).

You should output the time exactly as indicated; in particular, you should not remove leading zeroes.

样例 #1
样例输入 #1
11
09:41
18:06
12:14
00:59
00:00
14:34
01:01
19:07
11:59
12:00
21:37
样例输出 #1
09:41 AM
06:06 PM
12:14 PM
12:59 AM
12:00 AM
02:34 PM
01:01 AM
07:07 PM
11:59 AM
12:00 PM
09:37 PM

思路:将24进制转化成12进制,并且用am或者pm来表明上下午

运用了便捷if else看起来清晰一点: 判断了==0,>12,<12三种状况和am,pm

运用.format种{:02d}确保补0

T = int(input()) 
for _ in range(T): time= input() h, m = map(int, time.split(':'))  new_h = 12 if h == 0 else (h - 12 if h > 12 else h)am_pm = "PM" if h >= 12 else "AM"  print("{:02d}:{:02d} {}".format(new_h, m, am_pm)) 

4.Product of Binary Decimals

题面翻译
二进制小数的乘积
题目描述

我们称一个数字为二进制小数,如果它是一个正整数,并且其十进制表示中的所有数字都是0或1。例如, 1010111 1010111 1010111 是一个二进制小数,而 10201 10201 10201 787788 787788 787788 不是。

给定一个数 n n n,你被要求判断是否可能将 n n n 表示为一些(不一定是不同的)二进制小数的乘积。

输入格式

第一行包含一个整数 t t t 1 ≤ t ≤ 5 ⋅ 1 0 4 1 \leq t \leq 5 \cdot 10^4 1t5104)— 测试用例的数量。

每个测试用例的唯一一行包含一个整数 n n n 1 ≤ n ≤ 1 0 5 1 \leq n \leq 10^5 1n105)。

输出格式

对于每个测试用例,如果 n n n 可以表示为一些二进制小数的乘积,则输出 “YES”(不带引号),否则输出 “NO”(不带引号)。

你可以以任何形式输出 “YES” 和 “NO”(例如,字符串 “yES”、“yes” 和 “Yes” 都将被认为是肯定的响应)。

说明/提示

前五个测试用例可以表示为二进制小数的乘积如下:

121 = 11 × 11 121 = 11 \times 11 121=11×11
1 = 1 1 = 1 1=1 已经是一个二进制小数。
14641 = 11 × 11 × 11 × 11 14641 = 11 \times 11 \times 11 \times 11 14641=11×11×11×11
12221 = 11 × 11 × 101 12221 = 11 \times 11 \times 101 12221=11×11×101
10110 = 10110 10110 = 10110 10110=10110 已经是一个二进制小数。

题目描述

Let’s call a number a binary decimal if it is a positive integer and all digits in its decimal notation are either $ 0 $ or $ 1 $ . For example, $ 1,010,111 $ is a binary decimal, while $ 10,201 $ and $ 787,788 $ are not.

Given a number $ n $ , you are asked whether or not it is possible to represent $ n $ as a product of some (not necessarily distinct) binary decimals.

输入格式

The first line contains a single integer $ t $ ( $ 1 \leq t \leq 5 \cdot 10^4 $ ) — the number of test cases.

The only line of each test case contains a single integer $ n $ ( $ 1 \leq n \leq 10^5 $ ).

输出格式

For each test case, output “YES” (without quotes) if $ n $ can be represented as a product of binary decimals, and “NO” (without quotes) otherwise.

You can output “YES” and “NO” in any case (for example, strings “yES”, “yes”, and “Yes” will be recognized as a positive response).

样例 #1
样例输入 #1
11
121
1
14641
12221
10110
100000
99
112
2024
12421
1001
样例输出 #1
YES
YES
YES
YES
YES
YES
NO
NO
NO
NO
YES
提示

The first five test cases can be represented as a product of binary decimals as follows:

  • $ 121 = 11 \times 11 $ .
  • $ 1 = 1 $ is already a binary decimal.
  • $ 14,641 = 11 \times 11 \times 11 \times 11 $ .
  • $ 12,221 = 11 \times 11 \times 101 $ .
  • $ 10,110 = 10,110 $ is already a binary decimal.

解题思路:
作者小白一开始不能理解二进制十进制数的乘积那么大的数应该怎么判断呢,看到了n<100000,本来二进制十进制数就少,那么他的乘积不就更少了咩,那么就可以用预处理暴力解决,什么意思呢,就是遍历所有情况,如果全部由01组成那么必是二进制十进制的乘积,因为可以乘以一,其他的就慢慢遍历就好了,注意不要漏了1的情况

t=int(input())
for _ in range(t):n=int(input())s=str(n)if s.count('0')+s.count('1')==len(s):print('YES')else:l=[10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111]while (n>0):for m in l:if n%m==0:n=n//mbreakelse:breakif n==1:print('YES')else:print('NO')

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com