首页 > 试题广场 >

包含数字9的数

[编程题]包含数字9的数
  • 热度指数:31484 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

今年是2019年,KiKi想知道1~2019中有多少个包含数字9的数。包含数字的数是指有某一位是“9”的数,例如“2019”、“199”等。


输入描述:


输出描述:
一行,一个整数,表示1~2019中共有多少个数包含数字9。
#include <stdio.h>

int main() 
{
    int count = 0;
	for (int i = 1; i <= 2019; i++)
	{
		int ret = i;	//i的值不能变
		while (ret)
		{
			if (ret % 10 == 9)	//对每位数检查,有一位为9即可
			{
				count++;
				break;
			}
			ret /= 10;	//去掉低位
		}
	}
	printf("%d\n", count);
    return 0;
}

发表于 2024-05-11 19:16:54 回复(0)
#include <stdio.h>

int main()
{
    int a = 2019, count = 0, j;

    for (int i = 1; i <= a; i++)
    {
        j = i;
        while (j != 0)
        {
            if (j % 10 == 9)
            {
                count++;
                break;
            }
            j = j / 10;

        }
    }

    printf("%d", count);

    return 0;
}
发表于 2024-04-30 23:01:48 回复(0)
#include <stdio.h>

int main() {
    int tag=0, a[3]={0};
    for(int i=1; i<=2019; i++){
        a[2] = i%10;
        a[1] = (i/10)%10;
        a[0] = (i/100)%10;
        if(a[0]==9 || a[1]==9 || a[2]==9){
            tag++;
        }
    }
    printf("%d", tag);
    return 0;
}
发表于 2024-01-20 11:20:48 回复(0)
#include <stdio.h>

int main() {
    int i, count = 0;

    for (i = 0; i <= 2019; i++) {
        int m = i;
        while (m) {
            if (m % 10 == 9) {
                count++;
                break;
            }
            m /= 10;
        }
    }
    printf("%d\n", count);
    return 0;
}

发表于 2024-01-17 22:18:12 回复(0)
编辑于 2023-12-14 11:13:52 回复(0)
#include<stdio.h>

int main(void){
    int x=0;
    for(int i=1;i<=2019;i++){
        if(i%10==9 || i/100%10==9 || i%100/10==9){
            x++;
        }
    }
    printf("%d",x);
}

发表于 2023-11-26 16:12:41 回复(0)
#include <stdio.h>

int main()
 {
   int count = 0,temp = 0;
   int i = 0;
   for(i = 1 ; i <= 2019;i++)
   {
       temp = i;
       while(temp)
       {
        if(temp % 10 == 9)
        {
            count++;
            break;
        }
        temp /= 10;
       }

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

发表于 2023-06-27 18:59:46 回复(0)
#include<stdio.h>
int main() {
    int temp = 0,count = 0;
    for (int i = 1; i <= 2019; i++) {
        temp = i;
        while (temp) {
            if (temp % 10 == 9) {
                count++;
                break;//防止temp出现多个9,只需要记录一次
            }
            temp /= 10;
        }
    }
    printf("%d", count);
    return 0;
}

发表于 2023-04-04 16:20:23 回复(0)
#include <stdio.h>

int main() {
    int i = 0, count = 0;
    for (i = 1; i <= 2019; i++) {
        if (i % 10 == 9 || i / 10 == 9 || i % 100 == 9 || (i / 10) % 10 == 9 ||
                i / 100 == 9
                || i % 1000 == 9 || (i / 10) % 100 == 9 || (i / 100) % 10 == 9) {
            count++;
        }
    }
    printf("%d\n", count);
    return 0;
}

发表于 2023-03-19 16:13:59 回复(0)
我想问一下为什么第11行要  int a =  i; 才能进行while循环,我直接带i进while循环运行是没东西的。
#include <stdio.h>

int main()
{
    int a = 0;
    int i = 0;
    int count = 0;
   
    for (i = 1; i <= 2019; i++)
    {
       // int a = i;
        while(i>0)
        {

            if (i % 10 == 9)
            {
                count++;
                break;
            }
            else
            {
                i/= 10;//i=i/10
            }
         
               
        }
       
    }
    printf("%d",count);

}

发表于 2023-03-04 14:41:57 回复(0)
#include <stdio.h>

int main() {
    int i = 0;
    int count = 0;
    for(i = 1; i < 2020; i++)
    {
        if(i % 10 == 9)
        {
            count++;
        }
        else if(i / 10 % 10 == 9)
        {
            count++;
        }
        else if(i / 100 % 10 == 9)
        {
            count++;
        }
    }
    printf("%d", count);

    return 0;
}
发表于 2023-02-28 10:57:02 回复(0)
#include <stdio.h>

int main() {
    int count=0,i=1;
    while(i!=0&&i<=2019)
    {
        if(i%10==9||i/10%10==9||i/100%10==9)
        {
            count++;
        }
        i++;
    }
    printf("%d\n",count);
    return 0;
}

发表于 2022-11-10 20:06:31 回复(0)
#include <stdio.h>

int main() {
    int count=0;
    for(int i=1;i<=2019;i++)
    {
        int j=i;
        while(j)
        {
            if(j%10==9)
            {
                count++;
                break;
            }
            j/=10;
        }
    }
    printf("%d",count);
    return 0;
}

发表于 2022-10-24 22:01:21 回复(0)
发表于 2022-09-06 02:46:09 回复(0)
int main(){
    int count =0;
    for(int i=1;i<=2019;i++){
        if(i%10==9||i/10%10==9||i/100%10==9){
            count++;
        }
    }
    printf("%d",count);
    return 0;
}

发表于 2022-08-30 22:05:02 回复(0)
#include <stdio.h>
#define end 2019
int main(){
    int num = 0, digit;
    for(int i = 1; i <= end; i++){
        int temp = i;
        while(temp >= 9){
            digit = temp % 10;
            if(digit != 9){
                temp /= 10;
            } else {
                num++;
                break;
            }
        }
    }
    printf("%d\n",num);
    return 0;
}

发表于 2022-08-16 10:03:43 回复(0)
刚开始直接使用num了
#include<stdio.h>
int main ()
{
    int count = 0;
    int num = 1;
    for(num = 1 ;num <=2019;num++)
    {
        int m = num;//不能直接用num不然num永远到不了2019
        while(m)
        {
            if(m % 10 == 9)
            {
                count++;
                break;
            }
            else
            {
                m /= 10;
            }
        }
    }
    printf("%d",count);
    return 0;
}

发表于 2022-08-15 12:12:07 回复(0)
#include<stdio.h>
int main() {
    int sum = 0, a, i = 1, n;
    while (i <= 2019) {
        n = i;
        while (n > 0) {
            if (n % 10 == 9) {
                sum += 1;
                break;
            }
            n = n / 10;
        }
        i++;
    }
    printf("%d", sum);
}
发表于 2022-07-20 15:09:12 回复(0)

问题信息

上传者:牛客309119号
难度:
38条回答 3158浏览

热门推荐

通过挑战的用户

查看代码