201912-2 回收站选址

这篇博客探讨了两种不同的回收站选址算法的实现,包括代码细节和思路。第一种算法是基于自定义类 Rush 的简单实现,计算两点间的距离平方;第二种算法则使用二维数组,考虑了相邻点的个数和对角点的判断,优化了问题的解决。博客重点在于算法的逻辑和优化,适合对算法和数据结构感兴趣的读者深入理解。
摘要由CSDN通过智能技术生成

回收站选址

在这里插入图片描述
在这里插入图片描述

自己写的代码(只有20分)

package practice;

import java.util.ArrayList;
import java.util.Scanner;

public class test19092 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a = 0;
		int b = 0;
		int c = 0;
		int d = 0;
		int e = 0;
		ArrayList<Rush> arrays = new ArrayList<Rush>();
		for (int i = 0; i < n; i++) {
			arrays.add(new Rush(sc.nextInt(),sc.nextInt()));
		}
		for (int i = 0; i < n; i++) {
			String str = "";
			for (int j = 0; j < n; j++) {
				int t = arrays.get(i).recycling(arrays.get(j));
				str +=String.valueOf(t); 
			}
			if (count(str, 1)==4) {
				switch (count(str, 2)) {
				case 0:
					a++;
					break;
				case 1:
					b++;
					break;
				case 2:
					c++;
					break;
				case 3:
					d++;
					break;
				case 4:
					e++;
					break;
				}
				str="";
				continue;
			}else {
				str="";
				continue;
			}
			
		}
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
	
	}
	static int count(String s1,int n)
	{
		  int counter=0;  
	        if (s1.indexOf(String.valueOf(n)) == -1) {    
	            return 0;  
	        }   
	            while(s1.indexOf(String.valueOf(n))!=-1){  
	                counter++;  
	                s1=s1.substring(s1.indexOf(String.valueOf(n))+String.valueOf(n).length());  
	            }  
	            return counter;    
	}
	
}
class Rush{
	private int x;
	private int y;
	public Rush(int x,int y) {
		this.x = x;
		this.y = y;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getX() {
		return x;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getY() {
		return y;
	}
	public int recycling(Rush rush) {
		int t = (rush.getX()-this.x)*(rush.getX()-this.x)+(rush.getY()-this.y)*(rush.getY()-this.y);
		return t;
	}
}

满分答案

import java.util.*;
public class Main {
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        int array[][]=new int[n][2];
        int a[]=new int[n];
        int b[]={0,0,0,0,0};
        for(int i=0;i<n;i++){
            a[i]=0;//a[]用于记录每个点相邻点个数
            for(int j=0;j<2;j++){
                array[i][j]=input.nextInt();
            }
        }
        for(int i=0;i<n;i++){//对每个点相邻点个数进行计算
            for(int j=0;j<n;j++){
                if(j==i)
                    continue;
                else{
                    if(array[i][0]-1==array[j][0]&&array[i][1]==array[j][1]){
                            a[i]++;
                    }
                    if(array[i][0]+1==array[j][0]&&array[i][1]==array[j][1]){
                            a[i]++;
                    }
                    if(array[i][1]-1==array[j][1]&&array[i][0]==array[j][0]){
                            a[i]++;
                    }
                    if(array[i][1]+1==array[j][1]&&array[i][0]==array[j][0]){
                            a[i]++;
                    }
                }
            }
        }
        for(int k=0;k<n;k++){//计算每个对角点个数
            int count=0;
            if(a[k]==4){
                    for (int j = 0; j < n; j++) {
                        if (array[k][0] - 1 == array[j][0]&&array[k][1] - 1 == array[j][1]) {
                                count++;
                        }
                        if (array[k][0] - 1 == array[j][0]&&array[k][1] + 1 == array[j][1]) {

                                count++;
                        }
                        if (array[k][0] + 1 == array[j][0]&&array[k][1] - 1 == array[j][1]) {

                                count++;
                        }
                        if (array[k][0] + 1 == array[j][0]&&array[k][1] + 1 == array[j][1]) {

                                count++;
                        }
                    }
                    b[count]++;
            }
        }
        for(int l=0;l<5;l++){
            System.out.println(b[l]);
        }
    }
}

总结

正确答案的思路为,设置一个二维数组(需要学习),然后进行两次判断,第一次是判断相邻点的个数,就是一个循环来判断,相邻为一的个数,依次循环,若碰到自己,就跳过,然后,通过加减一位横纵坐标的方式,来判断上下左右,是否可以和每个点重合,其中相邻的个数用数组来记录(是个很好的方式),使用数组的时候可以很好的记录那些点为有相邻点(用的二维数组记录,根据二维数组的行),判断重合即可。同样用数组记录个数。

技能:二维数组,使用数组来记录数量。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值