【LQ系列】 ALGO-1~ALGO-2

进入算法训练部分,感到难度进一步提升,现在卡在“K好数”上,先上传前两道题:


【ALGO-1】 算法训练 区间k大数查询

问题描述

给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个。

输入格式

第一行包含一个数n,表示序列长度。

第二行包含n个正整数,表示给定的序列。

第三个包含一个正整数m,表示询问个数。

接下来m行,每行三个数l,r,K,表示询问序列从左往右第l个数到第r个数中,从大往小第K大的数是哪个。序列元素从1开始标号。

输出格式
总共输出m行,每行一个数,表示询问的答案。
样例输入
5
1 2 3 4 5
2
1 5 2
2 3 2
样例输出
4
2
数据规模与约定

对于30%的数据,n,m<=100;

对于100%的数据,n,m<=1000;

保证k<=(r-l+1),序列中的数<=106


Code:

import java.util.Arrays;
import java.util.Scanner ;

public class Main {

	// 在A中的[l-1, r-1]中寻找第K大的数
	private static int find( int[] A, int l, int r, int K ) {
		int[] B = new int[r-l+1] ;
		for( int i = l-1; i <= r-1; i ++ )
			B[i-(l-1)] = A[i] ;
		Arrays.sort(B) ;
		return B[(B.length-1)-K+1] ;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner( System.in ) ;
		int n = sc.nextInt() ;	// 序列长度
		int[] A = new int[n] ;	// 给定序列
		for( int i = 0; i < n; i ++ ) 
			A[i] = sc.nextInt() ;
		int m = sc.nextInt() ;	// 访问个数
		for( int i = 1; i <= m; i ++ ) {
			int l = sc.nextInt() ;
			int r = sc.nextInt() ;
			int K = sc.nextInt() ;	// 访问[l-1, r-1]中第K大的数
			
			System.out.println( find( A, l, r, K ) ) ;
		}
	}

}



【ALGO-2】 算法训练 最大最小公倍数  

问题描述

已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少。

输入格式

输入一个正整数N。

输出格式
输出一个整数,表示你找到的最小公倍数。
样例输入
9
样例输出
504
数据规模与约定

1 <= N <= 106


Code:

import java.math.BigInteger;
import java.util.Scanner ;

public class Main {
	
	// a 与 b 与 c 是否没有相同的公因子
	private  static boolean noCommonFactor( BigInteger a, BigInteger b, BigInteger c ) {
		if(  	a.gcd( b ).equals( new BigInteger( "1" ) ) && 
				a.gcd( c ).equals( new BigInteger( "1" ) ) && 
				b.gcd( c ).equals( new BigInteger( "1" ) ) )
			return true ;
		return false ;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner( System.in ) ;
		String str = sc.next() ;
		if( Integer.parseInt( str ) % 2 != 0 ) {	// 输入为奇数,则最大最小公倍数一定是 N * (N-1) * (N-2)
			BigInteger N = new BigInteger( str );
			System.out.println( N.multiply( N.subtract( new BigInteger("1") ) ).multiply( N.subtract( new BigInteger("2") ) ) ) ;
		}
		else {	// 输入为偶数,可能是 (N-1) * (N-2) * (N-3),也可能是 N * a * b (其中 a >= n-3)
			BigInteger result = new BigInteger( "0" );
			// 可能结果一:(n-1) * (n-2) * (n-3)
			BigInteger N = new BigInteger( str );
			BigInteger R1 = ( N.subtract( new BigInteger("1") ) ).multiply( N.subtract( new BigInteger("2") ) ).multiply( N.subtract( new BigInteger("3") ) ) ;		// (N-1) * (N-2) * (N-3)
			
			// 可能结果二:n * a * b		(a>=n-3)
			BigInteger R2 = new BigInteger( "0" ) ;
			for( char c ='1'; c <='3'; c ++ ) {	 //  n-3 =<a <= n-1
				BigInteger a = N.subtract( new BigInteger( "" + c  ) ) ;
				for( char c2 = '3'; c2 <='9'; c2 ++ ) {	// 假设 n-9 =< b <=n-1
					BigInteger b = N.subtract( new BigInteger( "" + c2) ) ;
					if( noCommonFactor( N, a, b ) ) {	// 如果N,a,b不存在相同的公因子
						BigInteger temp = N.multiply(a).multiply(b) ;
						if( temp.compareTo(R2) == 1  ) {	// temp > R2
							R2 = temp ;
							break ;
						}
						else  {		// temp <= R2
							continue ;
						}
					}	// if
				}	// for
			}	// for
			result = R1.max(R2) ;
			System.out.println( result ) ;
		}
	}
}



今天在CSDN看到一句话:“编程就是算法和数据结构,算法和数据结构是编程的灵魂。” 写的挺好,已经记在小本本上了~~

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值