今年是2019年,KiKi想知道1~2019中有多少个包含数字9的数。包含数字的数是指有某一位是“9”的数,例如“2019”、“199”等。
#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; }
#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; }
#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; }
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; }