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保存退出
随机推荐
- HDU 4512 吉哥系列故事——完美队形(LCIS)
Problem Description 吉哥这几天对队形比较感兴趣. 有一天,有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一 ...
- android_demo01
/layout/activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/ ...
- linux c
#include <stdio.h>#include <string.h>#include <strings.h> int main(){ char buf[ ...
- paper 86:行人检测资源(上)综述文献【转载,以后使用】
行人检测具有极其广泛的应用:智能辅助驾驶,智能监控,行人分析以及智能机器人等领域.从2005年以来行人检测进入了一个快速的发展阶段,但是也存在很多问题还有待解决,主要还是在性能和速度方面还不能达到一个 ...
- 夺命雷公狗---node.js---6net模块玩telnet通信(下)
我们来升级玩玩,废话不多说,代码如下所示: /** * Created by leigood on 2016/8/12. */ var net = require('net'); var ChatSr ...
- 夺命雷公狗---Thinkphp----1之目录介绍
ThinkPHP框架 特点: 免费开源 敏捷开发(快速开发) 面向对象 MVC思想 yii,ci之类的框架都有这些特点.是06年到现在的一个老牌框架,现在还是个很不错的框架 可以在thinkphp的官 ...
- python3使用csv模块读写csv文件
python3使用csv模块读写csv文件 读取csv文件: import csv #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() ...
- scrum站立会议------10.20
小组名称:nice! 小组成员:李权 于淼 杨柳 刘芳芳 项目内容:约跑app(约吧--暂定) 1.任务进度 2.燃尽图
- [CrunchBang]Linux系统下必要的中文字体
sudo apt-get install ttf-droid ttf-wqy-zenhei xfonts-wqy ttf-wqy-microhei ttf-arphic-ukai ttf-arphic ...
- 「ruby」使用rmagick处理图像
安装rmagick gem A new release 2.13.2 of RMagick is now available on github as well as rubygems. This r ...