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保存退出
随机推荐
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- JavaScript----marquee滚动标签 图片无缝滚动 插入百度地图
页面的自动滚动效果,可由javascript来实现, 但是有一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用marquee ...
- 资源Createwindow,对应标识符,绑定窗口
问? 定义一个CEdit cedit1:怎么和IDC_EDIT1 关联,可以在CEdit.Create()里传进去或者在DoDataExchange()里面绑定,是不是一定要先弄出个IDC_EDIT1 ...
- nyist 596 谁是最好的Coder
http://acm.nyist.net/JudgeOnline/problem.php?pid=596 谁是最好的Coder 时间限制:1000 ms | 内存限制:65535 KB 难度:0 ...
- sql 中实现打乱数据的排序
sql 中实现打乱数据的排序 order by NEWID()就实现了数据的打乱
- c++的学习内容一汇总篇(常更新)
在这里假定读者们是有一定编程经验的.例如c#,java,c或者其他任何编程语言. 所有语言都无外乎掌握它的语法,熟悉它的一些库的调用. ---------------语法篇-------------- ...
- Script to set the Purchase Order Status to ‘OPEN’(将采购订单重新打开)
Business Requirement: The finance user requests the IT team to change the PO status to OPEN as they ...
- 夺命雷公狗ThinkPHP项目之----企业网站25之网站前台面包屑导航URL的完善
如果想取出面包屑导航的url那么就必须在model层里面进行多取一个了: <?php namespace Home\Model; use Think\Model; class CategoryM ...
- 二项分布 多项分布 伽马函数 Beta分布
http://blog.csdn.net/shuimu12345678/article/details/30773929 0-1分布: 在一次试验中,要么为0要么为1的分布,叫0-1分布. 二项分布: ...
- access链接最原始代码,两种
using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web ...