找幸运数

题目描述

数字8最多的那个数为幸运数。

输入n和n个整数,找这n个数中的幸运数。在主函数中调用ndigit函数,判断某个整数x含数字8的个数。如果有多个幸运数输出第一个幸运数,如果所有的数中都没有含数字8,则输出NO.

函数int ndigit(int n,int k)功能:统计整数n中含数字k的个数。

输入描述

输入n个n个整数

输出描述

幸运数

输入样例

5 568 567 328 48768 8688

输出样例

8688


ANSWER(with a little presentation error)

#include <stdio.h>
#include <stdlib.h> //I think I should improve my POOR English, so all the comments are written in English int ndigit (int n, int k); int main()
{
/**
* @param n INPUT 1
* @param num the temp of the number in INPUT
* @param luckyNum the lucky number
* @param luckyDigCount the count of lucky digit in the lucky number
*/
int n, i, num, luckyNum = 0, luckyDigCount = 0; //get the INPUT
scanf("%d", &n); //get n numbers from console
//and find the lucky number
for (i = 0; i < n; i++)
{
//get the input
scanf("%d", &num); //if the count of lucky digit in current number more than current lucky number's
if (ndigit(num, 8) > luckyDigCount)
{
//set current number as lucky number
luckyDigCount=ndigit(num,8);
luckyNum = num;
}
}
//if lucky number doesn't have a lucky digit
//that means there is no lucky number in this test case
//so, Print "NO"
if (luckyDigCount==0)
{
printf("NO");
}
else
{
//Print the lucky number
printf("%d\n", luckyNum);
}
}
/**
* get the count of lucky digit in the param n
* @param n test number
* @param k lucky digit
* @return the count of lucky digit in the param n
*/
int ndigit (int n, int k)
{
int count = 0;
for (; n; n /= 10)
{
if (n%10 == k)
{
count++;
}
} return count;
}

SUMMARY

What if the OUTPUT is the biggest lucky number?

Add a judgement statement,that compare current number to the previous lucky number, after we ensure current number is one of the lucky numbers.

An Easy C Program Problem的更多相关文章

  1. Linux - 修复Ubuntu错误“System program problem detected”

    The error "System program problem detected" comes up when a certain application crashes. U ...

  2. Ubuntu每次启动都显示System program problem detected的解决办法

    Ubuntu每次启动都显示System program problem detected的解决办法 sudo gedit /etc/default/apport 将enabled=1改为enabled ...

  3. 关闭 ubuntu System program problem detected

    每次开机都出现: System program problem detected 很麻烦,关闭方法: vim /etc/default/apport # set this to 0 to disabl ...

  4. System program problem detected 解决

    每次开机都出现:System program problem detected 管理员权限打开:/etc/default/apport   su root   vim /etc/default/app ...

  5. Remove “System Program Problem Detected” Messages From Ubuntu

    One of my Ubuntu systems would pop up the following message multiple times after logging in: System ...

  6. 怎样关掉 ubuntu 中的 System Program Problem Detected 提示框

    怎样关掉 ubuntu 中的 System Program Problem Detected 提示框 方法如下:sudo gedit /etc/default/apport  打开该文件如下:# se ...

  7. 〖Linux〗Kubuntu KDE开机后总是提示“system program problem detected”的解决方法

    自从从Ubuntu切换到了Kubuntu之后,就经常在开机的时候提示“system program problem detected”: 查看 /var/crash/ 发现都是一些无关痛痒的程序在关机 ...

  8. ubuntu 12.04 ubuntu System program problem detected 解决方法

    1. ubuntu System program problem detected本人操作系统是ubuntu12.04,不知道是系统出了问题还是装的软件有问题,每次开机都出现:System progr ...

  9. Ubuntu每次启动都显示System program problem detected

    执行命令:sudo gedit /etc/default/apport 将enabled=1改为enabled=0保存退出

随机推荐

  1. 数据库迁移之从oracle 到 MySQL

    方式一: 手动方式导入导出 手动的方式导入, 就是操作步骤会比较繁琐一些. 对Table 的结构和数据: 1. 使用 SQL Developer 把 oracle 的 table 的schema 和 ...

  2. 无序数组的中位数(set+deque)hdu5249

    KPI Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. hdu(1171)多重背包

    hdu(1171) Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. .NET: C#: Datetime

    比较简单的类,一般用到它的属性.经常会用到的是DateTime.Now和DateTime.Now.TimeOfDay; using System; using System.Collections.G ...

  5. android设置系统模式

    android 静音与振动1,设置静音和振动静音和振动都属于来电后的动作.所以在设置静音和振动时都只是设置一些标识,并往数据库写入相应标识. 文件:packages/apps/settings/src ...

  6. 夺命雷公狗---Thinkphp----1之目录介绍

    ThinkPHP框架 特点: 免费开源 敏捷开发(快速开发) 面向对象 MVC思想 yii,ci之类的框架都有这些特点.是06年到现在的一个老牌框架,现在还是个很不错的框架 可以在thinkphp的官 ...

  7. nginx for windows中的一项缺陷

    按照官网上的说法,使用 start nginx 启动 nginx,使用 nginx -s quit 可以优雅地退出. 经实验,使用 start nginx 之后,会启动两个 nginx 的进程,据官网 ...

  8. TortoiseGit文件夹和文件图标不显示解决方法

    试了两种方法, 1.修改Max Cached Icons http://www.open-open.com/lib/view/open1414396787325.html 2.修改图标排序 http: ...

  9. 视频处理控件TVideoGrabber视频捕捉设设备相关问题

    选择一个视频捕捉设备 首先设置 VideoSource = vs_VideoCaptureDevice来选择一个视频捕捉设备作为一个视频源. 通过指定VideoDevice属性来选择当前的视频捕捉设备 ...

  10. php原子操作,文件锁flock,数据库事务

    php原子操作,文件锁flock,数据库事务 php没有继承posix标准支持的unix锁,只封装了一个linux系统调用flock(信号量也能做成锁),按理也是可以使用锁机制的,虽然效率低一点.ph ...