C/C++语言ACM格式输入输出练习 | 输入字符串数据

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 <stdio.h>
#include <string.h>
#include <stdlib.h>

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<iostream>
#include<vector>
#include<string>
#include<algorithm>

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 <stdio.h>

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<iostream>
#include<vector>
#include<string>
#include<algorithm>

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<stdio.h>
#include<stdlib.h>
#include<string.h>

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 <iostream>
#include <sstream>
#include <vector>
#include<string>
#include<algorithm>

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;
	}
}
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZhangDaniel_ZD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值