📄 count.txt
字号:
问题描述:
一本书的页码从自然数1开始顺序编码直到自然数n。 书的页码按照通常的习惯编排,
每个页码都不含多余的前导数字0。例如第6页用数字6表示,而不是006,06等。数字技术问
题要求对于给定的书的总页码n,计算出书的全部页码中分别用到多少数字0,1,2,3,4,
5,6,7,8?
// Count the digital in the number from 0 to n.
void CountFromLeft( int n = 0 )
{
unsigned long countArray[10] = {0};
// 10 for integer, one fore '\0'
char maxNumber[11] = {0};
if( n > 0 )
{
sprintf( maxNumber, "%d", n );
int cursor = 0;
int numberCount = 0;
while( maxNumber[ cursor ] != '\0' )
{
// Add one digital at the end, the count of the
// number will be about 10 double. So the count of
// left digital will be approximately 10 times.
for( int i=0; i<10; i++ )
{
countArray[i] *= 10;
}
// But not all
// For 2=>23, '1' 10 double, '2' only 3+1,
// For 2=>29, '2' will 10 dobule too.
for( int j=0; j<cursor; j++ )
{
countArray[ maxNumber[ j ] - '0' ]
-= '9' - maxNumber[ cursor ];
}
// There are some new digital for the last digit.
// Add one for each digital.
for( int i=0; i<10; i++ )
{
countArray[i] += numberCount;
}
// Not all again.
for( int i=1; i<=( maxNumber[ cursor ] - '0' ); i++)
{
countArray[i]++;
}
// Get the count for previous part.
numberCount *= 10;
numberCount += maxNumber[ cursor ] - '0';
cursor++;
}
}
// Output the result
cout << "Each digital:\n";
int Total = 0;
for( int i=0; i<10; i++ )
{
cout << "\t[" << i << "]: " << countArray[i] << "\n";
Total += countArray[i];
}
cout << "Total: " << Total << "\n";
}
从右到左的
这里的思路是如果你拿到的参数是103
你先算从1到3有多少个数字
再算03,因为03和3是一样的,所以这个地方就不加了
然后算03到103增加了多少
// Count the digital in the number from 0 to n.
void CountFromRight( int n = 0 )
{
unsigned long countArray[10] = {0};
unsigned int power[10] = {1,10,100,1000,10000,100000,1000000,10000000,1000
00000,1000000000};
int currentValue = n;
int currentPos = 0;
int addedNumber = 0;
// Initialize here only for security, you can remove next two lines.
int nextValue = 0;
int currentDigital = 0;
while( currentValue > 0 )
{
nextValue = currentValue / 10;
currentDigital = currentValue - nextValue * 10;
int currentPower = 0;
// Add the number of every digital( 1/10 of added number by this digit
)
if( currentPos > 0 )
{
currentPower = currentDigital * currentPos * power[ currentPos - 1
];
for( int j=0; j<10; j++)
{
countArray[ j ] += currentPower;
}
// not all
if( addedNumber < power[ currentPos - 1 ] - 1 )
{
countArray[ 0 ] -= power[ currentPos - 1 ] - 1 - addedNumber;
}
}
// Add the number of the less digital at top digit.
currentPower = power[ currentPos ];
for( int j=1; j<currentDigital; j++)
{
countArray[ j ] += currentPower;
}
// Add the number of current digital at top digit.
// You can move the last for into this if
if( currentDigital > 0 )
{
countArray[ currentDigital ] += addedNumber + 1;
}
currentValue = nextValue;
addedNumber += currentDigital * power[ currentPos ];
currentPos++;
}
// Output the result
cout << "Each digital:\n";
int Total = 0;
for( int i=0; i<10; i++ )
{
cout << "\t[" << i << "]: " << countArray[i] << "\n";
Total += countArray[i];
}
cout << "Total: " << Total << "\n";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -