C/C++语言ACM格式输入输出练习 | 输入字符串数据
文章目录
- 1、输入字符串个数、一行字符串、空格隔开
- 2、输入多组字符串、空格隔开
- 3、数多组字符串、逗号隔开
1、输入字符串个数、一行字符串、空格隔开
题目描述
对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
输入描述
输入有两行,第一行n
第二行是n个空格隔开的字符串
输出描述
输出一行排序后的字符串,空格隔开,无结尾空格
示例1
输入
5
c d a bb e输出
a bb c d e
C语言代码
#include
#include
#include int main() {int n;scanf("%d",&n);int str[n][100];for(int i = 0; i < n; i++){scanf("%s",str[i]);}qsort(str, n, sizeof(str[0]), strcmp);for(int i = 0; i < n; i++){printf("%s ", str[i]);}return 0;
}
C++语言代码
#include
#include
#include
#include using namespace std;int main() {vector<string> vstr;int n;cin >> n;while (n--) {string s;cin >> s;vstr.push_back(s);}sort(vstr.begin(), vstr.end());for(auto s: vstr) {cout << s << " ";}return 0;
}
2、输入多组字符串、空格隔开
题目描述
对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
输入描述
多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符,n<100
输出描述
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开
示例1
输入
a c bb
f dddd
nowcoder输出
a bb c
dddd f
nowcoder
C语言代码
#include int main(int argc, char** argv) {char str[100][200];int index = 0;while (scanf("%s", &str[index++]) != EOF) {if (getchar() == '\n') {qsort(str, index, sizeof(str[index]), strcmp);for (int i = 0; i < index; i++) {printf("%s ", str[i]);}printf("\n");index = 0;}}return 0;
}
C++语言代码
#include
#include
#include
#include using namespace std;int main() {string str;vector<string> result;while(cin >> str){result.push_back(str);if(cin.get() == '\n'){sort(result.begin(), result.end());for(auto tmp : result){cout << tmp << " ";}cout << endl;result.clear();}}return 0;
}
3、数多组字符串、逗号隔开
题目描述
对输入的字符串进行排序后输出
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
输入描述
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
输出描述
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格
示例1
输入
a,c,bb
f,dddd
nowcoder输出
a,bb,c
dddd,f
nowcoder
代码
#include
#include
#include int main(void) {char str[100][200];int i = 0, k = 0;char c;while(scanf("%c", &c) != EOF) {if(c == ',') {str[i++][k] = '\0';k = 0;} else if (c == '\n') {str[i++][k] = '\0';k = 0;//printf("%d",i);qsort(str, i, sizeof(str[0]), strcmp);for(int j = 0; j < i - 1; j++) {printf("%s,", str[j]);}printf("%s\n", str[i - 1]);i = 0;} else {str[i][k++] = c;}}
}
C++语言代码
#include
#include
#include
#include
#include using namespace std;int main() {string s1;while(getline(cin, s1)) {stringstream s2(s1);string s3;vector<string> s4;while(getline(s2, s3, ',')) {s4.push_back(s3);}sort(s4.begin(), s4.end());for(auto it = s4.begin(); it != s4.end() - 1; it++) {cout<< *it << ',';}cout << *(s4.end() - 1) << endl;}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
