题目描述
ACM俱乐部里的那些无聊家伙经常举行数数比赛- -。
比赛规则就是对于一个给出的正整数n,把1到n的正整数写在纸上,然后数里面数字1被写出来的次数。
输入格式
输入有多组数据。
每组数据一行,包含一个正整数n(小于等于2^26)。
输出
对应每组数据,输出所求的1的出现次数。
样例输入
11
20
样例输出
4
12
递归版1:
#include <stdio.h>
#include <stdlib.h>
int d[11];
int value;
void deal(int n){
if(n<=0)return;
int one,ten;
one=n%10;
n/=10;
ten=n;
for(int i=0;i<=one;i++)d[i]+=value;
while(ten){
d[ten%10]+=(one+1)*value;
ten/=10;
}
for(int i=0;i<10;i++)
d[i]+=value*n;
d[0]-=value;
value*=10;
deal(n-1);
}
int main() {
freopen("C:\\in.txt","r",stdin);
char str[50];
int n;
while(~scanf("%d",&n)){
for(int i=0;i<10;i++)d[i]=0;
value=1;
deal(n);
printf("%d\n",d[1]);
}
return 0;
}
题目些许变化,输入a,b输出a,b之间1的数目
递归版2:
#include "iostream"
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <functional>
using namespace std;
int NumberOf1(const char* strN);
int PowerBase10(unsigned int n);
int NumberOf1Between1AndN_Solution2(int n)
{
if(n <= 0)
return 0;
char strN[50];
sprintf(strN, "%d", n);
return NumberOf1(strN);
}
int NumberOf1(const char* strN)
{
if(!strN || *strN < '0' || *strN > '9' || *strN == '\0')
return 0;
int first = *strN - '0';
unsigned int length = static_cast<unsigned int>(strlen(strN));
if(length == 1 && first == 0)
return 0;
if(length == 1 && first > 0)
return 1;
// 假设strN是"21345"
// numFirstDigit是数字10000-19999的第一个位中1的数目
int numFirstDigit = 0;
if(first > 1)
numFirstDigit = PowerBase10(length - 1);
else if(first == 1)
numFirstDigit = atoi(strN + 1) + 1;
// numOtherDigits是01346-21345除了第一位之外的数位中1的数目
int numOtherDigits = first * (length - 1) * PowerBase10(length - 2);
// numRecursive是1-1345中1的数目
int numRecursive = NumberOf1(strN + 1);
return numFirstDigit + numOtherDigits + numRecursive;
}
int PowerBase10(unsigned int n)
{
int result = 1;
for(unsigned int i = 0; i < n; ++ i)
result *= 10;
return result;
}
int main(){
freopen("C:\\in.txt","r",stdin);
int a,b;
while(cin>>a>>b){
if(a>b)swap(a,b);
cout<<NumberOf1Between1AndN_Solution2(b)-NumberOf1Between1AndN_Solution2(a==0?0:a-1)<<endl;
}
return 0;
}
迭代版:
#include "iostream"
#include <fstream>
#include <algorithm>
#include <functional>
using namespace std;
/*
Suppose we have N=ABCDEFG.
if G<1, # of 1’s in the units digits is ABCDEF, else ABCDEF+1
if F<1, # of 1’s in the digit of tens is (ABCDE)*10, else if F==1: (ABCDE)*10+G+1, else (ABCDE+1)*10
if E<1, # of 1’s in 3rd digit is (ABCD)*100, else if E==1: (ABCD)*100+FG+1, else (ABCD+1)*100
… so on.
if A=1, # of 1 in this digit is BCDEFG+1, else it’s 1*1000000;
so to fast access the digits and helper numbers, we need to build the fast access table of prefixes and suffixes.
*/
int countOf1s(int n) {
int prefix[10], suffix[10], digit[10]; //10 is enough for 32bit integers
int i=0;
int base = 1;
while (base <= n) {
suffix[i] = n % base;
digit[i] = (n % (base * 10))/base;
prefix[i] = (n - suffix[i] - digit[i]*base)/10;
i++, base*=10;
}
int count = 0;
base = 1;
for (int j=0; j<i; j++) {
if (digit[j] < 1)
count += prefix[j];
else if (digit[j]==1)
count += prefix[j] + suffix[j] + 1;
else
count += prefix[j]+base;
base *= 10;
}
return count;
}
int main(){
freopen("C:\\in.txt","r",stdin);
int a,b;
while(cin>>a>>b){
if(a>b)swap(a,b);
cout<<countOf1s(b)-countOf1s(a==0?0:a-1)<<endl;
}
return 0;
}
简洁迭代版:
#include <stdio.h>
typedef long long LL;
LL Sum1s(LL n){
LL iCount=0;
LL iFactor=1;
LL iLowerNum=0;
LL iCurrNum=0;
LL iHigherNum=0;
while(n/iFactor!=0){
iLowerNum = n-(n/iFactor)*iFactor;
iCurrNum = (n/iFactor)%10;
iHigherNum = n/(iFactor*10);
switch(iCurrNum){
case 0:
iCount+=iHigherNum*iFactor;
break;
case 1:
iCount+=iHigherNum*iFactor+iLowerNum+1;
break;
default:
iCount+=(iHigherNum+1)*iFactor;
break;
}
iFactor*=10;
}
return iCount;
}
int main(){
printf("%ld\n",Sum1s(12));
return 0;
}