在 C++ 中一次获取大整数输入

发布于 2024-10-05 23:24:20 字数 55 浏览 3 评论 0原文

我想一次输入 1000 位整数,&想单独添加数字。有没有什么输入法可以接受这么大的输入?

i want to take a 1000 digit integer input all at a time,& want to add the digits separately.is there any input method to take such a large input?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

墨离汐 2024-10-12 23:24:20

您需要将其作为字符串输入。拆分它们,并将每个字符转换为整数。将它们加起来,就完成了。

例如,这里的数字(随机生成):

9624526619162264306083309360203157186784123851390498919674886891002552146753945797326679482200717699585297042606470048297021049209667042255911984240697992738371633115195140494325737382583412562136836759072897211537655046343769659111215754043609344618490646811291135643554115350431099553593485744944746093896695837300975718819726339233383800764568364950577294931831936979504756278187812548901366714205562309364234394802723329400976924082450161974562063268243689930750925213262044910428021004262080895556879515597779404780565380480750286553508081070834339176079062215815331059349488936312244526697733596052063044560959189161656978673936732284706841120711543620038686227462170335634371808995466024671420024705248851244350701111587608201303840696489479021196275228499780922745352396928865910631672384263395712487735712098161853665189905194589355110620257494673972892816413534347360049692019184831019218764766067298983043791063184786671132332077197148683743991683245617836086353821268720434176862469084808

这是 C++ 程序:

int strint(std::string &str) {
    int i;
    std::stringstream intstr(str);
    intstr >> i;
    return i;
}

int main () {
    std::string strdigit, schar;
    int sum = 0;
    std::cout << "Enter Digits: ";
    std::cin >> strdigit;
    std::stringstream ss;
    for (int i = 0; i < strdigit.length(); i++) {
        ss.clear();
        ss << strdigit[i];
        ss >> schar;
        sum += strint(schar);
    }
    std::cout << sum;
}

总和是:4479

You need to input that as a string. Split them, and convert each character to an integer. Add them up, and you're done.

Example, this number here (randomly generated):

9624526619162264306083309360203157186784123851390498919674886891002552146753945797326679482200717699585297042606470048297021049209667042255911984240697992738371633115195140494325737382583412562136836759072897211537655046343769659111215754043609344618490646811291135643554115350431099553593485744944746093896695837300975718819726339233383800764568364950577294931831936979504756278187812548901366714205562309364234394802723329400976924082450161974562063268243689930750925213262044910428021004262080895556879515597779404780565380480750286553508081070834339176079062215815331059349488936312244526697733596052063044560959189161656978673936732284706841120711543620038686227462170335634371808995466024671420024705248851244350701111587608201303840696489479021196275228499780922745352396928865910631672384263395712487735712098161853665189905194589355110620257494673972892816413534347360049692019184831019218764766067298983043791063184786671132332077197148683743991683245617836086353821268720434176862469084808

And here's the C++ program:

int strint(std::string &str) {
    int i;
    std::stringstream intstr(str);
    intstr >> i;
    return i;
}

int main () {
    std::string strdigit, schar;
    int sum = 0;
    std::cout << "Enter Digits: ";
    std::cin >> strdigit;
    std::stringstream ss;
    for (int i = 0; i < strdigit.length(); i++) {
        ss.clear();
        ss << strdigit[i];
        ss >> schar;
        sum += strint(schar);
    }
    std::cout << sum;
}

The sum is: 4479

只需将数字读入字符串并使用 std::accumulate 即可。例如:

std::string str("1234567890"); // your number here

int result = std::accumulate(str.begin(), str.end(), 0, [](int val, char ch)
{
    return val + (ch - '0');
});

std::cout << result << '\n'; // display the answer

Simply read the digits into a string and use std::accumulate. For example:

std::string str("1234567890"); // your number here

int result = std::accumulate(str.begin(), str.end(), 0, [](int val, char ch)
{
    return val + (ch - '0');
});

std::cout << result << '\n'; // display the answer
早茶月光 2024-10-12 23:24:20

您需要一个库来为此提供支持。

You need a library to provide support for this.

酷到爆炸 2024-10-12 23:24:20

将数字读入字符串并使用多精度数学库(例如 GMP 进行加法)。库应该具有在数字字符串和库的数字内部表示之间进行转换的函数。

(实际上,看起来 GMP 可以直接从 istream 读取数字,因此您甚至可能不需要字符串。)

Read the digits into a string and use a multiprecision math library such as GMP to do the addition. The library should have functions for converting between digit strings and the library's internal representation for numbers.

(Actually, it looks like GMP can read digits directly from an istream, so you may not even need a string.)

岁吢 2024-10-12 23:24:20

我对这个问题有点不清楚。如果您单独添加数字,那么我会认为您更多地将其视为字符串而不是整数(至少,直到您开始添加数字为止)。

你能解释一下1000位整数需要如何存储在内存中吗?

I'm a little unclear on the question. If you are adding the digits separately, then I would have thought you were treating it more as a string than an integer (at least, until you start adding up the digits).

Can you clarify how the 1,000-digit integer needs to be stored in memory?

夜巴黎 2024-10-12 23:24:20

正如马龙所建议的,为什么不简单地使用好的 'ol for 循环和字符串呢?

int main() {
        string str = "3985792792679283635";
        int len = str.length();
        int sum = 0;
        for(int i = 0; i < len; i++) {
                sum += str[i] - '0';
        }
        cout << sum << endl;

}

As marlon suggested, why not simply use good 'ol for loop and string?

int main() {
        string str = "3985792792679283635";
        int len = str.length();
        int sum = 0;
        for(int i = 0; i < len; i++) {
                sum += str[i] - '0';
        }
        cout << sum << endl;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文