Leetcode刷题笔记28:201612-1-2-3

第二十五天 2021-4-8 备战CSP
刷题模块:CSP 201612-1-2

一、201609-1

如此垃圾的逻辑和代码,竟然一遍编译过,一遍AC过。

能AC的代码就是好代码。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1010;

int n;
int num[N];
int l,r,mid;

int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&num[i]);
	}
	
	sort(num,num+n);
	
	if(n%2==0){
		r=n/2;
		l=r-1;
		if(num[l]!=num[r]){
			puts("-1");
			return 0;
		}
		else mid=l;
	}
	else l=r=mid=n/2;
	
	while(num[l]==num[mid] && num[r]==num[mid]){
		l--;
		r++;
		if(l==0) break;
	}
	
	if(l==0 && num[l]==num[mid] && num[r]==num[mid]){
		printf("%d",num[mid]);
		return 0;
	}
	if(num[l]!=num[mid] && num[r]!=num[mid]){
		printf("%d",num[mid]);
		return 0;
	}
	
	puts("-1");
	return 0;
}

二、201609-2

一遍过,有点爽。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int S,T,M;

int tex[8]={0,3,10,20,25,30,35,45};
int gear[7]={35,15,30,45,260,200,250};
int domain[7]={3500,5000,8000,12500,38500,58500,83500};

int main(){
	scanf("%d",&T);
	
	int i;
	M=0;
	for(i=0;i<7;i++){
		if(T <= M + gear[i] * (100-tex[i])) break;
		M = M + gear[i] * (100-tex[i]);
	}
	
	S=domain[i-1]+((T-M)*100)/(100-tex[i]);
	
	printf("%d\n",S);
	
	return 0;
}

三、201609-3

一遍过,超级爽

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1010;

typedef struct Suite{
	int attVal;
	int heaVal;
	Suite* next;
	Suite():attVal(0),heaVal(0),next(nullptr){}
	Suite(int attVal, int heaVal):attVal(attVal),heaVal(heaVal),next(nullptr){}
}Suite;
Suite *head[2];

int n;
int pos,att,hea,atterPos,deferPos;
int player[2];
int turn;

char summonStr[]="summon";
char attackStr[]="attack";
char endStr[]="end";
char opStr[100];

//return the pointer before pos
Suite* findSuite(int pos,int myturn){
	Suite* pot=head[myturn];
	while(--pos && pot->next!=nullptr){
		pot=pot->next;
	}
	return pot;
}
//remove suite from pos
void remove(int pos,int myturn){
	Suite* pot=findSuite(pos,myturn);
	pot->next=pot->next->next;
}
//add a suite in pos
void summon(){
	Suite* s=new Suite(att,hea);
	Suite* pot=findSuite(pos,turn);
	s->next=pot->next;
	pot->next=s;
}

void attack(){
	Suite* atter=findSuite(atterPos,turn)->next;
	int anoPlayer=(turn+1)%2;
	if(deferPos==0){
		player[anoPlayer]-=atter->attVal;
	}
	else{
		Suite* defer=findSuite(deferPos,anoPlayer)->next;
		atter->heaVal-=defer->attVal;
		defer->heaVal-=atter->attVal;
		if(atter->heaVal<=0) remove(atterPos,turn);
		if(defer->heaVal<=0) remove(deferPos,anoPlayer);
	}
}

void init(){
	head[0]=new Suite();
	head[1]=new Suite();
	turn=0;
	player[0]=player[1]=30;
}

void output(){
	//line 1
	if(player[1]<=0) puts("1");
	else if(player[0]<=0) puts("-1");
	else puts("0");
	//line 2
	printf("%d\n",player[0]);
	//line 3
	int count=0;
	Suite* temp=head[0]->next;;
	while(temp!=nullptr){
		count++;
		temp=temp->next;
	} 
	printf("%d ",count);
	temp=head[0]->next;
	while(temp!=nullptr){
		printf("%d ",temp->heaVal);
		temp=temp->next;
	}
	printf("\n");
	//line 4
	printf("%d\n",player[1]);
	//line 5
	count=0;
	temp=head[1]->next;
	while(temp!=nullptr){
		count++;
		temp=temp->next;
	} 
	printf("%d ",count);
	temp=head[1]->next;
	while(temp!=nullptr){
		printf("%d ",temp->heaVal);
		temp=temp->next;
	}
	printf("\n");
}

int main()
{
	init();
	scanf("%d", &n);
	while(n--){
		scanf("%s", opStr);
		if(!strcmp(opStr, summonStr)){
			scanf("%d %d %d", &pos, &att, &hea);
			summon();
		}
		else if(!strcmp(opStr, attackStr)){
			scanf("%d %d", &atterPos, &deferPos);
			attack();
		}
		else if(!strcmp(opStr, endStr)){
			turn=(turn+1)%2;
		}
		else{
			printf("Error\n");
		}
	}
	output();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值