您的位置:首页 > 移动开发 > 微信开发

通过编程实现,统计1~n有多少个9和输入一个整数再输入两个整数输出该整数的二进制表示方法中从右端开始的1到2位.(小程序)

2019-01-25 20:29 323 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_44571183/article/details/86651558
  1. 通过编程实现,统计1~n有多少个9:
#include<stdio.h>
int main()
{
int n,i;            //n,i为变量
int count = 0;      //count计数
int temp1,temp2;
printf("please input number\n");
scanf ("%d",&n);
for (i=1;i<=n;i++)
{
temp1 = i;
while (temp1 != 0)
{
temp2 = temp1 % 10;
temp1 =temp1 / 10;
if (temp2 ==9)
{
count +=1;
}
}
}
printf("total is %d\n",count);
return 0;
}
  1. 输入一个整数a,再输入两个整数p1,p2(p1,p2<32),输出该整数的二进制表示方法中从右端开始的p1到p2位.:
#include<stdio.h>
int main()
{
int p1,p2,i,n;   //n为该整数,i为变量
int j=0;         //为j赋值0
printf("please input two num p1and p2(p1<p2)and n..\n");
scanf("%d%d%d",&p1,&p2,&n);
for (i=p1;i<=p2;i++)
{
j=(n>>(i-1))&1;
if(j ==1)
{
printf("1")
}
if(j == 0)
{
printf("0");
}
}
return 0;
}

以上两个程序均运行过,结果正确(当然不排除输入进csdn时出现错误,不过目测没问题)
2019年1月25日晚

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