您好,欢迎来到年旅网。
搜索
您的当前位置:首页newcoder——OJ在线编程(C++)&INT_MIN(做的题中补充的知识点)

newcoder——OJ在线编程(C++)&INT_MIN(做的题中补充的知识点)

来源:年旅网
1. 输入有两行,第一行n,第二行是n个空格隔开的字符串;输出一行排序后的字符串,空格隔开,无结尾空格

输入:
5
c d a bb e
输出:
a bb c d e

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int len;
    cin>>len;
    vector<string> str(len);
    while(--len>=0){
        cin>>str[len];
    }
    sort(str.begin(),str.end());
    len=str.size()-1;
    for(int i=0;i<=len;i++){
        cout<<str[i]<<" ";
    }
    cout<<endl;
    return 0;
}
2.多个测试用例,每个测试用例一行。每行通过空格隔开,有n个字符,n<100;输出:对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

输入:
a c bb
f dddd
nowcoder
输出:
a bb c
dddd f
nowcoder

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int main(){
    string str;
    vector<string> strs;
    while(cin>>str){
        strs.emplace_back(str);
        if(cin.get()=='\n'){
            sort(strs.begin(),strs.end());
            int len=strs.size()-1;
            for(int i=0;i<=len;i++){
                cout<<strs[i]<<" ";
            }
            cout<<endl;
            strs.clear();
        }
    }
    return 0;
}
3.多个测试用例,每个测试用例一行。每行通过,隔开,有n个字符,n<100;对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格

输入:
a,c,bb
f,dddd
nowcoder
输出:
a,bb,c
dddd,f
nowcoder

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<sstream>
using namespace std;

int main(){
    string line;
    while(getline(cin,line)){
        istringstream sin(line);
        string str;
        vector<string> strs;
        while(getline(sin,str,',')){
            strs.emplace_back(str);
        }
        sort(strs.begin(),strs.end());
        int len=strs.size()-1;
        for(int i=0;i<len;i++){
            cout<<strs[i]<<',';
        }
        cout<<strs[len]<<endl;
    }
    return 0;
}
4.使用插入排序对链表进行排序。

class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        if(!head||!head->next) return head;
        ListNode* p,*q;//p指向要插入位置的前一个
        //再标准头文件limits.h中
        //#define INT_MAX 21474837
        //#define INT_MIN (-INT_MAX - 1)
        /*
        C中int类型是32位的,范围是-21474838到21474837 。 
        (1)最轻微的上溢是INT_MAX + 1 :结果是 INT_MIN; 
        (2)最严重的上溢是INT_MAX + INT_MAX :结果是-2; 
        (3)最轻微的下溢是INT_MIN - 1:结果是是INT_MAX; 
        (4)最严重的下溢是INT_MIN + INT_MIN:结果是0 。
        */
        ListNode* newhead=new ListNode(INT_MIN);
        q=head->next;
        newhead->next=head;
        while(q){
            for(p=newhead;p!=head;p=p->next){
                if(q->val>=p->val&&q->val<=p->next->val) break;
            }
            if(p!=head){
            head->next=q->next;
            q->next=p->next;
            p->next=q;
            q=head->next;
            }else{
                head=head->next;
                q=head->next;
            }
        }
        return newhead->next;
    }
};

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务