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

    经常用的就这些了;

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

发布了28 篇原创文章 · 获赞 58 · 访问量 3892

猜你喜欢

转载自blog.csdn.net/weixin_45882303/article/details/104406807