0-100有多少个9?

0-100有多少个9?

20个

  • 我的代码

#include <cstdio.h>
int get_hund()
{
    int total=0;
    int i;
    for(i=1;i<100;i++)
    {
        if(i%10==9)
        {
            total++;
        }
        if(i/10==9)
        {
            total++;
        }
    }
    return total;
}

int main()
{
    int get_hund();
    printf("%d\n",get_hund());
    return 0;
}
  • 涛哥代码

#include <cstdio.h>
int get_one(int n)
{
    int total=0;
    while(n>0)
    {
        if(n%10==9)
        {
            total++;
        }
        n=n/10;
    }
    return total;
}

int main()
{
    int total=0;
    int i;
    for(i=0;i<100;i++)
    {
        total+=get_one(i);
    }

    printf("%d\n",total);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42420263/article/details/86539584
今日推荐