zoukankan      html  css  js  c++  java
  • 牛客网各种输入输出总结

    参考:https://blog.csdn.net/inthesilence/article/details/107448234

    1.while(cin>>str)

    例题:HJ1 字符串最后一个单词的长度

    #include<iostream>
    using namespace std;
    
    int main(){
        string word;
        int ans=0;
        while(cin>>word){
            ans=word.size();
        }
        cout<<ans;
        return 0;
    }

    读取一行,按空格分隔

     2.getline(cin,str);

    读取一行,可能有空格分隔。

    #include<iostream>
    using namespace std;
    
    int main(){
        string str;
        getline(cin,str);
        char ch;
        cin>>ch;
        int ans=0;
        for(int i=0;i<str.size();i++){
            if(tolower(str[i])==tolower(ch))
                ans++;
        }
        cout<<ans;
        return 0;
    }

    3.while(cin>>n) 有n控制多行读入

     用set排序去重。

    #include<bits/stdc++.h>
    using namespace std;
    
    int main(){
        vector<int> a;
        priority_queue<int,vector<int>> pq;
        int n;
        
        while(cin>>n){
            int k;
            set<int> st;
            for(int i=0;i<n;i++){
                cin>>k;
                st.insert(k);
            }
            for(auto& num:st)
                cout<<num<<"
    ";
        }
    
        return 0;
    }

    试一下map:更改一下while循环:

        while(cin>>n){
            int k;
            map<int,int> mp;
            for(int i=0;i<n;i++){
                cin>>k;
                mp[k]++;
            }
            for(auto& num:mp)
                cout<<num.first<<"
    ";
        }

     

  • 相关阅读:
    Ural 1966 Cycling Roads
    SQL Server 2008 安装(lpt亲测)
    cf Round#273 Div.2
    poj 2318 TOYS
    计算几何好模板
    ❤Friends
    限制pyqt5应用程序 只允许打开一次
    pyqt5 菜单栏+信息提示框
    Android Linux deploy
    system分区解锁
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/15212990.html
Copyright © 2011-2022 走看看