华为上机题1:去掉最大最小值之后数的个数

题目:输入一串数,以','分隔,输出所有数中去掉最大值、最小值之后剩下的个数。(其中最大值与最小值可能有多个) 

Smple input:3,3,5,3,6,9,7,9   Sample outPut: 3

看到别人的参考答案:


自己尝试了一下,运行结果总是说内存只读,不可访问,主要是在读入字符的时候出错。(没弄明白为什么会这样)

自己尝试了下其他的方法,原理一样,就只是读入那一部分做了修改,改正后,结果就好了。

#include "stdafx.h"
#include <iostream>
#include <cctype>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

	int max = 0;
	int min = 100;
	int num = 0;
	int a[20];

	int i = 0;
	int j = 0;
	int temp;
// 输入数字串 
	string data;
	getline(cin,data);


	while(data[j] != '\0')
	{

		if(isdigit(data[j]))
		{
			temp = data[j]-'0';
			a[i] = temp;
			if(temp > max)
				max = temp;
			if(temp < min)
				min = temp;
			i++;
		}
		j++;

	}
	
	for(int n = 0; n< i ;n++)
	{
		if(a[n] > min && a[n] < max)
			num++;
	}
	cout << num <<endl;			
	return 0;
}
做的过程中,没有想到去优化,目前C++还是学习阶段,以后再来优化吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值