1、交换扑克牌

给定了两数组,规定只能交换一个元素,使得两个数组的总和相同。

package Gloton;
import java.util.Scanner;
//交换两个数组中某个元素使得两个数组元素和相同
//第一组测试用例
//1 1
//2 2
//第二组测试用例
//2
//2 1 3
public class GlodonMain2 {
    public static  int[] swap (int[]A ,int[]B){

        int[] result = new int[2];
        if(A == null || B == null){
            return result;
        }
        int sum =0;
        int sumA = 0;
        for (int i = 0; i < A.length; i++) {
            sum+=A[i];
            sumA+=A[i];
        }
        for (int i = 0; i < B.length; i++) {
            sum+=B[i];
        }
        int diff = sum/2 - sumA;
        for (int i = 0; i <  A.length; i++) {
            for (int j = 0; i <  B.length; i++) {
                if (A[i]-B[j]==-diff){
                    result[0]=A[i];
                    result[1]=B[i];
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String str1 = input.nextLine();
        String[] s1 = str1.split(" ");
        int[] A = new int[s1.length];
        for (int i = 0; i < s1.length; i++) {
            A[i] = Integer.parseInt(s1[i]);
        }
        String str2 = input.nextLine();
        String[] s2 = str1.split(" ");
        int[] B = new int[s2.length];
        for (int i = 0; i < s2.length; i++) {
            B[i] = Integer.parseInt(s2[i]);
        }
        int[] result = swap (A,B);
        System.out.println(result[0]+" "+ result[1]);
    }
}

2、圆中含有的方块最大数

alt alt

package Gloton;
//计算半径为R的原包含多少个1*1的格子
import java.util.Scanner;
public class GlodonMain4 {
    public static int  area(int r){
        int count = 0;
        if(r <= 1){
            return count;
        }
        //将图分红四份,按照左上角计算
        for(int i = 1 ;i <= r ; i++){
            for(int j = 1 ; j <= r ; j++){
                double distance = Math.sqrt(i*i+j*j);
                if (distance < r )
                    count++;
            }
        }
        return  count*4;
    }
    public static void main(String[] args) {
        Scanner input  = new Scanner(System.in);
        int r = input.nextInt();
        System.out.println(area(r));
    }
}