题解 | #整数中1出现的次数(从1到n整数中1出现的次数)#

整数中1出现的次数(从1到n整数中1出现的次数)

http://www.nowcoder.com/practice/bd7f978302044eee894445e244c7eee6

贴一个C++版代码
class Solution {
public:
int NumberOf1Between1AndN_Solution(int n) {
int base=1;
int res=0;
while(base<=n){
int cur=n/base%10;
int a=n/base/10;
int b=n%base;
//分三种情况讨论
if(cur==1){
res+=abase+b+1;
}else if(cur==0){
res+=abase;
}else{
res+=(a+1)
base;
}
base
=10;
}
return res;
}
};

全部评论

相关推荐

03-04 07:14
门头沟学院 C++
黑皮白袜臭脚体育生:老板:都给工作机会了还想要工资,哪来这么多好事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务