题解 | #OJ在线编程常见输入输出练习场--J-字符串排序(3)#c++/python3/java
字符串排序(3)
https://ac.nowcoder.com/acm/contest/5657/J
链接:https://ac.nowcoder.com/acm/contest/5657/J
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
输入描述:
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
输出描述:
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格
示例1
输入
复制
a,c,bb
f,dddd
nowcoder
输出
复制
a,bb,c
dddd,f
nowcoder
思路和心得:
输入时
1.c++可以用stringstream
//c++代码 #include <bits/stdc++.h> using namespace std; int main() { string s; while (cin >> s) { stringstream ss; ss << s; vector<string> words; string word; while (getline(ss, word, ',')) { words.push_back(word); } sort(words.begin(), words.end()); for (int i = 0; i < (int)words.size() - 1; i ++) cout << words[i] << ','; cout << words.back() << endl; } return 0; }
2.python3 可以用str.split(',')
#python3代码 while True: try: words = input().split(',') words.sort() for word in words[:-1]: print(word, end = ',') print(words[-1]) except: break
3.java可以用nextLine().split(",")
//java代码 import java.util.*; public class Main { public static void main(String [] args) { Scanner scan = new Scanner(System.in); while (scan.hasNextLine()) { String[] words = scan.nextLine().split(","); Arrays.sort(words, (w1, w2) -> w1.compareTo(w2)); int wn = words.length; for (int i = 0; i < wn - 1; i ++) { System.out.print(words[i] + ','); } System.out.println(words[wn-1]); } } }