An Easy C Program Problem
找幸运数
题目描述
数字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的更多相关文章
- Linux - 修复Ubuntu错误“System program problem detected”
		
The error "System program problem detected" comes up when a certain application crashes. U ...
 - Ubuntu每次启动都显示System program problem detected的解决办法
		
Ubuntu每次启动都显示System program problem detected的解决办法 sudo gedit /etc/default/apport 将enabled=1改为enabled ...
 - 关闭 ubuntu System program problem detected
		
每次开机都出现: System program problem detected 很麻烦,关闭方法: vim /etc/default/apport # set this to 0 to disabl ...
 - System program problem detected 解决
		
每次开机都出现:System program problem detected 管理员权限打开:/etc/default/apport su root vim /etc/default/app ...
 - 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 ...
 - 怎样关掉 ubuntu 中的 System Program Problem Detected 提示框
		
怎样关掉 ubuntu 中的 System Program Problem Detected 提示框 方法如下:sudo gedit /etc/default/apport 打开该文件如下:# se ...
 - 〖Linux〗Kubuntu KDE开机后总是提示“system program problem detected”的解决方法
		
自从从Ubuntu切换到了Kubuntu之后,就经常在开机的时候提示“system program problem detected”: 查看 /var/crash/ 发现都是一些无关痛痒的程序在关机 ...
 - ubuntu 12.04 ubuntu System program problem detected 解决方法
		
1. ubuntu System program problem detected本人操作系统是ubuntu12.04,不知道是系统出了问题还是装的软件有问题,每次开机都出现:System progr ...
 - Ubuntu每次启动都显示System program problem detected
		
执行命令:sudo gedit /etc/default/apport 将enabled=1改为enabled=0保存退出
 
随机推荐
- AIR 中的 File 对象 所访问的文件夹位置
			
AIR 中的 File 对象 所访问的文件夹位置 Link 关于File.cacheDirectory的一点说明 According to the Apple guidelines, data tha ...
 - :“boost/serialization/string.hpp”: No such file or directory 错误
			
主要原因是没有安装和配置boost库. 解决:http://www.programlife.net/boost-compile-and-config.html
 - 转:python webdriver API 之alert/confirm/prompt 处理
			
webdriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到 alert/conf ...
 - 转:python webdriver 环境搭建
			
第一节 环境搭建准备工具如下:-------------------------------------------------------------下载 python[python 开发环境]ht ...
 - C# WinForm动态添加MSChart控件
			
添加mschart.dll动态链接库 添加引用 System.Windows.Forms.DataVisualization MSChart控件作为方便的用户数据展示控件,可以方便的使用控件提 ...
 - 夺命雷公狗ThinkPHP项目之----企业网站24之网站前台列表页面包屑导航的显示
			
我们做面包屑导航的原理其实也是很简单的,我们的思路是: 首先找到该分类的id ,我们可以通过大 I来进行获取得到: 然后通过 大 D 方法让数据进入model层里面进行循环迭代查询, 当然,测试时候发 ...
 - 收缩TempDB的办法(转载)
			
有时候在数据库上运行一个数据量很大的查询语句,会导致TempDB数据量剧增,具体查看下面链接文章: SqlServer 一个查询语句导致tempdb增大55G 找到TempDB剧增的问题后,接下来的问 ...
 - (顺序表的应用5.4.2)POJ 1591 M*A*S*H(约瑟夫环问题的变形——变换步长值)
			
/* * POJ_1591_2.cpp * * Created on: 2013年10月31日 * Author: Administrator */ #include <iostream> ...
 - linux设备驱动归纳总结(八):3.设备管理的分层与面向对象思想【转】
			
本文转载自:http://blog.chinaunix.net/uid-25014876-id-110738.html linux设备驱动归纳总结(八):3.设备管理的分层与面向对象思想 xxxxxx ...
 - Linux之awk命令详解
			
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...