STR(nyoj 996)

本文介绍了一种特殊的字符串排序算法,该算法通过比较两两字符串组合后的大小来进行排序,并提供了一个C++实现示例。

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

一开始看难度是一,直接跳过不做了,结果。。。

大神博客地址http://blog.youkuaiyun.com/c_duoduo/article/details/51449537 thank you~


对字符串进行排序,要看两个字符串怎样组成的新字符串来排序。


#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
struct node{
	char a[60];
	bool operator < (const node& t)const{	//组成两个新的字符串,进行排序 
		char c1[110],c2[110];	//注意开的字符串大小,是两个要求的字符串 
		strcpy(c1,a);
		strcat(c1,t.a);
		 strcpy(c2,t.a);
		strcat(c2,a);
		return strcmp(c1,c2)>=0?true:false;//这里降序排列了,注意字符串排列要返回bool值 
	}
}a[200];	//字符串200够用了 
int main(){
	int T;
	scanf("%d",&T);
	getchar();
	getchar();
	while(T--){
		int n = 0;
		while(1){
			if(*gets(a[n].a)!=NULL){	//学习了,当读入'\n'则gets返回指针指向NULL 
				n++;
			}
			else break;
		}
		sort(a,a+n);
		for(int i=0;i<n;i++){
			printf("%s",a[i].a);
		}
		printf("\n");
		for(int i=n-1;i>=0;i--){
			printf("%s",a[i].a);
		}
		printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值