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保存退出
随机推荐
- eclipse jsp 加载服务器tomcat
1.window->Preferences
- C++Builder 笔记
1.界面窗口如何不显示标题栏? 在Form属性栏里面把BorderStyle的值设为None 2.wchar_t wchar_t是C/C++的字符类型,是一种扩展的存储方式,wchar_t类型主要用在 ...
- [原创]java WEB学习笔记51:国际化 概述,API 之 locale类,dataFormat类,numberFormat类, MessageFormat类,ResourceBundle 类
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 8. 星际争霸之php设计模式--享元模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- 使用php模拟post的几种方法
<?phpfunction file_get_contents_post($url,$post){$options=array('http'=>array('method'=>'PO ...
- ASP.NET 中通过Form身份验证 来模拟Windows 域服务身份验证的方法
This step-by-step article demonstrates how an ASP.NET application can use Forms authentication to ...
- Openstack的vnc界面定制
先来看一下青云的vnc界面: 在来看一下openstack的自带的vnc界面: 区别于感受 本身原理是一样的,但是vnc上面的html布局不一样而已,但是青云的vnc界面给人的感受是:清晰提示,信息给 ...
- HorizontalScrollView的配置
package com.exmple.jinritoutiao; import java.util.ArrayList;import java.util.Calendar;import java.ut ...
- lamp环境编译错误
GD 编译出错解决方法 编译最新的2.0.35版本,用默认的 ./configure编译 当make的时候,出现以下错误 configure.ac:64: error: possibly undefi ...
- xib中设置控件的圆角
1.http://my.oschina.net/ioslighter/blog/387991?p=1 利用layer.cornerRadius实现一个圆形的view,将layer.cornerRadi ...