幸运数字(C++、C语言)

幸运数字
难度:初阶
时间限制:1000ms
内存限制:128mb
题目描述
小T去某大型超市购物时,由于正好是当天第2333个买单的顾客,因此得到了一张刮奖券。
奖券内印有若干个号码,在刮开这些号码前小T可以在19中选择一个自己的幸运数字,然后与前面这些号码进行比对。其中包含幸运数字或者正好是幸运数字的整数倍的号码就成为幸运号码,出现的幸运号码越多,则对应获得的奖项也越大。
请你帮小T计算下他选择的幸运数字可以相应地获得多少个幸运号码。
输入格式
第一行是奖券上的号码个数n。
第二行输入n个号码ai,表示第i个号码。
第三行是小T选择的幸运数字(1
9)。
输出格式
一个整数,表示出现幸运数字的个数。

img

满足题目意思的就是幸运数字了,包含幸运数字或者能整除幸运数字。
当前数对幸运数字取%判断是否为0,如果为0,则是幸运数字,否则进行判断各个位数是否等于幸运数字。

#include <stdio.h>
int main() 
{
  int n, x,nums[1000];
  int count=0;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    scanf("%d", &nums[i]);
  }
  scanf("%d", &x);
 
  for(int i = 0; i < n; i++) {
    if(nums[i] % x == 0) {
      count++;
    }else{
       int temp=nums[i];
       while(temp != 0) {
           temp=nums[i]%10;
         if(x == temp) {
            count++;
            break;
         }
         nums[i]/= 10;
       }
    }
  }
   printf("%d",count);
  return 0;
}

img


#include <stdio.h>
int main()
{
    int n, lucky,a[10000],t;
    scanf("%d",&n);
    int count = 0;
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    scanf("%d",&lucky);
    for(int i = 0; i < n; i++){
        if(a[i] == lucky || a[i] %lucky == 0)
            count++;
        else{
            while(a[i]>0){
                t=a[i]%10;
                if(t==lucky)
                count++;
                a[i]/=10;
            }
        }
    }
    
    printf("%d", count);
    return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^