在 C++ 中一次获取大整数输入
我想一次输入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要将其作为字符串输入。拆分它们,并将每个字符转换为整数。将它们加起来,就完成了。
例如,这里的数字(随机生成):
这是 C++ 程序:
总和是:
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):
And here's the C++ program:
The sum is:
4479
只需将数字读入字符串并使用 std::accumulate 即可。例如:
Simply read the digits into a string and use std::accumulate. For example:
您需要一个库来为此提供支持。
You need a library to provide support for this.
将数字读入字符串并使用多精度数学库(例如 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.)我对这个问题有点不清楚。如果您单独添加数字,那么我会认为您更多地将其视为字符串而不是整数(至少,直到您开始添加数字为止)。
你能解释一下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?
正如马龙所建议的,为什么不简单地使用好的 'ol for 循环和字符串呢?
As marlon suggested, why not simply use good 'ol for loop and string?