整形数组合并

整形数组合并_牛客网

https://www.nowcoder.com/practice/c4f11ea2c886429faf91decfaf6a310b?tpId=37&tqId=21303&rp=0&ru=/ta/huawei&qru=/ta/huawei/question-ranking

while True:
    try:
        n1=int(input())
        s1=list(map(int,input().split()))
        n2=int(input())
        s2=list(map(int,input().split()))
        #s1.extend(s2)
        print(''.join(map(str,sorted(list(set(s1+s2))))))
    except:
        break

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int n1,n2,j;
    int s1[10000],s2[10000];
    long long res[20000];
    while(cin >> n1){
        j=0;
        for(int i=0;i<n1;i++){
            cin >> s1[i];
            res[j++]=s1[i];
        }
        cin >> n2;
        for(int i=0;i<n2;i++){
            cin >> s2[i];
            res[j++]=s2[i];
        }
        sort(res,res+n1+n2);
        for(int j=0;j<n1+n2;j++){
            if(res[j]!=res[j+1])
                cout << res[j];
        }
        cout << '\n';
    }
    return 0;
}

全部评论
print(''.join(sorted(list(set(s1+s2))))) 為什麼再用一下map函數呢,不加的結果為什麼不對啊?
点赞 回复 分享
发布于 2021-10-25 13:20
为什么要先把列表转换为集合,又再转换为列表。
点赞 回复 分享
发布于 2020-02-08 17:47

相关推荐

渴望wlb的牛油果很...:直说卡第一学历不就行了 非得拐弯抹角
点赞 评论 收藏
分享
迷茫的大四🐶:都收获五个了,兄弟那还说啥,不用改了,去玩吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务