博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
An Easy C Program Problem
阅读量:4474 次
发布时间:2019-06-08

本文共 1962 字,大约阅读时间需要 6 分钟。

找幸运数

题目描述

数字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 
#include
//I think I should improve my POOR English, so all the comments are written in Englishint 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.

转载于:https://www.cnblogs.com/JacZhu/p/5486536.html

你可能感兴趣的文章
英语语法(2)----点破主谓宾系表三大句型
查看>>
html如何与cgi数据交换,HTML网页与CGI之间通信的 实例分析
查看>>
html如何调用flash插件,htmlflash播放器插件如何播放 网页播放器flash插件怎么解决...
查看>>
mysql数据在html上面显示不出来的,HTML表格不能正确显示MySQL数据
查看>>
数据包和html,数据包和数据报有何区别?
查看>>
jq 异步调用一个html,聊聊如何将jQuery的$.ajax()用于异步HTTP请求
查看>>
html导出excel多sheet,js 导出多sheet表格
查看>>
html日期函数,我所见过的最简短、最灵活的javascript日期转字符串工具函数
查看>>
flann matlab,FLANN 快速的(近似)最近邻开源库
查看>>
pmta linux视频,PowerMTA (PMTA) 的安装和设置方法 – 黄忠 – 博客
查看>>
2016秋季C语言程序设计试题,2016c语言程序设计模拟试题
查看>>
C语言编程初体验 作文,C语言作文件操常用代码
查看>>
rar for android去广告,安卓解压神器RAR v5.30.39 去广告版
查看>>
android p什么变化,Android P预览版,这些调整和变化最值得关注
查看>>
android 7.0宽度432,全球最小的4G手机,比手掌还小,安卓7.0
查看>>
android fragmentstatepageradapter框架,Android FragmentStatePagerAdapter
查看>>
html自适应meta标签,自适应布局meta标签中viewport、content、width、initial-scale、minimum-scale、maximum-scale总结...
查看>>
html怎么加入编辑器,HTML 编辑器
查看>>
python发挥程度_你为什么用 Python?
查看>>
file 选择的文件胖多有多大_「HTML5 进阶」FileAPI 文件操作实战,内附详细案例,建议收藏...
查看>>