首页 > 其他分享 >输入技巧1

输入技巧1

时间:2023-02-14 13:05:48浏览次数:27  
标签:技巧 int scanf while printf include 输入 op

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

#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=='\n')
                    break;
                 // 回车表示读入完成
            }

            for(int j = 0; j < i; j ++)
                printf("%d ",a[j]);
            printf("\n");
        }
    }
    return 0;
}

 

标签:技巧,int,scanf,while,printf,include,输入,op
From: https://blog.51cto.com/u_15965659/6056612

相关文章