首页 > 解决方案 > 从 C++ 中的 3 位数字中提取数字是否可以不使用数组?

问题描述

问题是:编写一个函数,作为输入参数接收一个三位数的正数,因此必须得到相同的 3 位数除以中位数获得的最大和最小数之和。示例:函数 438 的输入参数 相同位数最大的是 843,最小的是 348,所以应该计算 (843 + 348) / 4。

我已经尝试过了,结果还可以,但是我的代码似乎很复杂,所以我问有没有更好的方法来做到这一点?

提前致谢

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int check(int x) {

    int a, b, c, biggestNum, smallestNum, medianNum;

    a = x / 100;
    b = (x / 10) % 10;
    c = x % 10;

    if (a > b && a > c && b > c) {

        biggestNum= a * 100 + b * 10 + c;
        smallestNum= c * 100 + b * 10 + a;
        medianNum= b;

    }
    else if (a > b && a > c && b < c) {

        biggestNum= a * 100 + c * 10 + b;
        smallestNum= b * 100 + c * 10 + a;
        medianNum= c;

    }
    else if (b > a && b > c && a < c) {

        biggestNum= b * 100 + c * 10 + a;
        smallestNum= a * 100 + c * 10 + b;
        medianNum= c;

    }
    else if (b > a && b > c && a > c) {

        biggestNum= b * 100 + a * 10 + c;
        smallestNum= c * 100 + a * 10 + b;
        medianNum= a;

    }
    else if (c > a && c > b && a > b) {

        biggestNum= c * 100 + a * 10 + b;
        smallestNum= b * 100 + a * 10 + c;
        medianNum= a;

    }
    else if (c > a && c > b && a < b) {

        biggestNum= c * 100 + b * 10 + a;
        smallestNum= a * 100 + b * 10 + c;
        medianNum= b;

    }   

    cout << "Smallest number is: " << smallestNum<< " ,biggest is: " << biggestNum << " and median is: " << medianNum<< "." << endl;

    return (biggestNum + smallestNum) / medianNum;
}


int main() {

    cout << "Enter one 3 digit positive number: ";
    int x;
    cin >> x;

    float result = check(x);
    cout << "The result is: " << result << "." << endl;

    system("pause");
    return 0;
}

标签: c++

解决方案


考虑到结果是用整数算术计算的,发布的代码不能真正产生正确的答案:

int check(int x)  // <- note the type of the returned value
{
    int biggestNum, smallestNum, medianNum;
    // ...
    return (biggestNum + smallestNum) / medianNum;  // <- This is an integer division
}

int main()
{
    int x; 
    // ...   
    float result = check(x);  // Now it's too late to get the right result
}

该逻辑也没有考虑所有可能的情况,事实上它忽略了重复的数字,并且if else if缺少默认分支(最终无条件else)的大构造使那些未初始化的变量未确定,因此以下操作给出了毫无意义的结果.

鉴于分配限制,我会写如下内容

#include <utility>

// The assignment is about 3-digit numbers, you should check that x is actually in
// the range [100, 999]. Note that one of the extremes is a special case.
// Well, both, actually.
double I_ve_no_idea_how_to_name_this(int x)
{
    constexpr int base = 10;
    int smallest = x % base;
    x /= base;
    int median = x % base;
    x /= base; 
    // Note that this "works" (extracting the third digit) even if
    // x isn't a 3-digit number. If you can assure the input is well
    // defined, you can simplify this.
    int biggest = x % base; 

    // Now we can sort the previous variables.
    using std::swap;
    if ( median < smallest ) {
        swap(median, smallest);
    }
    // Now I know that smallest <= median
    if ( biggest < median ) {
        swap(biggest, median);
    }
    // Now I know that median <= biggest
    // ...
    // Is that enough or am I missing something here?
    // Please think about it before running the code and test it.

    // Once the variables are sorted, the result is easily calculated
    return (biggest + smallest + base * (2 * median + base * (biggest + smallest)))
           / static_cast<double>(median);
}

推荐阅读