【C/结构体/字符串函数】碧蓝航线

3 篇文章 0 订阅
1 篇文章 0 订阅

做题目做到的,其实我并不玩碧蓝航线的,但是有具体情景就显得很实用?(划掉)也许可以用这个思路做一个痒痒鼠的程序,但是现在能力有限,而且不知道这么用爬虫扒取数据,所以实现起来有点困难。

况且现在痒痒鼠现在也内置御魂配置的功能了,所以不知道什么时候能写点实用的程序呀QAQ


 碧蓝航线

 原题目来自——万里ACM(wlacm.com)

(小声)哇,这个超链接编辑的时候好别扭,就不能双击这个就可以打开编辑的界面么,还要按上面的按钮...

 

 

题目描述

《碧蓝航线》是时下非常热门的一款游戏,是b站代理第二的游戏(第一FGO)。

《碧蓝航线》里面有一个演习模式,这个模式出击需要六艘战舰,前排3艘(前排战舰类型为DD(驱逐),CA(重巡),CL(轻巡)),后排3艘(后排战舰类型BB(战列),CV(航母))。每艘船都有战斗力,一般情况下总战斗力(六艘战斗力的和)和越大,演习的胜率越高。

因为前一段时间海军节,kirito迷上了《碧蓝航线》,他想最大可能性赢得演习的条件下,按阵营喜欢程度上阵战舰(总共4个阵营,白鹰、铁血、重樱、皇家),请你帮他选出6艘战舰,输出总战力和六艘战舰名字,若不行则输出NO。

 

输入

第1行给定一个正整数n,代表战舰数量(n<=100) 

第2行有4个值,代表kirito对4个阵营的喜爱程度(值越大越喜欢,1~4)

接下来n行,每行有4个量,分别为战舰名,长度(<20个字符),战斗力(<10000),类型(DD,CL,CA,BB,CV)和阵营(1白鹰,2铁血,3重樱,4皇家) 

输出

如果不行直接输出NO

如果可以第一行输出总战斗力,接下来3行按战斗力高到低输出前排战舰名字(若两艘战舰战力和阵营相同,则输出按字典序大的输出,提示strcmp函数比较字典序),接下来3行按战斗力高到低输出后排战舰名字(道理同上)

注:可以认为每一艘战舰都是独一无二的,即不会同名

 

输入样例

【样例1】
10
2 3 4 1
Enterprise 4000 CV 1
Nagato 4500 BB 3
Azuma 4200 CA 3
Hood 3800 BB 4
Eldridge 3900 DD 1
Yuudachi 3500 DD 3
Kaga 4000 CV 3
Akagi 4000 CV 3
PrinzEugen 4000 CA 2
Z46 3900 DD 2
【样例2】
6
1 2 3 4
22 9999 DD 1
33 9999 DD 4
Asuna 8888 CL 3
Minneapolis  8888 CA 1
Yamato 7777 BB 3
Musashi 7777 BB 3

输出样例

【样例1】
24600
Azuma
PrinzEugen
Z46
Nagato
Kaga
Akagi
【样例2】
NO    //后排无法组成故输出NO

 

AC代码

#include<stdio.h>
#include<string.h>
int main()
{
	struct battleship                    //定义结构体类型数组,用于存储不同战船的各类数据
	{
		char battleship_name[20];        //战舰名字
		int battleship_power;            //战舰战力
		char battleship_type[2];         //战舰类型缩写
		int battleship_camp;             //战舰阵营
		char battleship_position[10];    //战舰位置
	}battleship[100];                    //变量名为“battleship”的数据类型为——battleship的结构体数组
	int like[4], max[6] = { 0 };         //like数组储存对四个阵营的喜爱度,max数组储存最佳阵容的6艘船的元素序号
	int n, sum = 0, front = 0, behind = 0;    //分别储存 n:船只数量 sum:总战力 front:前排船只总数量 behind:后排船只总数量



	scanf("%d", &n);                    //输入船只数,如果小于6则无法组成,直接输出NO
	if (n < 6)
	{
		printf("NO\n");
		return 0;
	}
	for (int i = 0; i < 4; i++)scanf("%d", &like[i]);    //输入喜好程度

	for (int i = 0; i < n; i++)           //读取每艘船的数据
	{
		scanf("%s %d %s %d", battleship[i].battleship_name, &battleship[i].battleship_power, battleship[i].battleship_type, &battleship[i].battleship_camp);


//用于判断该类型船只属于前排还是后排
		if (strcmp(battleship[i].battleship_type, "DD") == 0 || strcmp(battleship[i].battleship_type, "CA") == 0 || strcmp(battleship[i].battleship_type, "CL") == 0)        //这里用strcmp函数来进行判断,不可直接用逻辑运算符!!!下同
		{
			front++;
			strcpy(battleship[i].battleship_position, "front");        //这里用strcpy来进行赋值,直接赋值好像并不行?下同
		}
		else
		{
			behind++;
			strcpy(battleship[i].battleship_position, "behind");
		}
	}



	if (front < 3 || behind < 3)        //如果前排或后排数量不足则无法组成,直接输出NO
	{
		printf("NO\n");
		return 0;
	}




	for (int i = 0; i < 3; i++)            //取出战力最高的3艘前排
	{
		for (int j = 0; j < n; j++)        //通过枚举法来查找
		{
			if (strcmp(battleship[j].battleship_position, "front") == 0)    //筛选前排
			{
				if (battleship[j].battleship_power > battleship[max[i]].battleship_power|| strcmp(battleship[max[i]].battleship_position, "front") != 0)max[i] = j;        //比较战力
				else if (battleship[j].battleship_power == battleship[max[i]].battleship_power)
				{
					if (like[battleship[j].battleship_camp - 1] > like[battleship[max[i]].battleship_camp - 1])max[i] = j;        //比较喜爱度
					else if (like[battleship[j].battleship_camp - 1] == like[battleship[max[i]].battleship_camp - 1])
					{
						if (strcmp(battleship[j].battleship_name, battleship[max[i]].battleship_name) > 0)max[i] = j;        //比较字典序
					}
				}
			}
		}
		sum += battleship[max[i]].battleship_power;            //计入总战力
		strcpy(battleship[max[i]].battleship_position, " ");   //已被选出的船不参与下次筛选
	}



//同上选出战力最高的3艘后排
	for (int i = 3; i < 6; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (strcmp(battleship[j].battleship_position, "behind") == 0)
			{
				if (battleship[j].battleship_power > battleship[max[i]].battleship_power|| strcmp(battleship[max[i]].battleship_position, "behind") != 0)max[i] = j;
				else if (battleship[j].battleship_power == battleship[max[i]].battleship_power)
				{
					if (like[battleship[j].battleship_camp - 1] > like[battleship[max[i]].battleship_camp - 1])max[i] = j;
					else if (like[battleship[j].battleship_camp - 1] == like[battleship[max[i]].battleship_camp - 1])
					{
						if (strcmp(battleship[j].battleship_name, battleship[max[i]].battleship_name) > 0)max[i] = j;
					}
				}
			}
		}
		sum += battleship[max[i]].battleship_power;
		strcpy(battleship[max[i]].battleship_position, " ");
	}



	printf("%d\n", sum);        //输出组成在最佳阵容战力
	for (int i = 0; i < 6; i++)    //输出阵容
	{
		printf("%s\n", battleship[max[i]].battleship_name);
	}



	return 0;
}

总结

 

做这个的时候刚刚自学完结构体,然后就想着用一下了,但是这道题目好像不是很能体现结构体的好处?(也许阅读程序的时候会更加容易理解吧)

不过也算是一次不错的尝试,在一开始的时候没有搞清楚结构体类型的类型名变量名。。。然后就。。。没定义变量直接用类型名代替了。。。(好蠢(っ °Д °;)っ!!)

最后还是AC了,之前的几次尝试中输入样例1的时候无法输出Z46这个数据,后来发现是max初值的问题,修改一下之后就好了~

感觉通过枚举法来找出max在数据数量很多的时候可能效率就会很低,感觉会有更好的方法。

感觉这题目还是很容易的,发上来纯粹图个乐,纪念一下第一次使用结构体类型!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值