小吖白又白


私信TA

用户名:H1810819128

访问量:42895

签 名:

时而理性,时而感性,无药可救

等  级
排  名 52
经  验 10946
参赛次数 4
文章发表 77
年  龄 0
在职情况 学生
学  校 贺州学院
专  业 CS

  自我简介:

菜鸡

解题思路:
先排序,在把排序好的元素遍历,如果是奇数,就插在一个新的容器的前面,如果是偶数,就插在后面,可以用STL中的list容器或者vetor容器实现,也可以先遍历元素,把是奇数的元素和是偶数的元素分别放到两个数组中,在分别排序,奇数从大到小,偶数从小到大,再按照题目的格式输出就行,以下给出用list容器实现的代码供参考。


注意事项:

参考代码:

//list
#include<bits/stdc++.h>
using namespace std;
list<int>lt1;
list<int>lt2;
int main() {
	for(int i = 1; i<=10;i++)
	{
		int x;
		cin>>x;
		lt1.push_back(x);
	}
	lt1.sort();
	list<int>::iterator it;
	for(it = lt1.begin();it!=lt1.end();it++) 	
	{
		if(*it%2==1)
			lt2.push_front(*it);
		else
			lt2.push_back(*it);
	}
	for(it = lt2.begin();it!=lt2.end();it++)
		cout<<*it<<" ";	
	return 0;
}


//普通数组
#include<bits/stdc++.h>
using namespace std;
int a[15];
int b[15];
int main() {
	int an=0,bn=0;
	for(int i = 1;i<=10;i++) {
		int x;
		cin>>x;
		if(x%2==1)
			a[++an] = x;
		else
			b[++bn] = x;
	}
	sort(a+1,a+an+1,greater<int>());
	sort(b+1,b+bn+1);
	for(int i = 1;i<=an;i++)
		cout<<a[i]<<" ";
	for(int i = 1; i<=bn;i++)
		cout<<b[i]<<" ";
	
	
	return 0;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区