您的位置:首页 > 理论基础 > 数据结构算法

我思故我在系列—数据结构面试NO.30题(题目搜集整理者JULY,非常感谢!!)

2011-11-04 15:27 330 查看
30.

在从1 到n的正数中1 出现的次数

题目:输入一个整数n,求从1 到n 这n 个整数的十进制表示中1 出现的次数。

从1到n进行for()循环,将每个整数中出现的1的次数求总和;

#include<stdio.h>

#include<stdlib.h>

int everycount(int num)

{

int count=0;

while(num!=0)

{

if((num%10)==1)

{

count++;

}

num=num/10;

}

return count;

}

int Numcount(int sum)

{

int num=0;

int i;

for(i=1;i<=sum;i++)

{

num+=everycount(i);

}

return num;

}

int main(int argc,char* argv[])

{

int times;

times=Numcount(100);

printf("the times of 1 is:%d",times);

return 1;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐