博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
十进制数1~n中1出现的次数
阅读量:6424 次
发布时间:2019-06-23

本文共 2211 字,大约阅读时间需要 7 分钟。

//copyright@ saturnman

//updated@ 2011 July
#include "stdafx.h"
#include "string.h"
#include "stdlib.h"

int NumberOf1(const char* strN);

int PowerBase10(unsigned int n);

/

// Find the number of 1 in an integer with radix 10
// Input: n - an integer
// Output: the number of 1 in n with radix
/
int NumberOf1BeforeBetween1AndN_Solution2(int n)
{
if(n <= 0)
return 0;

// convert the integer into a string

char strN[50];
sprintf(strN, "%d", n);

return NumberOf1(strN);

}

/
// Find the number of 1 in an integer with radix 10
// Input: strN - a string, which represents an integer
// Output: the number of 1 in n with radix
/
int NumberOf1(const char* strN)
{
if(!strN || *strN < '0' || *strN > '9' || *strN == '\0')
return 0;

int firstDigit = *strN - '0';

unsigned int length = static_cast<unsigned int>(strlen(strN));

// the integer contains only one digit

if(length == 1 && firstDigit == 0)
return 0;

if(length == 1 && firstDigit > 0)

return 1;

// suppose the integer is 21345

// numFirstDigit is the number of 1 of 10000-19999 due to the first digit
int numFirstDigit = 0;
// numOtherDigits is the number of 1 01346-21345 due to all digits
// except the first one
int numOtherDigits = firstDigit * (length - 1) * PowerBase10(length - 2);
// numRecursive is the number of 1 of integer 1345
int numRecursive = NumberOf1(strN + 1);

// if the first digit is greater than 1, suppose in integer 21345

// number of 1 due to the first digit is 10^4. It's 10000-19999
if(firstDigit > 1)
numFirstDigit = PowerBase10(length - 1);

// if the first digit equals to 1, suppose in integer 12345

// number of 1 due to the first digit is 2346. It's 10000-12345
else if(firstDigit == 1)
numFirstDigit = atoi(strN + 1) + 1;

return numFirstDigit + numOtherDigits + numRecursive;

}

/

// Calculate 10^n
/
int PowerBase10(unsigned int n)
{
int result = 1;
for(unsigned int i = 0; i < n; ++ i)
result *= 10;

return result;

}
int num1Ofn(int n)
{
int count=0;
while(n>0)
{
if(n%10 == 1)
count++;
n/=10;
}
return count;
}
int count1Num(int n)
{
int sum=0;
for (int i =1;i <= n;i ++)
{
sum+=num1Ofn(i);
}
return sum;
}
int main()
{
printf("%d\n", count1Num(21345));
printf("%d\n", NumberOf1BeforeBetween1AndN_Solution2(21345));
return 0;
}

转载地址:http://kzyga.baihongyu.com/

你可能感兴趣的文章
Spring Data Redis—Pub/Sub(附Web项目源码)
查看>>
RSD和wlwmanifest是什么
查看>>
Linkedin工程师是如何优化他们的Java代码的(转)
查看>>
winfrom 如何保存datagridview中的某一行数据
查看>>
面向领域驱动的应用开发框架Apworks 2.0发布
查看>>
开发自己的Web服务处理程序(以支持Ajax框架异步调用Web服务方法)
查看>>
ref和out
查看>>
黑客教父详解账号泄露全过程:1亿用户已泄露
查看>>
程序员必须软件
查看>>
Canvas里的globalCompositeOperation
查看>>
解决Unable to locate theme engine in module_path: "pixmap"
查看>>
贝叶斯文本分类c#版
查看>>
Centos安装KDE或GNOME
查看>>
Eclipse & IDEA 中常用的快捷键
查看>>
javascript ---IPhone滑动解锁
查看>>
table固定行和表头
查看>>
<每天读一点职场心理学>读书笔记
查看>>
Android权限大全代码
查看>>
android 判断SIM卡是哪个运营商
查看>>
删除N天前的M(天)个目录 、删除N天前最后修改的文件 ForFiles, dos command 批处理命令cmd/bat...
查看>>