判断满足条件的三位数

该篇文章展示了如何使用Java编写一个程序,通过遍历并检查从101到输入的三位数n内的整数,找出其中具有唯一一个数字重复的完全平方数,计算并输出符合条件的个数。

判断满足条件的三位数

import java.util.Scanner;

public class PerfectSquare {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int number = sc.nextInt();

        System.out.printf("count=%d\n", search(number));

}

}

输入样例:

输入三位数正整数n(最高位数字非0): 500

输出样例:

函数search返回[101, n]

三位数中有两位数字相同的

完全平方数的个数

count=6

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int number = sc.nextInt();

        System.out.printf("count=%d\n", search(number));

    }

   

    public static int search(int n) {

        int count = 0;

       

        for (int i = 0; i <= n; i++) {

            int temp = (int) Math.sqrt(i);

            if (temp * temp == i) {

                int[] digits = new int[10];

                int num = i;

                int duplicateCount = 0;

                while (num > 0) {

                    int digit = num % 10;

                    digits[digit]++;

                    if (digits[digit] == 2) { // 只统计出现两次的数字

                        duplicateCount++;

                    }

                    num /= 10;

                }

                if (duplicateCount == 1) { // 只有一个数字出现两次才计数

                    count++;

                }

            }

        }

       

        return count;

    }

}

在PTA平台上,判断满足条件三位数问题通常是统计给定区间内的三位数中有两位数字相同的完全平方数的个数。以下是几种不同的实现方法: ### 方法一:使用`while`循环和`pow`函数 ```c #include <stdio.h> #include <math.h> int search( int n ) { int index = 11; int a, b ,c ,t; int count = 0; while (pow(index, 2) <= n) { t = pow(index, 2); index++; a = t % 10; b = (t / 10) % 10; c = t / 100; if (a == b || b == c || a == c) { count++; } } return count; } int main() { int number; scanf("%d", &number); printf("count=%d\n", search(number)); return 0; } ``` 此方法从11开始,利用`while`循环计算平方数,通过`pow`函数计算平方,再通过取余和除法运算获取三位数的每一位,判断是否有两位数字相同,若满足条件则计数器加1 [^2]。 ### 方法二:使用`for`循环和`sqrt`函数 ```c #include <stdio.h> #include <math.h> int search(int n) { int count = 0; for (int i = 101; i <= n; i++) { int number = i; if ((int) sqrt(number) * (int) sqrt(number) == number) { int lastNumber = number % 10; number /= 10; int middleNumber = number % 10; number /= 10; int firstNumber = number % 10; if (firstNumber == middleNumber || firstNumber == lastNumber || middleNumber == lastNumber) { count++; } } } return count; } int main() { int number; scanf("%d", &number); printf("count=%d\n", search(number)); return 0; } ``` 该方法使用`for`循环遍历区间`[101, n]`内的所有数,通过`sqrt`函数判断是否为完全平方数,再获取每一位数字判断是否有两位相同,满足条件则计数器加1 [^3]。 ### 方法三:使用`for`循环和`pow`函数 ```c #include <stdio.h> #include <math.h> int search(int n) { int x, g, s, b, sum = 0; for(int i = 11; i <= sqrt(n); i++) { x = pow(i, 2); g = x % 10; s = x % 100 / 10; b = x / 100; if (g == s || g == b || s == b) { sum++; } } return sum; } int main() { int number; scanf("%d", &number); printf("count=%d\n", search(number)); return 0; } ``` 此方法同样使用`for`循环,从11开始,通过`pow`函数计算平方数,获取每一位数字判断是否有两位相同,满足条件则计数器加1 [^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值