爱问知识人 爱问教育 医院库

用c语言编写程序 输入一个正整数n

首页

用c语言编写程序 输入一个正整数n

用c语言编写程序 输入一个正整数n,求1到n的数中1出现的个数

提交回答
好评回答
  • 2008-11-14 15:14:00
    /*用c语言编写程序 输入一个正整数n,求1到n的数中1出现的个数*/#include"stdio.h"int count(int n){ int i,j,c=0; for(i=1;i<=n;i++) {  j=i;  while(j!=0)  {   if(i%10==1) /*判断位数中是否有1*/   {    c++;       }   j=j/10; /*让数除以十让他原来的高位变为低位,即原来的十位变成个位*/  } } return c;}main(){ int c,n; printf("input n:\n"); scanf("%d",&n); c=count(n); printf("%d\n",c);  }

    f***

    2008-11-14 15:14:00

其他答案

    2008-11-14 19:24:00
  • 现在这里站个位置,我吃了饭跟你编好

    y***

    2008-11-14 19:24:00

  • 2008-11-14 18:22:00
  •   #include <stdio。h>#include <string。h>int count_one(unsigned int n){ int cnt = 0; char buf[100]; for (int i = 1; i <= n; i++){  sprintf(buf, "%d", i);  for(int j = 0;j < strlen(buf); j++)if(buf[j] == '1')cnt++; } return cnt;}int main(){ printf("%d %d %d\n", count_one(1), count_one(100), count_one(1000)); return 0;}[41@~/c-cpp]$ gcc -Wall --std=c99 test。
      c[42@~/c-cpp]$ 。/a。out 1 21 301。

    h***

    2008-11-14 18:22:00

  • 2008-11-14 15:12:00
  • 不知道这个是不是你想要的#include"stdio.h"#include"string.h"int count(int num){ int count=0; while(num>0) {  if(num%10==1)//判断个位  count++;  num/=10;//去掉个位,得到下一个数 } return count;}void main(){ int num,i,sum=1; printf("Please insert the value of num:"); scanf("%d",&num); for(i=2;i<=num;i++)  sum+=count(i);//循环得到每一个数字中字符'1'的个数 printf("the tatol '1' is:%d\n",sum);}

    l***

    2008-11-14 15:12:00

类似问题

换一换

相关推荐

正在加载...
最新资料 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):