目录

多测试用例的输入方式

目录

多测试用例的输入方式

多测试用例并不少见,对于大多数同学来说so easy!

但是我觉得这种东西还是要来谈谈,因为我们在敲代码的过程中会遇到各种各样的输入方式,多测试的数组,字符,常数等输入。

本文只讨论 c和c++的多测试用例输入方式:

一.单独一个变量的输入:

拿int举例:

C:


int n;
while(scanf("%d",&n)!=EOF)
{
    ……
}

或者更简洁:(两种等价)

int n;
while(~scanf("%d",&n))
{
    ……
}

C++:


int n;
while (cin >> n)
{
  ...
}

二数组,字符串的多测试输入

拿字符串举例:

C:


char s[1000];
while(gets(s))
{
  ...
}

不过gets已经逐渐被淘汰了,不建议使用gets函数

char s[1000];
while(~scanf("%s",s))
{
	//……
}

C++:

输入字符串建议用getline()比较安全,它会帮你检查是否溢出


string a;
while(getline(cin,a,'='))
{
   ……
   getchar();//吸收换行符,否则只能输入两次
}