zoukankan      html  css  js  c++  java
  • 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
    ", 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.

  • 相关阅读:
    UVA 1515 Pool construction 最大流跑最小割
    BZOJ 1060: [ZJOI2007]时态同步 树形DP
    Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP
    BZOJ 4027: [HEOI2015]兔子与樱花 贪心
    BZOJ 2435: [Noi2011]道路修建 dfs搜图
    HDU 5297 Y sequence 容斥/迭代
    HDU 5296 Annoying problem dfs序 lca set
    HDU 5289 Assignment RMQ
    343D/Codeforces Round #200 (Div. 1) D. Water Tree dfs序+数据结构
    php mysqli扩展库之预处理操作
  • 原文地址:https://www.cnblogs.com/JacZhu/p/5486536.html
Copyright © 2011-2022 走看看