2 solutions

  • 0
    @ 2025-3-31 22:38:38
    #include <bits/stdc++.h>
    #define endl '\n'
    using namespace std;
    typedef pair<int, int> PII;
    using ll = long long;
    using ULL = unsigned long long;
    const int N = 1e7 + 5;
    
    int n;
    
    bool check(int n) {
        string s = to_string(n);
        string a = s;
        reverse(s.begin(),s.end());
        if (s == a) return true;
        else return false;
    }
    
    int getsum(int n) {
        ll ans = 0;
        while (n) {
            auto x = n%10;
            ans += x;
            n /= 10;
        }
        return ans;
    }
    
    inline void solve() {
        cin >> n;
        for (int i = 10000; i <= 999999; i++) {
            if (check(i) && getsum(i) == n) {
                cout << i << endl;
            }
        }
    }
    
    int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        int _ = 1;
        //int _; cin >> _;
        while (_--) solve();
        return 0;
    }
    
    • 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;
      }
      
      
      
      • 1

      Information

      ID
      1838
      Time
      1000ms
      Memory
      512MiB
      Difficulty
      5
      Tags
      # Submissions
      26
      Accepted
      14
      Uploaded By