国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > 华为机试―去掉最大值、最小值后剩下的个数

华为机试―去掉最大值、最小值后剩下的个数

来源:程序员人生   发布时间:2015-01-14 09:11:53 阅读次数:3240次

输入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

测试结果:


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生