正式的ACM,对于输入一般都是比较标准的,不需要参赛人员考虑输入的特殊情况,但也有一些比赛,对于输入没有严格控制,比如下面这个:
输入N个数字,可能以空格分隔,也可能以逗号分隔,不说明输入的个数,也不说明输入以什么方式结束(换行还是EOF)。
这时,对于输入就比较坑爹,这里写了一个简单的输入处理函数。代码如下:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
static vector<int> g_inputList;
void getInput()
{
char ctmp;
int numOfInt = 0;
int iFlag = 0;
int nInput = 0;
while (1)
{
int isEof = scanf("%c", &ctmp);
if (isEof == EOF || '\n' == ctmp || ' ' == ctmp || ',' == ctmp)
{
if (numOfInt > 0)
{
if (iFlag > 0)
{
nInput = 0-nInput;
}
g_inputList.push_back(nInput);
}
nInput = 0;
numOfInt = 0;
iFlag = 0;
if (isEof == EOF || '\n' == ctmp)
{
break;
}
}
else if ('-' == ctmp)
{
iFlag = 1;
}
else
{
numOfInt++;
nInput = nInput*10 + (ctmp -'0');
}
}
}