今年是2019年,KiKi想知道1~2019中有多少个包含数字9的数。包含数字的数是指有某一位是“9”的数,例如“2019”、“199”等。
#include <stdio.h>
int main()
{
int count = 0;
int num=2019;
//scanf("%d", &num);
for (int i = 0; i <= num; i++)
{
if (i % 10 == 9)
{
count++;
}
else if ((i / 10) % 10 == 9)
{
count++;
}
else if ((i / 100) % 10 == 9)
{
count++;
}
else if ((i / 1000) % 10 == 9)
count++;
}
printf("%d\n", count++);
return 0;
} #include <stdio.h>
int main() {
int a = 0, b = 9;
for (int i = 90; i <= 2019; i++) {
a = i;
while (a) {
if (a % 10 == 9) {
b++;
break;
}
a = a / 10;
}
}
printf("%d", b);
return 0;
} int main() {
int a = 0, b = 0, c = 0, d = 0, n = 0;
while (a * 1000 + b * 100 + c * 10 + d <= 2019) {
d++;
if (d == 10) {
c++;
d = 0;
if (c == 10){
b++;
c = 0;
if (b == 10) {
a++;
b = 0;
}
}
}
if (b == 9 || c == 9 || d == 9) {
n++;
}
}
printf("%d", n);
return 0;
} /*思路 : 首先想到使用数组的方式来实现
先遍历循环1-2019 然后将这些数据存放在数组里面
然后使用 == 来判断哪些
但是后来又想到了一个for循环的方式
先1-10 10-100 101-1000 1001-2000 2001-2019
因为只有一个数的末尾才包含数字9*/
/*
# include <stdio.h>
int main ()
{
int count = 0;
for (int i = 1; i<=2019; i++)
{
if (i%9==0)
{
count ++;
}
}
printf("%d",count);
return 0;
}
//error 看错题目了...... 看成题目要求多少个能被9整除的数字了......
*/
# include <stdio.h>
int main ()
{
int count = 0;
for (int i = 1; i<=2019; i++)
{
//个位、十位、百位取余结果若为9,则是我们需求的数字
if (i%10==9||(i/10)%10==9||(i/100)%10==9)
{
count ++;
}
}
printf("%d",count);
return 0;
}
#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;
}