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保存退出
随机推荐
- 转:python webdriver API 之alert/confirm/prompt 处理
webdriver 中处理 JavaScript 所生成的 alert.confirm 以及 prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到 alert/conf ...
- csuoj 1337: 搞笑版费马大定理
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1337 1337: 搞笑版费马大定理 Time Limit: 1 Sec Memory Limit ...
- android studio ADB not responding.
打开cmd 输入 netstat -aon|findstr "5037" 找到谁在占用5037端口 记住他的pid. 例如pid为 2028 输入 taskkill ...
- html随笔
<!DOCTYPE HTML> <html> <head> <meta charset = "utf-8"> <script ...
- paper 30 :libsvm的参数说明
English: libsvm_options: -s svm_type : set type of SVM (default 0) 0 -- C-SVC 1 -- nu-SVC 2 -- one-c ...
- 夺命雷公狗---node.js---16之项目的构建在node+express+mongo的博客项目1
废话不多说我们直接开工... 直接在目录下打开黑窗口: 然后就开始看看我们创建出来的文件了: 然后就开始创建项目下的目录了: 从这里就可以清晰的看得到我们的目录都是以前后台来分离开来的,引入模版也很简 ...
- scan & ATPG
Testability用来表征一个manufactured design的quality. 将testability放在ASIC前端来做,成为DFT(Design For Test),用可控(cont ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON InpaintingCt1
zw版[转发·台湾nvp系列Delphi例程]HALCON InpaintingCt1 unit Unit1;interfaceuses Windows, Messages, SysUtils, Va ...
- 【python】python环境的安装与配置
安装配置pip / easy_install / virtualenv 在ubuntu 10.10之后的版本中,要这样安装 sudo apt-get install python-pip python ...
- C语言初学者代码中的常见错误与瑕疵(13)
https://www.cpfn.org/bbs/viewtopic.php?f=85&t=5940&sid=ccbcf716d21191452e7c08a97b502337& ...