【问题标题】:Is this how you add the digits of multiple numbers? (units, tens, hundreds)这是您添加多个数字的数字的方式吗? (单位、十、百)
【发布时间】:2021-12-07 09:14:03
【问题描述】:

我只是想确定这段代码是好的还是需要改进的。大家觉得呢?

#include <iostream>
using namespace std;

    u = (a%10+b%10+c%10+d%10+e%10+f%10);
    t = (a / 10 % 10 + b / 10 %10 + c / 10 % 10 + d / 10 %10 + e / 10 % 10 + f / 10 %10);
    h = (a / 100 % 10 + b / 100 % 10 + c / 100 % 10 + d / 100 % 10 + e / 100 % 10 + f / 100 % 10);
    
    cout << "The sum of the first digit is: " << u << "\n";
    cout << "The sum of the second digit is: " << t << "\n";
    cout << "The sum of the third digit is: " << h ; 

【问题讨论】:

  • 是的,还有改进的余地,但Code Review 可能是一个更好的提问地方。你也可以考虑创建一个minimal reproducible example。
  • 在这里和代码审查中,您应该提供完整的代码。这里不多,但在代码审查中,代码应该是正确的并且可以工作。您发布的代码将导致编译器错误的墙壁,这是一些改进的空间
  • 我建议你熟悉数组和循环。
  • 确实,每当您重复代码时,您都应该想办法让计算机进行重复。代码应该采用任意数量的任意长度的数字,并且即使在那时它仍然更小且更具可读性(尽管您可能需要一些技巧来显示“first”、“second”等。跨度>
  • 感谢大家的建议!我和我的朋友找到了一个使用 for 循环的解决方案,它使阅读更容易:)

标签: c++ operators extract addition digits


【解决方案1】:

您至少可以通过两种方式使其更通用:

  1. 只需为一个号码编写代码。
    然后循环输入任何数字。
  2. 编写代码以添加任何ith 数字。

因此,在这种情况下,您可以只关注如何提取给定数字 n 的 ith 数字。
如您的代码所示,可以使用(n / divisor) % 10 完成什么,其中divisor 将是exp(10, i),i 从0 开始。
Demo

#include <algorithm>  // for_each
#include <iostream>  // cout
#include <numeric>  // accumulate
#include <vector>

int main()
{
    std::vector<int> v{123'456, 99'999, 100};

    auto sum_of_ith_digit = [](const std::vector<int>& v, size_t i) {
        auto divisor{1};
        while (i != 0) { divisor *= 10; i--; }  // 1 for digit 0, 10 for digit 1, 100 for digit 2...

        return std::accumulate(std::cbegin(v), std::cend(v), 0,  // for each number in v
            [&divisor](auto& total, const auto& n){              // get ith digit
                return total + (n / divisor) % 10;               // and add it to a total,
            });                                                  // then return the total
    };

    auto units = sum_of_ith_digit(v, 0);
    auto tens = sum_of_ith_digit(v, 1);
    auto hundreds = sum_of_ith_digit(v, 2);

    std::for_each(std::cbegin(v), std::cend(v), [first=true](const int n) mutable {
        std::cout << (first ? "v = " : ", ") << n; first = false; });
    std::cout << "\n\tunits = " << units << ", tens = " << tens << ", hundreds = " << hundreds << "\n";
}

// Outputs:
//     v = 123456, 99999, 100
//         units = 15, tens = 14, hundreds = 14

下面的代码应该完全一样,尽管它可能更容易理解。 Demo

#include <iostream>  // cout
#include <vector>

int sum_of_ith_digit(const std::vector<int>& v, size_t i)
{
    // 1 for digit 0, 10 for digit 1, 100 for digit 2...
    int divisor{1};
    while (i != 0) { divisor *= 10; i--; }

    // For each number in v, get ith digit, and add it to a total, then return the total
    int total{0};
    for (int n : v) { total += (n / divisor) % 10; }

    return total;
};


int main()
{
    std::vector<int> v{123'456, 99'999, 100};

    int units = sum_of_ith_digit(v, 0);
    int tens = sum_of_ith_digit(v, 1);
    int hundreds = sum_of_ith_digit(v, 2);

    std::cout << "v = ";
    bool first{true};
    for (int n : v)
    {
        if (not first) { std::cout << ", "; }
        else { first = false; }
        std::cout << n;
    }
    std::cout << "\n\tunits = " << units << ", tens = " << tens << ", hundreds = " << hundreds << "\n";
}

// Outputs:
//     v = 123456, 99999, 100
//         units = 15, tens = 14, hundreds = 14

【讨论】:

  • @MaartenBodewes 哈哈,你是对的。我应该从真正的循环开始。我会更新我的帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
  • 2016-04-07
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多