第四届决赛试题

1、好好学习

汤姆跟爷爷来中国旅游。一天,他帮助中国的小朋友贴标语。他负责贴的标语是分别写在四块红纸上的四个大字:“好、好、学、习”。但是汤姆不认识汉字,他就想胡乱地贴成一行。

请你替小汤姆算一下,他这样乱贴,恰好贴对的概率是多少?
答案是一个分数,请表示为两个整数比值的形式。例如:1/3 或 2/15 等。
如果能够约分,请输出约分后的结果。

注意:不要书写多余的空格。
请严格按照格式,通过浏览器提交答案。
注意:只提交这个比值,不要写其它附加内容,比如:说明性的文字。

代码:

答案:1/6

/**
 * 好好学习
 * @description
 * @author zhangbiao
 * @time 2018-5-11 下午5:20:15
 */
public class Main01 {
    //答案;1/6
    public static void main(String[] args) {
        int[] a={1,1,3,4};
        fullArray(a, 0, 3);
        System.out.println("num="+num);
    }
    //数组交换
    public static void swap(int[] a,int x,int y){
        int temp=a[x];
        a[x]=a[y];
        a[y]=temp;
    }
    static int num=0;
    //全排列
    public static void fullArray(int[] a,int guang,int end){
        if(guang==end){
            num++;
            System.out.println(Arrays.toString(a));
        }else{
            for(int i=guang;i<=end;i++){
                swap(a, guang, i);
                fullArray(a, guang+1, end);
                swap(a, guang, i);
            }
        }
    }
}

2、猜灯谜

村的元宵节灯会上有一迷题:
请猜谜 * 请猜谜 = 请边赏灯边猜
小明想,一定是每个汉字代表一个数字,不同的汉字代表不同的数字。
请你用计算机按小明的思路算一下,然后提交“请猜谜”三个字所代表的整数即可。

代码:

答案:897

/**
 * 猜灯谜
 * @description
 * @author zhangbiao
 * @time 2018-5-11 下午5:44:44
 */
public class Main02 {
    //答案:897
    public static void main(String[] args) {
        for(int a=1;a<=9;a++){//请
            for(int b=0;b<=9;b++){//猜
                for(int c=0;c<=9;c++){//谜
                    if(f(a, b, c)){
                        return ;
                    }
                }
            }
        }
    }
    //判断是否符合条件
    public static boolean f(int a,int b,int c){
        int num=a*100+b*10+c;
        int sum=num*num;
        String str=sum+"";
        char[] ch = str.toCharArray();
        if(ch.length==6){
            if(ch[1]==ch[4]&&ch[0]-'0'==a&&ch[5]-'0'==b){
                System.out.println(num);
                return true;
            }
        }
        return false;
    }
}

3、填算式

(ABCD - EFGH) * XY = 900
每个字母代表一个0~9的数字,不同字母代表不同数字,首位不能为0。
比如,(5012 - 4987) * 36 就是一个解。

请找到另一个解,并提交该解中 ABCD 所代表的整数。
请严格按照格式,通过浏览器提交答案。
注意:只提交 ABCD 所代表的整数,不要写其它附加内容,比如:说明性的文字。

代码:

答案:6048

/**
 * 填算式
 * @description
 * @author zhangbiao
 * @time 2018-5-12 下午2:43:48
 */
public class Main03 {
    //答案:6048
    public static void main(String[] args) {
        int[] a={0,1,2,3,4,5,6,7,8,9};
        fullArray(a, 0, 9);
    }
    //交换数组
    public static void swap(int[] a,int x,int y){
        int temp=a[x];
        a[x]=a[y];
        a[y]=temp;
    }
    //全排列
    public static void fullArray(int[] a,int guang,int end){
        if(guang==end){
            if(a[0]!=0&&a[4]!=0&&a[8]!=0){
                int b1=a[0]*1000+a[1]*100+a[2]*10+a[3];
                int b2=a[4]*1000+a[5]*100+a[6]*10+a[7];
                int b3=a[8]*10+a[9];
                if((b1-b2)*b3==900){
                    System.out.println(b1);
                }
            }
        }else{
            for(int i=guang;i<=end;i++){
                swap(a, guang, i);
                fullArray(a, guang+1, end);
                swap(a, guang, i);
            }
        }
    }
}

4、埃及分数

古埃及曾经创造出灿烂的人类文明,他们的分数表示却很令人不解。古埃及喜欢把一个分数分解为类似: 1/a + 1/b 的格式。

这里,a 和 b 必须是不同的两个整数,分子必须为 1
比如,2/15 一共有 4 种不同的分解法(姑且称为埃及分解法):

1/8 + 1/120
1/9 + 1/45
1/10 + 1/30
1/12 + 1/20

那么, 2/45 一共有多少个不同的埃及分解呢(满足加法交换律的算同种分解)? 请直接提交该整数(千万不要提交详细的分解式!)。

代码:

答案:7

/**
 * 埃及分数
 * @description
 * @author zhangbiao
 * @time 2018-5-12 下午3:09:27
 */
public class Main04 {
    //答案:7
    public static void main(String[] args) {
        int num=0;
        for(int a=1;a<100000;a++){
            for(int b=1;b<100000;b++){
                if(45*(b+a)==2*a*b&&a<b){
                    num++;
                    System.out.println(a+"--"+b);
                }
            }
        }
        System.out.println("num="+num);
    }
}

5、连续奇数和

小明看到一本书上写着:任何数字的立方都可以表示为连续奇数的和。
比如:

2^3 = 8 = 3 + 5
3^3 = 27 = 7 + 9 + 11
4^3 = 64 = 1 + 3 + … + 15

虽然他没有想出怎么证明,但他想通过计算机进行验证。 请你帮助小明写出 111 的立方之连续奇数和表示法的起始数字。如果有多个表示方案,选择起始数字小的方案。

代码:

答案:371

/**
 * 连续奇数和
 * @description
 * @author zhangbiao
 * @time 2018-5-12 下午3:22:26
 */
public class Main05 {
    //思路:利用等差数列求和,设第一项为x,共有y项
    //答案:371
    public static void main(String[] args) {
        int a=111;
        int x,y;
        for(x=1;x<100000;x=x+2){
            for(y=1;y<100000;y++){
                if((x+y-1)*y==111*111*111){
                    System.out.println(x+"--"+y);
                }
            }
        }
    }
}

6、金蝉素数

考古发现某古墓石碑上刻着一个数字:13597,后研究发现:
这是一个素数!
并且,去掉首尾数字仍是素数!
并且,最中间的数字也是素数!
这样特征的数字还有哪些呢?通过以下程序的帮助可以轻松解决。请仔细阅读代码,并填写划线部分缺失的代码。

代码:

答案:k==x.length-1

/**
 * 金蝉素数(简单)
 * @description
 * @author zhangbiao
 * @time 2018-5-12 下午4:24:49
 */
public class Main07 {
    static boolean isPrime(int n)
    {
        if(n<=1) return false;
        for(int i=2; i*i<=n; i++){
            if(n%i==0) return false;
        }
        return true;
    }

    static void f(int[] x, int k)
    {
        if(k==x.length-1){  // 填空位置
            if(isPrime(x[0]*10000 + x[1]*1000 + x[2]*100 + x[3]*10 + x[4]) &&
                isPrime(x[1]*100 + x[2]*10 + x[3]) &&
                isPrime(x[2]))
                System.out.println(""+x[0]+x[1]+x[2]+x[3]+x[4]);
            return;
        }

        for(int i=k; i<x.length; i++){
            {int tmp=x[k]; x[k]=x[i]; x[i]=tmp; }
            f(x,k+1);
            {int tmp=x[k]; x[k]=x[i]; x[i]=tmp; }
        }
    }

    static void test()
    {
        int[] x = {1,3,5,7,9};
        f(x,0);
    }

    public static void main(String[] args)
    {
        test();
    }
}

7、快速排序

快速排序算法是典型的分治思想的运用。它使用某个key把全部元素分成两组,其中一组的元素不大于另一组。然后对这两组再次进行递归排序。

以下代码实现了快速排序。请仔细阅读代码,填写缺少代码的部分。

代码:

答案:li<=ri

/**
 * 快速排序
 * @description
 * @author zhangbiao
 * @time 2018-5-12 下午4:33:20
 */
public class Main08 {
    public static void main(String[] args) {
        int[] x={9,8,5,4,2};
        f(x, 0, 4);
        System.out.println(Arrays.toString(x));
    }

    static void f(int[] x, int left, int right)
    {
        if(left >= right) return;

        int key = x[(left+right)/2];

        int li = left;
        int ri = right;
        while(li<=ri){
            while(x[ri]>key) ri--;
            while(x[li]<key) li++;

            if(li<=ri){    //填空位置
                int t = x[li];
                x[li] = x[ri];
                x[ri] = t;
                li++;
                ri--;
            }   
        }

        if(li < right) f(x, li, right);
        if(ri > left) f(x, left, ri);
    }
}

猜你喜欢

转载自blog.csdn.net/shaonianbz/article/details/80383990
今日推荐