对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
https://ac.nowcoder.com/acm/contest/5657#question
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格
a,c,bb f,dddd nowcoder
a,bb,c dddd,f nowcoder
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(){ string s; vector<string> res; char c; do{ c = cin.get(); if(c != ',' && c != '\n') s += c; else if(c == ','){ res.push_back(s); s.clear(); } else{ if(!s.empty()){ res.push_back(s); s.clear(); } sort(res.begin(), res.end()); for(int i = 0; i < res.size(); ++i){ cout << res[i]; if(i < res.size() - 1) cout << ','; } cout << endl; res.clear(); } }while(c != EOF); }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { string line; while (getline(cin, line)) { vector<string> ans; stringstream ss(line); string s; while (getline(ss, s, ',')) { ans.push_back(s); } sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); ++i) { if (i != ans.size() - 1) cout << ans[i] << ","; else cout << ans[i]; } cout << "\n"; } return 0; }
import sys for line in sys.stdin: a = line.strip().split(',') a_list = sorted(a) for ax in a_list: if ax==a_list[-1]: print(ax,end='') else: print(ax,end=',') print()
#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; void print_vec(vector<string>& data){ for(int it = 0; it < data.size()-1; it++){ cout << data[it] << ","; } cout << data[data.size()-1] << endl; data.clear(); } int main() { vector<string> v; string str; while(getline(cin,str)){ size_t pos = 0; string delimiter = ","; while ((pos = str.find(delimiter)) != string::npos) { v.emplace_back(str.substr(0, pos)); str.erase(0, pos + delimiter.length()); } v.push_back(str); sort(v.begin(), v.end()); print_vec(v); } }
importjava.io.*; importjava.util.*; publicclassMain{ publicstaticvoidmain(String [] args) throwsIOException{ BufferedReader bf = newBufferedReader(newInputStreamReader(System.in)); try{ while(true){ String [] st = bf.readLine().split("\\,"); Arrays.sort(st); String ans = ""; for(String s: st){ ans += s+","; } System.out.println(ans.substring(0,ans.length()-1)); } }catch(Exception e){ return; } } } |
#include<bits/stdc++.h> using namespace std; int main(){ vector<string> res; string temp; while(cin>>temp){ if(temp==","){ continue; } res.push_back(temp); if(cin.get()=='\n'){ sort(res.begin(),res.end()); for(int i=0;i<res.size();i++){ if(i!=res.size()-1) cout<<res[i]<<','; else cout<<res[i]<<endl; } res.clear(); } } }
#include <iostream> #include <sstream> #include <vector> #include <algorithm> using std::cin; using std::cout; using std::sort; using std::vector; using std::string; using std::istringstream; int main() { string line; while(std::getline(cin,line)) { istringstream iss(line); string tmp; vector<string> str_vec; while(std::getline(iss,tmp,',')) { str_vec.push_back(tmp); } sort(str_vec.begin(),str_vec.end()); for(int i = 0; i < str_vec.size(); i++) { cout << str_vec[i]; if(i != str_vec.size() - 1) cout << ","; } cout << std::endl; } return 0; }
while(true) { let line = readline(); if (line) { let arr = line.split(','); let ret = []; let min = arr[0]; let minIndex = 0; while (arr.length){ for(let i = 0; i < arr.length; i++) { if(arr[i] < min) { min = arr[i]; minIndex = i; } } arr.splice(minIndex,1); ret.push(min); min = arr[0]; minIndex = 0; } console.log(ret.join(',')); } else { break; } }