题目:输入一串数,以','分隔,输出所有数中去掉最大值、最小值之后剩下的个数。(其中最大值与最小值可能有多个)
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++还是学习阶段,以后再来优化吧!