更新时间:2023-02-10 17:34:54
由于大多数计算机无法容纳1000个数字的整数,因此您将不得不对输入进行字符串操作或使用 Big数字库.让我们尝试前者.
Since most computers can't hold an integer that is 1000 digits, you will either have to operate on the input as a string or use a Big Number library. Let's try the former.
当将输入视为字符串时,每个数字都是一个介于'0'
到'9'
(含)范围内的字符.
When treating the input as a string, each digit is a character in the range of '0'
to '9'
, inclusive.
因此,这归结为计数字符:
So, this boils down to counting characters:
std::string text;
cin >> text;
const unsigned int length = text.size();
unsigned int digit_count = 0;
for (i = 0; i < length; ++i)
{
if (!std::isdigit(text[i]))
{
break;
}
++digit_count;
}
cout << "text has " << digit_count << "digits\n";