GoogeJam_Sorting

本文展示了一个使用C++编写的程序,该程序从输入文件读取数据,并将读取到的数据按照奇偶分类后分别进行排序。排序完成后,程序会将排序结果输出到指定文件中。对于偶数序列采用降序排列,而奇数序列则按升序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<fstream>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 1010
int vec[maxn];
int odd_vec[maxn];
int even_vec[maxn];

bool cmp(int a,int b)
{
    return a>b;
}

int main()
{
	ifstream in("C-large-practice.in");
	ofstream out("cs.out");
	int T,N,tmp;


	in >> T;

	for(int i = 0;i<T;i++)
	{
		in >> N;
		int cnt_odd = 0,cnt_even = 0;
		for(int j = 0;j<N;j++)
		{
			in>> tmp;
			if(tmp %2 == 0){
				even_vec[cnt_even++] = tmp;
				vec[j] = 0;
			}else{
				odd_vec[cnt_odd++] = tmp;
				vec[j] = 1;
			}
		}

/*		cout<<"排序前的偶数序列:"<<endl;
		for(int k =0;k<cnt_even;k++)
			cout<<even_vec[k]<<" ";
		cout<<endl;*/

		sort(even_vec,even_vec+cnt_even,cmp);

	/*	cout<<"排序后的偶数序列:"<<endl;
		for(int k =0;k<cnt_even;k++)
			cout<<even_vec[k]<<" ";
		cout<<endl;*/


		/*cout<<"排序前的奇数序列:"<<endl;
		for(int k =0;k<cnt_odd;k++)
			cout<<odd_vec[k]<<" ";
		cout<<endl;*/

		sort(odd_vec,odd_vec+cnt_odd);

	/*	cout<<"排序后的奇数序列:"<<endl;
		for(int k =0;k<cnt_odd;k++)
			cout<<odd_vec[k]<<" ";
		cout<<endl;*/

		int e = 0,o = 0;

		/*
		for(int j = 0;j<N;j++)
		{
			if(vec[j] == 0){
				vec[j] = even_vec[e++];
			}else{
				vec[j] = odd_vec[o++];
			}
		}*/

		out<<"Case #"<<i+1<<":";
		for(int j = 0;j<N;j++)
		{
			if(vec[j] == 0){
				out<<" "<<even_vec[e++];
			}else{
				out<<" "<<odd_vec[o++];
			}
		}
		out<<endl;
	}


	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值