1 solutions

  • 0
    @ 2024-9-4 13:46:10
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    // 判断一个数是否是回文数
    bool isPalindrome(int num) {
    	int original = num;
    	int reversed = 0;
    	while (num > 0) {
    		reversed = reversed * 10 + num % 10;
    		num /= 10;
    	}
    	return original == reversed;
    }
    
    // 计算一个数的各位数字之和
    int digitSum(int num) {
    	int sum = 0;
    	while (num > 0) {
    		sum += num % 10;
    		num /= 10;
    	}
    	return sum;
    }
    
    void findSpecialNumbers(int n) {
    	vector<int> results;
    	
    	// 检查所有五位和六位数
    	for (int i = 10000; i < 1000000; i++) {
    		if (isPalindrome(i) && digitSum(i) == n) {
    			results.push_back(i);
    		}
    	}
    	
    	// 输出所有满足条件的数
    	if (results.empty()) {
    		cout << "没有找到符合条件的数。" << endl;
    	} else {
    		for (int num : results) {
    			cout << num << endl;
    		}
    	}
    }
    
    int main() {
    	int n;
    	cin >> n;
    	
    	findSpecialNumbers(n);
    	
    	return 0;
    }
    
    
    

    Information

    ID
    1838
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    7
    Tags
    # Submissions
    20
    Accepted
    9
    Uploaded By