大家好,我是被白菜拱的猪。
一个热爱学习废寝忘食头悬梁锥刺股,痴迷于girl的潇洒从容淡然coding handsome boy!
Time goes quickly!啊!没想到大二就这么稀里糊涂的过去,由于疫情原因,终究是没有开学,一学期啥也没听的我,最后还要做这个期末算法设计,不做不行啊,这日后可咋整啊,屁都不会,哪家公司要你。
只要功夫深,铁杵磨成绣花针。十道算法题,在没有听一节课的情况下,通过自学在查查百度看看能做出多少,在这里记录一下,奥利给!!!
我真是醉了,一道题竟然做一下午,是瞧不起我java吗???通样的思路,c++过去了,java没过去,靠!!!!
第一题
The alpaca is very gentle and timid. If someone feeds it, the alpaca must wait for someone to walk away before eating, even if the owner is familiar with it. However, it sometimes loses its temper and knows what pain is. Alpacas will spit on the faces of other animals when they are not happy.
lct have a group of alpacas, each alpaca has a number id and the alpaca are divided into two groups A and B to fight. Each alpaca has HP x
i
and each alpaca will spit, the i−th alpaca spit have a damagevalue c
i
.
alpaca in group A and B will sort Ascending order by id
The alpaca is particularly stupid. Each attack will only attack the living alpaca with the lowest number. When the alpaca’s HP≤0 the alpaca retires and other alpaca can not attack it, when all alpaca in a group have retired, the other group will win.
Group A will first attack.
Alpacas will attack in order of id from small to large
Alpacas don’t attack dead alpaca!!!
Input
The first line have three integer n,A,B---- there have n alpacas , group A have A alpacas, group B have B alpacas. (1≤n≤100,A+B=n,1≤A,B≤n)
The next n lines each line have two integer x
i
,c
i
— number i alpaca’s HP and damagevalue (1≤x
i
,c
i
≤100)
The next line have A integer – group A alpaca’s id
The next line have B integer – group B alpaca’s id
Output:
In the first line print the winner group have save how many alpacas.
In the next line:
If group A win output the Character painting like example.
If group B win,output “not A”.
Example
input
5 3 2
3 1
4 2
5 3
2 1
7 5
1 5 4
2 3
output
2
/yt/yt/yt/yt
/yt /yt
/yt /yt /yt
/yt /yt/yt
/yt /yt
/yt/yt /yt
/yt /yt
/yt /yt
/yt /yt
/yt /yt
/yt /yt
/yt /yt/yt/yt/yt/yt/yt/yt/yt/yt
/yt /yt
/yt /yt/yt
/yt /yt /yt
/yt/yt/yt/yt/yt/yt/yt/yt/yt/yt/yt
/yt /yt /yt /yt
/yt /yt /yt /yt
/yt /yt /yt /yt
/yt /yt /yt /yt
input
5 2 3
3 1
4 2
5 3
2 1
7 5
2 3
1 5 4
output
1
not A
Hint
Note : Character painting end have no more Space
because alpaca in group A and B will sort Ascending order by id
A: (id=1, x= 3, c=1), (id=5, x=7, c=5), (id=4, x=2, c=1)
B: (id=2, x=4, c=2), (id=3, x=5, c=3)
is become
A: (id=1, x= 3, c=1), (id=4, x=2, c=1), (id=5, x=7, c=5)
B: (id=2, x=4, c=2), (id=3, x=5, c=3)
in the first round attack:
A attack B:
(id=1, x= 3, c=1) attack (id=2, x=4, c=2) -> (id=2, x=3, c=2)
(id=4, x=2, c=1) attack (id=2, x=3, c=2) -> (id=2, x=2, c=2),
(id=5, x=7, c=5) attack (id=2 x=2, c=1) - > (id=2, x=-3, c=2) (id=2) is retires
B attack A:
(id=3.x=5,c=3) attack (id=1,x=3,c=1) -> (id=1,x=0,c=1) (id=1) is retires
A attack B:
(id=4, x=2, c=1) attack (id=3, x=5, c=3) -> (id=3, x=4, c=3)
(id=5, x=7, c=5) attack (id=3, x=5, c=3) -> (id=3, x=-1, c=3)
end
Group A win, and have two alpacas alive.
**
不管,java没过我也要贴上去
**
java版
/**
*
*/
package com.java.algorithm;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @ClassName: Demo1
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Lily
* @date 2020年6月18日
*/
public class Demo1 {
/**
* 定义羊驼类
*/
class Alpaca {
int id;
int HP;
int damage;
Alpaca(int id, int HP, int damage) {
this.id = id;
this.HP = HP;
this.damage = damage;
}
}
public static void main(String[] args) {
// 输入
Scanner scan = new Scanner(System.in);
// 数量
int num = scan.nextInt();
int numA = scan.nextInt();
int numB = scan.nextInt();
// 定义一个数组,用来接收羊驼
Alpaca[] alpaca = new Alpaca[num];
// 定义组A,B
List<Alpaca> groupA = new ArrayList<Alpaca>();
List<Alpaca> groupB = new ArrayList<Alpaca>();
for (int i = 0; i < num; i++) {
int hp = scan.nextInt();
int damage = scan.nextInt();
// 内部类,先new 外部类在.内部类
alpaca[i] = new Demo1().new Alpaca(i + 1, hp, damage);
}
/*
* for(int i=0;i<num;i++){ System.out.println(alpaca[i].id); }
*/
// 分队
for (int i = 0; i < numA; i++) {
groupA.add(alpaca[scan.nextInt() - 1]);
}
for (int i = 0; i < numB; i++) {
groupB.add(alpaca[scan.nextInt() - 1]);
}
// 对A组进行排序
Collections.sort(groupA, new Comparator<Alpaca>() {
public int compare(Alpaca a1, Alpaca a2) {
// 按照Alpaca的id进行升序排列
if (a1.id > a2.id) {
return 1;
}
return -1;
}
});
// 对B组进行排序
Collections.sort(groupB, new Comparator<Alpaca>() {
public int compare(Alpaca a1, Alpaca a2) {
// 按照Alpaca的id进行升序排列
if (a1.id > a2.id) {
return 1;
}
return -1;
}
});
//战斗,先遍历A
while(groupA.size()>0&&groupB.size()>0){
//A先对B攻击
for(int j=0;j<groupA.size();j++){
groupB.get(0).HP-=groupA.get(j).damage;
//假如B没写了就remove掉
if(groupB.get(0).HP<=0){
groupB.remove(0);
}
}
//然后B对A攻击
for(int j=0;j<groupB.size();j++){
groupA.get(0).HP-=groupB.get(j).damage;
//假如B没写了就remove掉
if(groupA.get(0).HP<=0){
groupA.remove(0);
}
}
}
if(groupA.size()>groupB.size()){
System.out.println(groupA.size());
System.out.println(" /yt/yt/yt/yt");
System.out.println(" /yt /yt");
System.out.println(" /yt /yt /yt");
System.out.println(" /yt /yt/yt");
System.out.println("/yt /yt");
System.out.println("/yt/yt /yt");
System.out.println(" /yt /yt");
System.out.println(" /yt /yt");
System.out.println(" /yt /yt");
System.out.println(" /yt /yt");
System.out.println(" /yt /yt");
System.out.println(" /yt /yt/yt/yt/yt/yt/yt/yt/yt/yt");
System.out.println(" /yt /yt");
System.out.println(" /yt /yt/yt");
System.out.println(" /yt /yt /yt");
System.out.println(" /yt/yt/yt/yt/yt/yt/yt/yt/yt/yt/yt");
System.out.println(" /yt /yt /yt /yt");
System.out.println(" /yt /yt /yt /yt");
System.out.println(" /yt /yt /yt /yt");
System.out.println(" /yt /yt /yt /yt");
}else{
System.out.println(groupB.size());
System.out.println("not A");
}
}
}
c++
#include <iostream>
#include<algorithm>
#include <string>
using namespace std;
//对数组进行排序
//void sort(int* arr, int num)
//{
// for (int i = 0; i < num - 1; i++)
// {
// if (arr[i] > arr[i + 1])
// {
// int temp;
// temp = arr[i]; arr[i] = arr[i + 1];
// arr[i + 1] = temp;
// }
// }
//}
void forword(int* arr, int num) {
for (int i = 0; i < num - 1; i++) {
arr[i] = arr[i + 1];
}
}
int main() {
int hp[100]; //存放血量
int damage[100];//存放攻击值
int A[100]; //A组成员
int B[100];//B组成员
int n, a, b;
string str = R"( /yt/yt/yt/yt
/yt /yt
/yt /yt /yt
/yt /yt/yt
/yt /yt
/yt/yt /yt
/yt /yt
/yt /yt
/yt /yt
/yt /yt
/yt /yt
/yt /yt/yt/yt/yt/yt/yt/yt/yt/yt
/yt /yt
/yt /yt/yt
/yt /yt /yt
/yt/yt/yt/yt/yt/yt/yt/yt/yt/yt/yt
/yt /yt /yt /yt
/yt /yt /yt /yt
/yt /yt /yt /yt
/yt /yt /yt /yt
)";
//输入
int i, j, k;
cin >> n >> a >> b;
for (i = 0; i < n; i++) {
cin >> hp[i] >> damage[i];
}
for (j = 0; j < a; j++) {
cin >> A[j];
}
for (k = 0; k < b; k++) {
cin >> B[k];
}
//分别对A,B排序
sort(A, A+a);
sort(B, B+b);
int first;
//排序完之后开始攻击
while (a > 0 && b > 0) {
//A对B攻击
for (i = 0; i < a; i++) {
//B取出第一个
first = B[0]; //B中存放的是序号
hp[first - 1] = hp[first - 1]- damage[A[i] - 1];
//假如第一个打没血了,就换成第二个
if (hp[first - 1] <= 0) {
//整个队伍向前继续挨打
forword(B, b);
b = b - 1;
}
}
for (i = 0; i < b; i++) {
//A取出第一个
first = A[0]; //A中存放的是序号
hp[first - 1] -= damage[B[i] - 1];
//假如第一个打没血了,就换成第二个
if (hp[first - 1] <= 0) {
//整个队伍向前继续挨打
forword(A, a);
a = a - 1;
}
}
}
if (a > b) {
cout << a << endl;
cout << str;
}
else {
cout << b << endl;
cout << "not A";
}
return 0;
}
不行了,这算法真的是要人命。