一、题目链接
http://noi.openjudge.cn/ch0105/30/
二、解题思路
◎ 显然,在待检测的整数m能被19整除的前提下,只需要统计m中数字3的个数即可;
◎ 定义并输入两个int类型的整数m、k,分别代表待检测的整数、题目要求的数字3的个数;
◎ 定义boolean类型(C++程序为bool类型)的逻辑量flag,标记整数m是否满足题目要求,初始时flag为false,也即假定整数m不满足要求;
◎ 定义int类型的整数three,代表m中数字3的个数,初始时three为0;
◎ 如果m能被19整除,则做以下操作:
→ 在当前整数m尚未降阶为0时,循环处理如下:
*** 首先,获取当前整数m的个位数m % 10,如果为3,则令three++;
*** 其次,令m = m / 10,也即移除当前整数m的个位数;
→ 上述循环结束后,如果three等于k,则令flag = true,也即m满足要求;
◎ 如果flag为true,输出YES,否则输出NO。
三、Java程序
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int k = input.nextInt();
boolean flag = false;
int three = 0;
if (m % 19 == 0) {
while (m != 0) {
if (m % 10 == 3) {
three++;
}
m = m / 10;
}
if (three == k) {
flag = true;
}
}
if (flag) {
System.out.print("YES");
}
else {
System.out.print("NO");
}
}
}
四、C++程序
#include <iostream>
using namespace std;
int main()
{
int m;
int k;
cin >> m;
cin >> k;
bool flag = false;
int three = 0;
if (m % 19 == 0)
{
while (m != 0)
{
if (m % 10 == 3)
{
three++;
}
m = m / 10;
}
if (three == k)
{
flag = true;
}
}
if (flag)
{
cout << "YES";
}
else
{
cout << "NO";
}
return 0;
}