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

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

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

C语言求整数中1出现的次数(从1~n的整数)

解题思路

直接写一个遍历从1~n然后计算每个数字中1的个数计算和

 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param n int整型 
 * @return int整型
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */
//计算数字含有多少1
int numof_1(int num){
    int count=0;
    while(num>0){
        if(num%10==1)
            count++;
        num=num/10;
    }
    return count;
}
int NumberOf1Between1AndN_Solution(int n ) {
    // write code here
    int count=0;
    for(int i=0;i<=n;i++){
        count+=numof_1(i);
    }
    return count;
}
全部评论

相关推荐

2025-12-17 17:15
华东师范大学 运营
点赞 评论 收藏
分享
做黑夜里的那道光:两年电赛完赛没必要写,纯扣分
双非本科求职如何逆袭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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