同时检查能否被 19 整除,以及十进制表示中数字 3 的出现次数。
OJ: noi_openjudge
题目 ID: ch0105-30
难度:入门
标签:条件判断整除字符串python
日期: 2026-07-30 23:01
题意
给定正整数 3。两个条件都满足时输出 YES。
思路
整除条件可直接用 number % 19 == 0 判断。把整数转换为字符串后,str(number).count("3") 就能统计数字 3 的出现次数。
两个条件使用 and 连接,只有都成立时才输出 YES。
代码
Python代码
python
number, required_count = map(int, input().split())
is_valid = number % 19 == 0 and str(number).count("3") == required_count
print("YES" if is_valid else "NO")C++代码
cpp
#include <cstdio>
int main(){
int n,k;
scanf("%d%d",&n,&k);
if( n % 19 != 0){
printf("NO");
return 0;
}
int cnt = 0;
while( n !=0){
if( n % 10 == 3)
cnt++;
n /= 10;
}
if( cnt == k)
printf("YES");
else
printf("NO");
return 0;
}复杂度
设
总结
同一输入需要满足多条性质时,分别写出每条布尔条件,再用逻辑运算组合,表达会清楚且易检查。