C++之acm多组输入总结

C++之acm多组输入总结

ACM竞赛题目的输入数据常要求有多组,并且格式多种多样,这是初次登OJ平台的同学的一个障碍。实际上,这些格式可以归为固定的几种类型,本文介绍各种类型的处理方法,以帮助同学们克服这些障碍。

--------------------------------------

由于小飞之前对c的输入有了较为详细的了解,所以关于c的就直接上链接了:C语言之acm输入汇总

--------------------------------------

好!下面就是干货了:


  1. 以一组数据为例:测试a+b
  • #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a,b;
        cin>>a>>b;
        cout<<a+b<<endl;
        return 0;
    }
    
    
    // 输出结果:
    7 9
    16
    
    Process returned 0 (0x0)   execution time : 3.375 s
    Press any key to continue.
    

     

        2.有多组测试数据,直到读至输入文件结尾为止

 

  • #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a,b;
        while(cin>>a>>b)
            {
             cout<<a+b<<endl;
            }
             return 0;
    }
    
    
    //    输出结果:
    7 9
    16
    4 9
    13
    4 9
    13
    1 6
    7
    

    3.在开始的时候输入一个N,接下来是N组数据

  • #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a,b,T;
        cin>>T;
        while(T--)
            {
                cin>>a>>b;
                 cout<<a+b<<endl;
            }
             return 0;
    }
    
    
    //    输出结果:
    3
    4 9
    13
    1 3
    4
    1 8
    9
    
    Process returned 0 (0x0)   execution time : 10.708 s
    Press any key to continue.
    

    4.输入不说明有多少组数据,但以某个特殊输入为结束标志

  • 假设以a或者b为零 结束

  • #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a,b;
        while(cin>>a>>b)
            {
                if(a==0||b==0)
                    break;
                else
                 cout<<a+b<<endl;
            }
             return 0;
    }
    
    
    //     输出结果:
    2 3
    5
    4 9
    13
    0 6
    
    Process returned 0 (0x0)   execution time : 7.490 s
    Press any key to continue.

    5.读入字符

  • 使用getchar()
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        char a;
        while((a=getchar())!=EOF)
            {
                 cout<<a;
            }
             return 0;
    }
    //  输出结果:
    4
    4
    d
    d
    
    
    9
    9
    l
    l
    

    ---------------------------------------------------------------------

      PS:可能有很多朋友分不清楚get,gets,getline,cin.get的细微区别这里我就不总结了,我直接上链接了这个大佬确实写的不错:点它就完事了,这是一个传送门

----------------------------------------------------------------------------

6. 读入字符使用cin.get

  • #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        char a;
        while(cin.get(a))
            {
                 cout<<a;
            }
             return 0;
    }
    
    //  输出结果:
    2
    2
    4
    4
    7
    7
    w
    w
    
    
    4
    4
    e
    e
    

7.string型的读入

  • #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        string a;
        while(getline(cin,a))
            {
                 cout<<a;
            }
             return 0;
    }
    
    
    //  输出:
    byuciankmlsl,
    byuciankmlsl,
    gcybusahj
    gcybusahj
    5962213jhytgvu
    5962213jhytgvu
    yvuscbhaj
    yvuscbhaj
    

     

8.字符数组

  • #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        char a[100];
            while(cin.getline(a,100))
            {
                 cout<<a;
            }
             return 0;
    }
    
    
    //  输出结果:
    chyuwchn
    chyuwchn
    yucbhjn
    yucbhjn
    uybchd
    uybchd
    5412tfugyihusojnxlk
    5412tfugyihusojnxlk

    经常用的就这些了;

  • 喜欢的就点个赞吧!!!

 

  • 13
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liknana

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值