题目:统计1~100之间出现了几次数字9
程序分析:
要统计1到100之间出现了几次数字9,可以采用三种不同的方法来实现。以下是三种不同方法的解题思路、代码示例以及各自的优缺点:
方法1: 遍历法
思路:
- 使用循环遍历1到100之间的每个数字,然后对每个数字进行取余和除法操作,以检查其中是否包含数字9。
public class CountNinesMethod1 {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
int num = i;
while (num > 0) {
if (num % 10 == 9) {
count++;