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保存退出 
随机推荐
- 【Origin】晨起忆梦
			昨夜有梦又还乡, 忆起少年儿郎样: 田畔有花不忍折, 岁岁年年观花放. -作于二零一五年八月四日 
- jetbrains公司全系列软件最简单破解教程
			由于最新jetbrains发布了IntelliJ IDEA 15. PyCharm 5.PhpStorm10.WebStorm 11.RubyMine等各个版本,但是改变了注册方法.原先的注册码包括注 ... 
- Grunt 自动化部署之css、image、javascript、html压缩Gruntfile.js配置
			grunt.initConfig方法 用于模块配置,它接受一个对象作为参数.该对象的成员与使用的同名模块一一对应. 每个目标的具体设置,需要参考该模板的文档.就cssmin来讲,minify目标的参数 ... 
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
			题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ... 
- sql字符转换函数大全
			删除空格 有两个函数,TTRIM()和LTRIM(),可以用来从字符串中剪掉空格.函数LTRIM()去除应该字符串前面的所有空格:函数RTRIM()去除一个字符串尾部的所有空格.这些和vbscript ... 
- 3D语音天气球(源码分享)——通过天气服务动态创建3D球
			转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ... 
- [Ubuntu] Remove Byte Order Mark (BOM) from files recursively [Forward article]
			Original article: http://www.yiiframework.com/wiki/570/remove-byte-order-mark-bom-from-files-recursi ... 
- 关于centos更新后virtualbox无法使用的问题
			http://blog.csdn.net/zgglj/article/details/50325675 
- x3270: PCOM的替代品
			在Linux上登录Mainframe的利器,而且也是PCOM的免费版.原来没有仔细研究,今天花了一些时间学习,确实能满足基本的要求: x3270的Homepage 看一下Release Note, ... 
- linux设备驱动归纳总结(四):2.进程调度的相关概念【转】
			本文转载自:http://blog.chinaunix.net/uid-25014876-id-65555.html linux设备驱动归纳总结(四):2.进程调度的相关概念 xxxxxxxxxxxx ... 
