C语言之最长队名

欢迎进入我的C语言世界

题目

Problem Description

Jack所在的班级决定组团报名参加FZU校赛。为了体现班级的团结和睦,班长决定用班级所有人的名字连起来组成一个史上最长最醒目的队名。

因为听说在分数相同的情况下,队名字典序小的会排在更前面,班长还希望连成的史上最长队名拥有最小的字典序。

Input

输入数据第一行包含一个整数T,表示测试数据的组数。对于每组测试数据:

第一行为一个整数n(0<n<=10000),表示班级成员数。

接下来n行为班级每个人的名字。名字由小写字母组成,每个人名字长度均相同。

Output

对于每组测试数据,输出一行,表示连接成的史上最长队名。

Sample Input

1
3
jim
tom
joe

Sample Output

jimjoetom

答案

下面展示 实现代码一

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string arr[10001];//*****
int cmp(string c, string d)//比较名字的长度 
{
    
    
	if(c < d)
	{
    
    
		return 1;
	}
	else 
	{
    
    
		return 0;
	}
}
int main()
{
    
    
	int T;
	int i, j;
	while(scanf("%d",&T) != EOF)
	{
    
    
		for(i = 0; i < T; i++)
		{
    
    
			int n;
			cin >> n;
			for(j = 0; j < n; j++)
			{
    
    
				cin >> arr[j];
			}
			sort(arr, arr + n, cmp);		
			for(j = 0; j < n; j++)
			{
    
    
				cout << arr[j];
			}
			cout << endl;
		}
	} 
	return 0;
} 

下面展示 实现代码二

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
struct Node{
    
    
	char name[1000];
}node[10001];
int cmp(Node a, Node b)//比较名字的长度 
{
    
    
	if(strcmp(a.name, b.name) < 0)
	{
    
    
		return 1;
	}
	else 
	{
    
    
		return 0;
	}
}
int main()
{
    
    
	int T;
	int i, j;
	while(scanf("%d",&T) != EOF)
	{
    
    
		for(i = 0; i < T; i++)
		{
    
    
			int n;
			cin >> n;
			for(j = 0; j < n; j++)
			{
    
    
				cin >> node[j].name;
			}
			sort(node, node + n, cmp);		
			for(j = 0; j < n; j++)
			{
    
    
				cout << node[j].name;
			}
			cout << endl;
		}
	} 
	return 0;
} 

本题感悟

本块内容可能来自课本或其他网站,若涉及侵权问题,请联系我进行删除,谢谢大家啦~

这题对string的应用以及struct的应用和sort的应用。
以上。

猜你喜欢

转载自blog.csdn.net/hongguoya/article/details/105626811
今日推荐