c++ 中长数字的分隔数字

发布于 2024-10-05 13:20:02 字数 187 浏览 1 评论 0原文

例如,我有一个很长的数字 12345678901,我想单独获取每个数字来使用它。我真的很努力了,但还是没成功?有什么想法吗?

但我在所有这些方面都有问题 当我尝试使用 11 位或更多数字(我想要的)时,我的程序停止工作 我正在 Visual Studio 中运行我的程序 在其他情况下——较小的数字——就很好了.. 和我的号码很长有什么关系吗?

I have for example the long number 12345678901 and I want to get separately each digit to use it. I tried really hard but I didn't make it so far? Any ideas?

but I have a problem in all of them
when I try those with a number of 11 digits and more (that what I want) my program stops working
I'm running my program in visual studio
in other case-smaller numbers--is just fine..
any connection with the fact that my number is long?

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

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

发布评论

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

评论(5

你不是我要的菜∠ 2024-10-12 13:20:03
std::vector<int> digits;

while(number > 0)
{
   digits.push_back(number%10); //push the last digit in
   number /= 10; //truncate the digit
}

std::reverse(digits.begin(), digits.end()); // the digits were in reverse order
std::vector<int> digits;

while(number > 0)
{
   digits.push_back(number%10); //push the last digit in
   number /= 10; //truncate the digit
}

std::reverse(digits.begin(), digits.end()); // the digits were in reverse order
偏爱你一生 2024-10-12 13:20:03

这将为您提供变量 b 中的数字。

long a = 12345678901;
while(a > 0) {
   long b = a % 10;
   a /= 10;
}

This will give you digits in variable b.

long a = 12345678901;
while(a > 0) {
   long b = a % 10;
   a /= 10;
}
我爱人 2024-10-12 13:20:03

一种方法是将数字转换为字符串(不确定该方法是什么,但我知道存在这样的东西),然后一次访问字符串的每个字符。

One way would be to convert the number to a string (not sure what the method is for that but I know such things exist) and then access each character of the string one at a time.

坐在坟头思考人生 2024-10-12 13:20:03
long residual= number;
int base= 10;

do
{
    long digit= residual%base;
    std::cout << digit << '\n';
    residual/= base;
} while (residual!=0);
long residual= number;
int base= 10;

do
{
    long digit= residual%base;
    std::cout << digit << '\n';
    residual/= base;
} while (residual!=0);
友谊不毕业 2024-10-12 13:20:03

您可以通过将其转换为字符串来获得您想要的内容,而不是计算数字:

// This converts the binary representation of the long into a string.
std::stringstream ss;
ss << long_number;
std::string number_as_string = ss.str();

// Then visit all the characters of the string.
for (std::string::const_iterator it = number_as_string.begin();
     it != number_as_string.end();
     ++it)
{
    std::cout << *it << std::endl;
}

这将打印数字,例如,如果您有 12345:

1
2
3
4
5

因此您可以访问迭代器 *it 如上面的代码。

Instead of calculating the digits, you can get what you want by converting it into a string:

// This converts the binary representation of the long into a string.
std::stringstream ss;
ss << long_number;
std::string number_as_string = ss.str();

// Then visit all the characters of the string.
for (std::string::const_iterator it = number_as_string.begin();
     it != number_as_string.end();
     ++it)
{
    std::cout << *it << std::endl;
}

This will print the numbers, say, if you have 12345:

1
2
3
4
5

So you can process each of them accessing the iterator *it as the above code.

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