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<<"
    ";
        }

     

  • 相关阅读:
    xtrabackup
    spark对机器的要求
    hbase的总结
    TO B公司高效能的组织建设实践
    如何给客户展示实力
    什么样的IT队伍是好队伍
    程序员如何使用OKR
    云原生
    Flink 的18个小知识点
    apt 常用命令
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/15212990.html
Copyright © 2011-2022 走看看