You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从整数的首位开始扫描并提取其中的奇数位数字?

Fixing the Odd Digit Output Order Issue

Your current code does a great job of grabbing all the odd digits from the input number, but since it starts pulling digits from the rightmost end (using num % 10), the output ends up reversed. Let's walk through a few straightforward fixes to get those digits printed from the first to the last odd one:

Method 1: Use a String (Simplest & Most Intuitive)

This is the easiest approach—just read the input as a string instead of a number. That way, we can iterate left to right directly, checking each character to see if it's an odd digit:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string numStr;
    cin >> numStr; // Read input directly as a string to preserve digit order
    
    for (char c : numStr) {
        int digit = c - '0'; // Convert character to integer value
        if (digit % 2 != 0) {
            cout << digit;
        }
    }
    
    return 0;
}

Why this works:

  • Strings keep digits in the exact order the user entered them, so we don't have to mess with reversing anything later.
  • Iterating from the first character to the last lets us print odd digits in the correct left-to-right sequence with minimal code.

Method 2: Use a Stack to Reverse Collected Digits

If you want to stick with numeric operations (using long long), we can collect the odd digits in a stack first, then pop them out to print in the original order:

#include <iostream>
#include <stack>

using namespace std;

int main() {
    long long num;
    stack<int> oddDigits;
    cin >> num;
    
    while (num > 0) {
        int digit = num % 10;
        num = num / 10;
        if (digit % 2 != 0) {
            oddDigits.push(digit); // Store odd digits in the stack
        }
    }
    
    // Pop from stack to print in original left-to-right order
    while (!oddDigits.empty()) {
        cout << oddDigits.top();
        oddDigits.pop();
    }
    
    return 0;
}

Why this works:

  • Stacks follow a "last-in, first-out" (LIFO) structure. The first odd digit we extract (rightmost in the original number) gets pushed last, so popping starts with the leftmost odd digit first.

Method 3: Recursive Approach

We can use recursion to process the highest digits first, then print the odd digit as we backtrack through the recursive calls:

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

void printOddDigits(long long num) {
    if (num == 0) {
        return;
    }
    // Get the leftmost digit
    int digitCount = to_string(num).length();
    int leftmostDigit = num / (long long)pow(10, digitCount - 1);
    long long remainingNum = num % (long long)pow(10, digitCount - 1);
    
    if (leftmostDigit % 2 != 0) {
        cout << leftmostDigit;
    }
    printOddDigits(remainingNum);
}

int main() {
    long long num;
    cin >> num;
    printOddDigits(num);
    return 0;
}

Note:

  • This method works but is a bit less efficient than the string or stack approaches. It's a neat example of how recursion can handle order-dependent logic though!

内容的提问来源于stack exchange,提问作者Plamen Mateev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:57:41