ITPub博客

首页 > IT职业 > IT生活 > 算法 test 五个用例

算法 test 五个用例

IT生活 作者:ewen321 时间:2021-09-09 09:03:07 0 删除 编辑

package t170427;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Solution170427_weimeng {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int test_case=sc.nextInt();//测试用例的数量
for(int t=0;t List boatList=new ArrayList();
int boatCount=sc.nextInt();//船的数量

for(int i=0;i Boat boat=new Boat(i+1,sc.nextInt(),sc.nextInt());
boatList.add(boat);
}
Boat b=Collections.min(boatList);
System.out.println("#"+(t+1)+" "+b.index);
}

sc.close();

}

}
class Boat implements Comparable{
Boat(int index,int x,int y){
this.index=index;
this.x=x;
this.y=y;
this.distance=xx+yy;
if(x>0 && y>0){
this.xiangxian=1;
}
if(x>0 && y this.xiangxian=2;
}
if(x this.xiangxian=3;
}
if(x0){
this.xiangxian=4;
}
}

int index;
int x;
int y;
int xiangxian;
int angle;
int distance;
@Override
public int compareTo(Boat boat) {
if(this.xiangxian return -1;
}
if(this.xiangxian>boat.xiangxian){
return 1;
}

if(this.xiangxian==1 || boat.xiangxian==3){
//x1/y1>x2/y2 return 1
if(Math.abs(this.xboat.y) >Math.abs(boat.xthis.y)){
return 1;
}
if(Math.abs(this.xboat.y) this.y)){
return -1;
}
}
if(this.xiangxian==2 || boat.xiangxian==4){
//y1/x1>y2/x2 return 1
if(Math.abs(this.yboat.x) >Math.abs(boat.ythis.x)){
return 1;
}
if(Math.abs(this.yboat.x) this.x)){
return -1;
}
}

//象限一样,夹角一样,继续判断距离
if(this.distance>boat.distance){
return 1;
}else{
return -1;
}

}

}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Solution {

static int N; // 书的种类
static int K; // 每种书内必读书的个数
static int M; // 读书周期
static int[][] Input = null; // 每本必读书的点赞数
static int sum = 0;
static List list = null;


public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
int test_case = sc.nextInt();
for (int i = 0; i sum = 0;
N = sc.nextInt();
K = sc.nextInt();
M = sc.nextInt();

Input = new int[N][K];
for (int j = 0; j for (int k = 0; k Input[j][k] = sc.nextInt();
}
}
fnMaxSum(i);
}
sc.close();

}

private static void fnMaxSum(int test_case) {

list = new ArrayList();
for (int j = 0; j Arrays.sort(Input[j]);
}
for (int j = 0; j for (int k = K - 1; k >= (K - M / 2) && k >= 0; k--) {
list.add(Input[j][k]);
}
}
Collections.sort(list);
for (int j = list.size() - 1; j > list.size() - M - 1 && j >= 0; j--) {
sum += list.get(j);
}
System.out.println("#" + (test_case + 1) + " " + sum);
}

}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Solution {

static int TestCase;
static int answer;
static int N,M,K;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TestCase=sc.nextInt();
for(int t=1;t sc.nextLine();
N=sc.nextInt();
M=sc.nextInt();
K=sc.nextInt();
answer=M+1;
Map> relative=new HashMap>();
for(int i=1;i sc.nextLine();
int k1=sc.nextInt();
int k2=sc.nextInt();
ArrayList arr=relative.get(k1);
if(arr==null){
arr=new ArrayList();
}
arr.add(k2);
relative.put(k1, arr);
}
getNum(1,1,relative);
if(answer==M+1){
answer=-1;
}
System.out.println("#"+t+" "+answer);
}
}

public static void getNum(int num,int key,Map> rela){
if(num if(num answer=num;
}
return;
}else{
if(num>M || rela.get(key)==null){
return;
}else{
num++;
for(int i=0;i getNum(num,rela.get(key).get(i),rela);
}
}
}
}
}
package t170223;

import java.util.Scanner;

/**

  • 陈潇2017.02.23考试的题目
  • @author meng3.wei
  • @date 20170419
  • 题目很容易,但是没有练习,就是编写不出来
  • 公司经常会有部门出现人力缺口,那么人事部门就会招聘人员。但每个应聘人员都想去自己喜爱的部门,而招聘担当也想满足各位的愿望。

所以限定条件:

1.每个部门只出现1个人力缺口,而应聘人力也是一对一。即N个部门 N个人力

2.应聘人力为 3

招聘担当为此想出了一个办法来最大满足各位的需求:

即每个人对每个部门进行打分 4个部门时 4个人就进行打分 最高分4分 最低分1分 分数不重复

打分表如下:

部门1

部门2

部门3

部门4

人力1

4

2

3

1

人力2

2

3

4

1

人力3

3

1

2

4

人力4

4

3

1

2

然后招聘担当尽量去每个部门满意度最高的人去相应部门,当然会有多人最想去的部门相同,但一个部门只能去一个人。所以需要在不同的组合选择中。选择出总得分最高的。

即

方式一:人力1->部门1 人力2->部门3 人力3->部门4 人力4-> 部门2 总得分为 4+4+4+3 =15

方式二:人力1->部门2 人力2->部门3 人力3->部门4 人力4-> 部门1 总得分为 2+4+4+4 =14

方式三:.......

方式四:.......

等等。。。。

要求我们给出所有方式组合中 总得分最高的 分数 即输出15这个值

Test case 输入

2 --case 总数

4 --部门数

4 2 3 1 -- 每个人的打分 依次是1~N的部门

2 3 4 1

3 1 2 4

4 3 1 2

5 --部门数

4 2 3 1 5-- 每个人的打分 依次是1~N的部门

2 3 5 1 4

3 5 2 4 1

4 3 5 1 2

1 4 3 2 5

输出

1 15
2 XX --(自己编的case我没算输出)


/
public class Solution170223_weimeng {
static int[][] deptScore;
static int maxScore=0;
public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
//int test_case=sc.nextInt();
int test_case=1;
for(int t=0;t int deptSize=sc.nextInt();
deptScore=new int[deptSize][deptSize];
int[] array=new int[deptSize];
for(int i=0;i array[i]=i;
for(int j=0;j deptScore[i][j]=sc.nextInt();
}
}
process(array,0,array.length-1);
System.out.println(maxScore);
}

sc.close();

/String str="123";
process(str.toCharArray(),0,str.length()-1);
/
}

public static void process(int[] array,int start,int end){
if(start==end){
//System.out.println(String.valueOf(array));
int currentScore=countCurrentScore(array,deptScore);
if(currentScore>maxScore){
maxScore=currentScore;
}

}else{
for(int index=start;index int temp=array[start];
array[start]=array[index];
array[index]=temp;
process(array,start+1,end);
temp=array[start];
array[start]=array[index];
array[index]=temp;
}
}
}

public static int countCurrentScore(int[] array,int [][] deptScore){
int count=0;
for(int i=0;i count=count+deptScore[i][array[i]];
}
return count;
}

}

package Solution02_yourname;

import java.util.Scanner;

public class Solution512_lizhun {

static int T,N,small,big;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
for (int testcase = 1; testcase N = sc.nextInt();
small = big = 0;
if (N%2 != 0) {//奇数
small = 2;
if (isSuShu(N - small)) {
big = N - small;
}
} else { //偶数
int x = N/2;
for(int i = x;i > 1; i--){
//如果已经找到了big和small就跳出循环
if (big != 0) {
break;
}

 for (int j = 2; j 

}
printRes(testcase);
}

}
private static void printRes(int testcase) {
if (big == 0) {
System.out.println("#" + testcase + " -1");
} else {
System.out.println("#" + testcase + " " + small + " " + big);
}
}
private static boolean isSuShu(int i) {
for (int j = 2; j if (i%j == 0) {
return false;
}
}
return true;
}

}
package t170223;

import java.util.Scanner;

/**

  • 陈潇2017.02.23考试的题目
  • @author meng3.wei
  • @date 20170419
  • 题目很容易,但是没有练习,就是编写不出来
  • 公司经常会有部门出现人力缺口,那么人事部门就会招聘人员。但每个应聘人员都想去自己喜爱的部门,而招聘担当也想满足各位的愿望。

所以限定条件:

1.每个部门只出现1个人力缺口,而应聘人力也是一对一。即N个部门 N个人力

2.应聘人力为 3

招聘担当为此想出了一个办法来最大满足各位的需求:

即每个人对每个部门进行打分 4个部门时 4个人就进行打分 最高分4分 最低分1分 分数不重复

打分表如下:

部门1

部门2

部门3

部门4

人力1

4

2

3

1

人力2

2

3

4

1

人力3

3

1

2

4

人力4

4

3

1

2

然后招聘担当尽量去每个部门满意度最高的人去相应部门,当然会有多人最想去的部门相同,但一个部门只能去一个人。所以需要在不同的组合选择中。选择出总得分最高的。

即

方式一:人力1->部门1 人力2->部门3 人力3->部门4 人力4-> 部门2 总得分为 4+4+4+3 =15

方式二:人力1->部门2 人力2->部门3 人力3->部门4 人力4-> 部门1 总得分为 2+4+4+4 =14

方式三:.......

方式四:.......

等等。。。。

要求我们给出所有方式组合中 总得分最高的 分数 即输出15这个值

Test case 输入

2 --case 总数

4 --部门数

4 2 3 1 -- 每个人的打分 依次是1~N的部门

2 3 4 1

3 1 2 4

4 3 1 2

5 --部门数

4 2 3 1 5-- 每个人的打分 依次是1~N的部门

2 3 5 1 4

3 5 2 4 1

4 3 5 1 2

1 4 3 2 5

输出

1 15
2 XX --(自己编的case我没算输出)


/
public class Solution170223_weimeng {
static int[][] deptScore;
static int maxScore=0;
public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
//int test_case=sc.nextInt();
int test_case=1;
for(int t=0;t int deptSize=sc.nextInt();
deptScore=new int[deptSize][deptSize];
int[] array=new int[deptSize];
for(int i=0;i array[i]=i;
for(int j=0;j deptScore[i][j]=sc.nextInt();
}
}
process(array,0,array.length-1);
System.out.println(maxScore);
}

sc.close();

/String str="123";
process(str.toCharArray(),0,str.length()-1);
/
}

public static void process(int[] array,int start,int end){
if(start==end){
//System.out.println(String.valueOf(array));
int currentScore=countCurrentScore(array,deptScore);
if(currentScore>maxScore){
maxScore=currentScore;
}

}else{
for(int index=start;index int temp=array[start];
array[start]=array[index];
array[index]=temp;
process(array,start+1,end);
temp=array[start];
array[start]=array[index];
array[index]=temp;
}
}
}

public static int countCurrentScore(int[] array,int [][] deptScore){
int count=0;
for(int i=0;i count=count+deptScore[i][array[i]];
}
return count;
}

}

package Solution02_yourname;

import java.util.Scanner;

public class Solution512_lizhun {

static int T,N,small,big;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
T = sc.nextInt();
for (int testcase = 1; testcase N = sc.nextInt();
small = big = 0;
if (N%2 != 0) {//奇数
small = 2;
if (isSuShu(N - small)) {
big = N - small;
}
} else { //偶数
int x = N/2;
for(int i = x;i > 1; i--){
//如果已经找到了big和small就跳出循环
if (big != 0) {
break;
}

 for (int j = 2; j 

}
printRes(testcase);
}

}
private static void printRes(int testcase) {
if (big == 0) {
System.out.println("#" + testcase + " -1");
} else {
System.out.println("#" + testcase + " " + small + " " + big);
}
}
private static boolean isSuShu(int i) {
for (int j = 2; j if (i%j == 0) {
return false;
}
}
return true;
}

}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4325/viewspace-2807847/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    65
  • 访问量
    639