以华为机试题的输入为例(判断两个IP是否属于同一子网_牛客题霸_牛客网 (nowcoder.com))
方法1:
#includeusing namespace std; string line; int main() { int a=0,b=0,c=0,d=0; while(~scanf("%d.%d.%d.%d", &a,&b,&c,&d)) { } return 0; }
scanf函数输入成功则返回成功的个数,如果输入不成功,则返回-1,-1按位取反(~)的值为0。如此可实现行数不定的输入。在牛客OJ上是又自动的输入终止符的,如果是在本地IDE,需要结束输入,按一下ctrl+Z即可。
方法2:
#include#include using namespace std; string line; int main() { int a=0,b=0,c=0,d=0; char s; while(getline(cin, line)) { stringstream ss; ss << line; ss >> a >> s >> b >> s >> c >> s >> d; } return 0; }
方法3:
#include#include using namespace std; string line; int main() { int a=0,b=0,c=0,d=0; char s; while(getline(cin, line)) { sscanf(line.c_str(), "%d.%d.%d.%d", &a,&b,&c,&d); } return 0; }



