[Lanqiao Cup 3491] Lucky Number (Simulation & Enumeration & Java)

Problem Description

input Output

Problem-solving ideas

The numbers that satisfy the meaning of the question need to meet these conditions:

1. There is an even number of digits.

2. The sum of each digit in the first half is equal to the sum of each digit in the second half.

The idea of ​​the code below is to traverse from 1 all the way, convert the number into a String, and then operate on the first half of the digits and the second half of the digits respectively, and finally compare them.

Note: Output the number directly when submitting, otherwise it will time out! ! ! ! !

AC code

public class Main {
    public static void main(String[] args) {
    	/*
        // 定义最大数值
        int maxNum = 100000000;

        // 记录符合条件的数的数量
        int ans = 0;

        // 遍历数字范围
        for (int i = 1; i <= maxNum; i++) {
            // 将数字转换为字符串以便获取长度
            String str = String.valueOf(i);
            int len = str.length();

            // 如果数字的位数为奇数,跳过该数字
            if (len % 2 != 0) {
                continue;
            }

            // 分别计算左半部分和右半部分的数字之和
            int left = 0, right = 0;
            for (int j = 0; j < len; j++) {
                if (j < len / 2) {
                    left += str.charAt(j) - '0';
                } else {
                    right += str.charAt(j) - '0';
                }
            }

            // 如果左右两部分的数字之和相等,符合条件
            if (left == right) {
                // 输出符合条件的数字
                // System.out.println(i);
                ans++;
            }
        }

        // 输出符合条件的数字数量
        System.out.println(ans);
        */

        // 输出给定的数字
        System.out.println(4430091);
    }
}

related information 

Common methods of String class

public class StringMethodsExample {
    public static void main(String[] args) {
        // 1. length(): 返回字符串的长度
        String str1 = "Hello, World!";
        int length = str1.length();
        System.out.println("Length: " + length);

        // 2. charAt(int index): 返回指定索引位置的字符
        char charAtIndex = str1.charAt(7);
        System.out.println("Character at index 7: " + charAtIndex);

        // 3. substring(int beginIndex): 返回从指定索引开始到字符串末尾的子字符串
        String substring1 = str1.substring(7);
        System.out.println("Substring from index 7: " + substring1);

        // 4. substring(int beginIndex, int endIndex): 返回从指定索引开始到指定索引结束的子字符串
        String substring2 = str1.substring(7, 12);
        System.out.println("Substring from index 7 to 12: " + substring2);

        // 5. equals(Object obj): 比较字符串内容是否相等
        String str2 = "Hello, World!";
        boolean isEqual = str1.equals(str2);
        System.out.println("Are strings equal? " + isEqual);

        // 6. equalsIgnoreCase(String anotherString): 比较字符串内容是否相等,忽略大小写
        String str3 = "hello, world!";
        boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str3);
        System.out.println("Are strings equal (ignore case)? " + isEqualIgnoreCase);

        // 7. indexOf(String str): 返回第一次出现指定子字符串的索引
        int indexOfWorld = str1.indexOf("World");
        System.out.println("Index of 'World': " + indexOfWorld);

        // 8. toLowerCase(): 将字符串转换为小写
        String lowerCaseStr = str1.toLowerCase();
        System.out.println("Lowercase: " + lowerCaseStr);

        // 9. toUpperCase(): 将字符串转换为大写
        String upperCaseStr = str1.toUpperCase();
        System.out.println("Uppercase: " + upperCaseStr);

        // 10. trim(): 去除字符串两端的空白字符
        String strWithSpaces = "   Trim me!   ";
        String trimmedStr = strWithSpaces.trim();
        System.out.println("Trimmed string: '" + trimmedStr + "'");
    }
}

(by Memories)

おすすめ

転載: blog.csdn.net/qq1677852098/article/details/134537347