题目描述:
句子正序
输入一个字符串 可能会有多个连续空格
如student. a am I
把句子正序 正确输出
I am a student.
如果句子中有多个连续的空格 输出省略为一个

#include<iostream>
#include<string>
using namespace std;
int main() {
	string s;
	string word[1000];
	int j = 0;
	getline(cin, s);//接收输入串
	string temp;
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == ' ') {
			if (temp != "") {
				word[j++] = temp;
			}
			temp = "";
		}
		else {
			temp += s[i];
		}
	}

	cout << temp;//输出最后一个字符串;
	for (int i = j; i > 0; i--) {
		cout << word[i] << " ";
	}
	cout << word[0];
	return 0;
}

输出:
兰州大学计算机专硕复试题
1.排序
输入一个数组 ,输出按照这样的排序
偶数按照从大到小的排序
奇数按照从小到大的排序
如果有奇数那么第一个数字是奇数
输出按照奇数偶数交叉的顺序排列
如果奇数和偶数的个数不一样多,那么多出的数直接附加在输出的后面

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a, int b) {
	return a > b;
}
int main() {
	int a[100] = { -1 }, b[100], c[200];
	int n, j = 0, k = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> c[i];
	}
	for (int i = 0; i < n; i++) {
		if (c[i] % 2 == 0) {
			a[j++] = c[i];//偶数
		}
		else {
			b[k++] = c[i];//奇数
		}
	}
	sort(a, a + j, cmp);
	sort(b, b + k);
	//交叉输出数组
		int t = 0;
		while (t < j&&t < k) {//输出长度相同的部分
			cout << b[t] << a[t];
			t++;
		}
		while (t < j) {//a数组还有剩余
			cout << a[t++];
		}
		while (t < k) {//b数组还有剩余
			cout << b[t++];
		}
	return 0;
}

运行结果:
兰州大学计算机专硕复试题

相关文章:

  • 2022-12-23
  • 2021-10-18
  • 2021-06-15
  • 2021-05-26
  • 2021-10-22
  • 2021-07-07
  • 2021-11-04
  • 2021-04-23
猜你喜欢
  • 2022-12-23
  • 2021-12-08
  • 2021-12-12
  • 2022-01-10
  • 2022-01-08
  • 2021-09-08
相关资源
相似解决方案