• 蓝桥杯(3.6)


    1221. 四平方和

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		for(int i=0;i*i <= n;i++) {
    			for(int j=i;i*i + j*j <= n;j++) {
    				for(int k=j;i*i + j*j + k*k <= n;k++) {
    					int t1 = n-i*i-j*j-k*k;
    					int t2 = (int)Math.sqrt(t1);
    					if(t1 >= k*k && t2*t2 == t1) {//加上t1 >= k*k
    						System.out.println(i+" "+j+" "+k+" "+t2);
    						return ;
    					}
    				}
    			}
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		List<int[]> list = new ArrayList<>();//1
    		
    		for(int c = 0;c*c<=n;c++) {
    			for(int d=c;c*c+d*d<=n;d++) {
    				list.add(new int[] {c*c+d*d,c,d});
    			}
    		}//2
    		
    		list.sort(new Comparator<int[]>() {
    			public int compare(int[] o1, int[] o2) {
    				if(o1[0]!=o2[0])
    					return o1[0]-o2[0];
    				if(o1[1]!=o2[1])
    					return o1[1]-o2[1];
    				return o1[2]-o2[2];
    			};
    		});//3
    		
    		for(int a=0;a*a<=n;a++) {
    			for(int b=a;a*a+b*b<=n;b++) {
    				int t = n-a*a-b*b;
    				//二分
    				int l = 0,r = list.size()-1;
    				while(l<r) {
    					int mid = (l+r)/2;
    					if(list.get(mid)[0] >= t)	r = mid;
    					else	l = mid+1;
    				}
    				//l或者r
    				if(list.get(l)[0] == t) {
    					int c = list.get(l)[1];
    					int d = list.get(l)[2];
    					System.out.println(a+" "+b+" "+c+" "+d);
    					return ;
    				}
    			}
    		}
    		
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		Map<Integer,int[]> map = new HashMap<>();//1
    		
    		for(int c = 0;c*c<=n;c++) {
    			for(int d=c;c*c+d*d<=n;d++) {
    				Integer cd = c*c + d*d;
    				if(map.get(cd) == null)
    					map.put(cd,new int[] {c,d});
    			}
    		}//2
    		
    		for(int a=0;a*a<=n;a++) {
    			for(int b=a;a*a+b*b<=n;b++) {
    				Integer t = n-a*a-b*b;
    				if(map.get(t) != null) {
    					int c = map.get(t)[0];
    					int d = map.get(t)[1];
    					System.out.println(a+" "+b+" "+c+" "+d);
    					return ;
    				}
    			}
    		}
    		
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    795. 前缀和

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		int[] res = new int[n+1];
    		int[] a = new int[n+1];
    
    		for(int i=1;i<=n;i++)
    			res[i] = sc.nextInt();
    		//求前缀和数组
    		for(int i=1;i<=n;i++) {
    			a[i] = a[i-1] + res[i];
    		}
    		
    		while(m-- != 0) {
    			int l = sc.nextInt();
    			int r = sc.nextInt();
    			System.out.println(a[r]-a[l-1]);
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    796. 子矩阵的和

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    		int q = sc.nextInt();
    		int[][] a = new int[n+1][m+1];
    		int[][] b = new int[n+1][m+1];
    		
    		for(int i=1;i<=n;i++)
    			for(int j=1;j<=m;j++)
    				a[i][j] = sc.nextInt();
    
    		for(int i=1;i<=n;i++)
    			for(int j=1;j<=m;j++)
    				b[i][j] = b[i][j-1]+b[i-1][j]-b[i-1][j-1]+a[i][j];//初始化前缀和数组
    		
    		while(q-- != 0) {
    			int x1 = sc.nextInt();
    			int y1 = sc.nextInt();
    			int x2 = sc.nextInt();
    			int y2 = sc.nextInt();
    			System.out.println(b[x2][y2]-b[x2][y1-1]-b[x1-1][y2]+b[x1-1][y1-1]);
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    理解MVC、MVP、MVVM在干什么,进化的原因。
    罗丹明聚乙二醇氨基,Rhodamine-PEG-NH2,NH2-PEG-Rhodamine荧光标记
    《工程伦理》1-13章汇总
    凝思系统ftp只能下载文件,不能上传文件
    PGSQL中的LIKE,ILIKE,SIMILAR TO的使用
    Windows系统下,python安装netCDF4步骤
    【操作系统】调度算法
    python 生成html文件并端口展示
    Verilog代码之求勾股定理和arctan
    “Docker:革新应用程序开发和部署“
  • 原文地址:https://blog.csdn.net/qq_62552630/article/details/136514507