您的位置:首页 > 其它

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

2016-05-17 09:17 337 查看
输入一个整数 n,求从 1 到 n 这 n 个整数的十进制表示中 1 出现的次数。

例如输入 12,从 1 到 12 这些整数中包含 1 的数字有 1, 10, 1 1 和 12, 1 一共出现了 5 次

#include <stdio.h>
#include <stdlib.h>

int count1(int n);
int count2(int n);

int main(void)
{
int x;

printf("输入一个数:");
scanf("%d",&x);
printf("\n从0到%d一共遇到%d(%d)个1\n",x,count1(x),count2(x));

return 0;
}

//解法一
int count1(int n)
{
int count = 0;
int i,t;

//遍历1到n
for(i=1;i<=n;i++)
{
t=i;
//依次处理当前遍历到的数字的各个位
while(t != 0)
{
//若为1则统计加一
count += (t%10 == 1)?1:0;
t/=10;
}
}

return count;
}

//解法二:
int count2(int n)
{
int count = 0;//统计变量
int factor = 1;//分解因子
int lower = 0;//当前处理位的所有低位
int higher = 0;//当前处理位的所有高位
int curr =0;//当前处理位

while(n/factor != 0)
{
lower = n - n/factor*factor;//求得低位
curr = (n/factor)%10;//求当前位
higher = n/(factor*10);//求高位

switch(curr)
{
case 0:
count += higher * factor;
break;
case 1:
count += higher * factor + lower + 1;
break;
default:
count += (higher+1)*factor;
}

factor *= 10;
}

return count;
}


方法一就是从1开始遍历到N,将其中的每一个数中含有“1”的个数加起来,比较好想。

方法二比较有意思,核心思路是这样的:统计每一位上可能出现1的次数。

比如123:

个位出现1的数字:1,11,13,21,31,...,91,101,111,121

十位出现1的数字:10~19,110~119

百位出现1的数字:100~123

总结其中每位上1出现的规律即可得到方法二。其时间复杂度为O(Len),Len为数字长度

整数的二进制表示中 1 的个数

题目:整数的二进制表示中 1 的个数

要求:

输入一个整数,求该整数的二进制表达中有多少个 1。

例如输入 10,由于其二进制表示为 1010,有两个 1,因此输出 2。

分析:

解法一是普通处理方式,通过除二余二统计1的个数;

解法二与解法一类似,通过向右位移依次处理,每次与1按位与统计1的个数

解法三比较奇妙,每次将数字的最后一位处理成0,统计处理的次数,进而统计1的个数

代码实现(GCC编译通过):

#include <stdio.h>
#include <stdlib.h>

int count1(int x);
int count2(int x);
int count3(int x);

int main(void)
{
int x;
printf("输入一个数:\n");
setbuf(stdin,NULL);
scanf("%d",&x);
printf("%d转二进制中1的个数是:",x);
printf("\n解法一:%d",count1(x));
printf("\n解法二:%d",count2(x));
printf("\n解法三:%d",count3(x));
printf("\n");
return 0;
}

//除二、余二依次统计每位
int count1(int x)
{
int c=0;
while(x)
{
if(x%2==1)
c++;
x/=2;
}
return c;
}

//向右移位,与1按位与统计每位
int count2(int x)
{
int c=0;
while(x)
{
c+=x & 0x1;
x>>=1;
}
return c;
}

//每次将最后一个1处理成0,统计处理次数
int count3(int x)
{
int c=0;
while(x)
{
x&=(x-1);
c++;
}
return c;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: