華為機試―去掉最大值、最小值后剩下的個數
來源:程序員人生 發布時間:2015-01-14 09:11:53 閱讀次數:3474次
輸入1串數,以','分隔,輸出所有數中去掉最大值、最小值以后剩下的個數。(其中最大值與最小值可能有多個)
Sample input: 3,3,5,3,6,9,7,9
Sample output: 3
#include<stdio.h>
/*
解題思路:使用strtok分割函數分割字符串,統計最大最小的數字,
遍歷1遍數組,計算除最大最小的數字的個數
*/
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
{
char s[100];
int a[100];
int i=1;
fgets(s,100,stdin);
char *p=strtok(s,",");
int start=atoi(p);
a[0]=start;
int _min=start;
int _max=start;
while(p=strtok(NULL,","))
{
int t=atoi(p);
if(t>_max)_max=t;
if(t<_min)_min=t;
a[i++]=t;
}
int count=0;
for(int j=0;j<i;++j)
{
if(a[j]!=_max&&a[j]!=_min)
count++;
}
printf("%d
",count);
return 0;
}
測試數據:3,3,5,3,6,9,7,9
測試結果:

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈