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;
}