通过编程实现,统计1~n有多少个9 提示:n通过参数传入

来源:互联网 发布:超少年密码之人工智能 编辑:程序博客网 时间:2024/06/05 07:13
#include <stdio.h>int fun(int n){    int i;    int tmp;    int k;    int count = 0;    for(i=1;i<=n;i++)    {        tmp = i;        while(tmp != 0)        {            if(tmp == 9 || k == 9)            {                count++;            }            k = tmp % 10;            tmp = tmp / 10;        }    }    return (count-1);}int main(){    int num;    int count = 0;    printf("Please input n:");    scanf("%d",&num);    count = fun(num);    printf("the number of 9 between 1 and %d is:%d\n",num,count);    return 0;}
0 0