码迷,mamicode.com
首页 > 其他好文 > 详细

华为机试—去掉最值剩下的个数

时间:2015-06-27 09:53:34      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:华为机试   华为java   机试java   去掉最值个数   

题目:
输入一串字符,只包含“0-10”和“,”找出其中最小的数字和最大的数字(可能不止一个),输出最后剩余数字个数。

c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void main()
{
    char str[100];
    printf("输入一组字符串:\n");
    scanf("%s",&str);

    size_t len=strlen(str);
    int array[100];
    int count=0;
    for(unsigned int i=0;i<len;i++)
    {
        if(str[i]>=‘0‘&&str[i]<=‘9‘)
            array[count++]=str[i]-‘0‘;

    }
    array[count]=‘\0‘;
    int result=count;
    int min=array[0];
    int max=array[0];
    for(int j=0;j<count;j++)
    {
        if(max<array[j])
            max=array[j];
        else if(min>array[j])
            min=array[j];
    }
    for(int k=0;k<count;k++)
    {
        if(array[k]==min)
            result--;
        if(array[k]==max)
            result--;
    }
    printf("%d\n",result);
}

java

import java.util.Scanner;

public class maxMin {

    public static void main(String[] args) {

        System.out.println("输入一组字符串:");
        Scanner s = new Scanner(System.in);
        String str = s.next();

        int len = str.length();
        int[] array = new int[100];
        int count = 0;

        for (int i = 0; i < len; i++) {
            if (str.charAt(i) >= ‘0‘ && str.charAt(i) <= ‘9‘)
                array[count++] = str.charAt(i) - ‘0‘;
        }

        int min = array[0];
        int max = array[0];

        for (int j = 0; j < count; j++) {
            if (max < array[j])
                max = array[j];
            else if (min > array[j])
                min = array[j];
        }

        int result = count;
        for (int k = 0; k < count; k++) {
            if (array[k] == min)
                result--;
            if (array[k] == max)
                result--;
        }

        System.out.println(result);
    }

}

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

华为机试—去掉最值剩下的个数

标签:华为机试   华为java   机试java   去掉最值个数   

原文地址:http://blog.csdn.net/wtyvhreal/article/details/46655705

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!