数一下 1到 100 的所有整数中出现多少次数字9

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
int main()
{  
 //数一下 1到 100 的所有整数中出现多少次数字9
 //100中不包含9,所以我们只需要看十位和各位是否包含9
 //该数关于10求余,其模为9则个位包含9;该数除以十,其商为9则十位包含9
 int num;
 int count=0;
 for (num = 1; num < 100; num++)
 {
  int a = num % 10;
  int b = num / 10;
  if (a == 9)
  {
   count++;
  }
  if (b == 9)
  {
   count++;
  }
 }
 printf("出现9的次数=%d\n", count);
  system("pause");
  return 0;
 }

猜你喜欢

转载自blog.csdn.net/weixin_43240245/article/details/82934586
今日推荐