输入技巧1

在不知道需要输入多少个数,每个数之间有个空格,当输入换行时结束,如何进行读入操作。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int a[150];
int main()
{
    int n;
    char op;
    while(~scanf("%d", &n))
    {
        while(n --)
        {
            int i = 0;
            while(1)
            {
                scanf("%d",&a[i++]);// 存到数组里
                while((op=getchar())== ' ');//如果是空格不处理
                ungetc(op,stdin);//退格,个人理解是把这个字符清零
                if(op=='
')
                    break;
                 // 回车表示读入完成
            }

            for(int j = 0; j < i; j ++)
                printf("%d ",a[j]);
            printf("
");
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lcchy/p/10139571.html