美文网首页
安徽农业大学第一届编程与思维比赛试题

安徽农业大学第一届编程与思维比赛试题

作者: 桐桑入梦 | 来源:发表于2020-03-08 05:58 被阅读0次



    第一题思路:
    排序,找到第一个a[i]>=i的位置i
    
    public class Solutions {
        public static int solve(String[] args) {
            Scanner scanner = new Scanner( System.in );
            int n = scanner.nextInt();
            Integer[] a = new Integer[n];
    
            for( int i = 0; i < n; i++ )
                a[i] = scanner.nextInt();
    
            //从小到大排序
            Arrays.sort(a, new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1 < o2 ? 1 : -1;
                }
            });
    
            int result = -1;
            for( int i = 0; i < n; i++ ){
                if( a[i] <= i ){
                    result = i;
                    break;
                }
            }
            return result == -1 ? n : result;
        }
    }
    
    
    第二题思路:
    直接计算
    
    public class Solutions {
        public static void main(String[] args) {
            Scanner scanner = new Scanner( System.in );
            int m = scanner.nextInt();
            int n = scanner.nextInt();
            double[][] a = new double[m][n], b = new double[m][n];
    
            for( int i = 0; i < m; i++ ){
                for( int j = 0; j < n; j++ ){
                    a[i][j] = scanner.nextDouble();
                }
            }
            for( int i = 0; i < m; i++ ){
                for( int j = 0; j < n; j++ ){
                    b[i][j] = scanner.nextDouble();
                }
            }
    
            double mse = 0;
            Double[] ab = new Double[ m * n ];
            for( int i = 0; i < m; i++ ){
                for( int j = 0; j < n; j++ ){
                    double tmp = ( a[i][j] - b[i][j] ) * ( a[i][j] - b[i][j] );
                    mse += tmp;
                    ab[ i * m + j ] = tmp;
                }
            }
    
            //从大到小排序
            Arrays.sort( ab, new Comparator<Double>() {
                @Override
                public int compare(Double o1, Double o2) {
                    return o1 < o2 ? 1 : -1;
                }
            });
    
            for( int i = 0; i < m * n / 10; i++ )
                mse -= ab[i];
    
            mse /= m * n - m * n / 10;
            System.out.printf( "%.5f", mse );
        }
    }
    
    第三题思路:
    感染者和未感染者相遇,两个人掉头的效果和不掉头直行的效果是相同的,发挥想象力这个地方。
    
    public class Solutions {
        public static void main(String[] args) {
            Scanner scanner = new Scanner( System.in );
            int m = scanner.nextInt();
            int[] a = new int[m];
            for( int i = 0; i < m; i++ )
                a[i] = scanner.nextInt();
    
            int res = 0;
            if( a[0] > 0 ){
                //正面而来的人全部感染
                for( int i = 1; i < m; i++ ){
                    if( a[i] < 0 && ( - a[i] ) > a[0] )
                        res++;
                }
                //确实有正面而来的人
                if( res != 0 ){
                    for( int i = 1; i < m; i++ ){
                        if( a[i] > 0 && a[i] > a[0] )
                            res++;
                    }
                }
            }
            else if( a[0] < 0 ){
                for( int i = 0; i < m; i++ ){
                    if( a[i] > 0 && a[i] < ( - a[0] ) )
                        res++;
                }
    
                if( res != 0 ){
                    for( int i = 0; i < m; i++ ){
                        if( a[i] < 0 && ( - a[i] ) > ( - a[0] ) )
                            res++;
                    }
                }
            }
            res += 1; //加上自己
            System.out.println( res );
        }
    }
    
    
    最后一个:
    使用枚举,注意及时终止错误的枚举
    
    import java.util.Scanner;
    public class Solutions {
        public static void main(String[] args) {
            int res = 0;
            for( int i = 1; i <= 198 / 4; i++ ){
                for( int j = i; j + i <= 198 / 2; j++){
                    for( int k = j; i + j + k <= 198 * 3 / 4; k++){
                        int m = 198 - i - j - k;
                        if( m >= k){
                            res++;
                            System.out.println( i + "," + j + "," + k + "," + m );
                        }
                    }
                }
            }
            System.out.println( res );
        }
    }
    
    

    相关文章

      网友评论

          本文标题:安徽农业大学第一届编程与思维比赛试题

          本文链接:https://www.haomeiwen.com/subject/pcvrdhtx.html