现在的位置: 首页 > 综合 > 正文

ACM编程赛题目共享(上)

2018年03月23日 ⁄ 综合 ⁄ 共 3354字 ⁄ 字号 评论关闭

首先,我想申明的是,这里的ACM竞赛是我们学院自己组织的,题目不是很难,不过只有几题,还有两题改天补上!此外,我还附上了自己写的答案。有兴趣的朋友不妨也写下,或者帮我更正一下!题目见下:

 

Problem A 兔子(Rabbits)

(Time Limit :1 Seconds ,Memory limit :32768K)

兔子有很强的繁殖能力,在出生后两个月后,就有繁殖能力。一对兔子每个月能生出一对小兔子来。

第一个月,小兔子没有繁殖能力,所以还是一对;

第二个月,生下一对小兔,所以一共是两对;

第三个月,老兔子又生下一对,因为小兔子还没有繁殖能力,所以一共是三对;

……

如果所有的兔子都不死,那么一年后可以繁殖多少对兔子?

 

Input:本体目无输入

Output:按顺序输出每月的兔子数

例子:

Sample Output:

1     2     3     5     8     13    ……

 

 

我的源程序:

public class Rabbits {

    public static void main(String[] args) {

       int count = 0;

       int f0 = 1, f1 = 2;

       while (count < 6) {

           System.out.print(f0 + "/t" + f1 + "/t");

           f0 = f0 + f1;

           f1 = f0 + f1;

           count++;

       }

    }

}

 

 

 Problem C 哥德巴赫猜

(Time Limit :1 Seconds ,Memory limit :32768K)

哥德巴赫猜想大致可以分为两个猜想:

a   每个不小于6的偶数都可以表示为两个奇素数之和;

b   每个不小于9的奇数都可以表示为三个奇素数之和;

这个猜想至今还没有人完整的证明出来,现在请你用计算机程序来验证猜想a在100以内的正确性

Input:本体目无输入

Output:输出100以内所有大于6的偶数的素数拆分

例子:

Sample Output:

6=3+3

8=3+5;

10=3+7;

12=5+7;

……

100=?+?

 

我的源程序:

public class GoldbachGuessing {

    public static boolean isPrime(int value) {

       boolean judge = true;

       int limit = (int) Math.sqrt(value) + 1;

       for (int i = 2; i < limit; i++)

           if (value % i == 0) {

              judge = false;

              break;

           }

       return judge;

    }

 

    public static void main(String[] args) {

       System.out.println("/t/t验证猜想a在100以内的正确性");

       for (int i = 6; i < 100; i += 2)

           for (int j = 2; j <= i - j; j++)

              if (isPrime(j) && isPrime(i - j)) {

                  System.out.println(i + "=" + j + "+" + (i - j));

                  break;

              }

 

       System.out.println("/n/n/t/t验证猜想b在100以内的正确性");

       outer: for (int i = 9; i < 100; i += 2)

           for (int j = 3; j <= i - j; j++)

              for (int k = 3; j <= i - j - k; k++)

                  if (isPrime(j) && isPrime(k) && isPrime(i - j - k)) {

                     System.out.println(i + "=" + j + "+" + k + "+"

                            + (i - j - k));

                     continue outer;

                  }

    }

}

 

 

Problem D EA模拟大作孢子补丁

(Time Limit :1 Seconds ,Memory limit :32768K ,input file : ea.in)

Ea公司年度大作<孢子>要出补丁了!补丁要求用户创建的生物需要能够拥有自己的计数方式来加减自己种族的资金,生物的计数方式是按照生物的总手指数来确定的,也就是说,只有4个手指的种族以4为基数,有10个手指的以10为基数。现在作为EA旗下的一名员工,the boss要求你完成总共有6个手指的生物计数方式加减运算。

 

Input:每行有两个数(记住是6个手指的生物),中间用空格隔开,所有的数都是正数,并且第一个数大于第二个数,计算number1+ number2以及number1- number2

Output:输出两个数的和以及差,用空格隔开,不同的组用回车隔开,

例子:

Sample Input:

5     1

Output:

10    1

 

 

我的源程序:

import java.io.*;

 

public class SixFingers {

    public static void Counting(int value) {

       int count = 1;

       int m = 6;

       while (value >= m) {

           ++count;

           m *= 6;

       }

       int array[] = new int[count];

       int i = 0;

       while (value != 0) {

           array[i] = value % 6;

           value /= 6;

           i++;

       }

       for (int k = array.length - 1; k >= 0; k--)

           System.out.print(array[k]);

       System.out.print("/t");

    }

 

    public static void main(String[] args) throws IOException {

       File ea = new File("input.txt");

       if (ea.exists() == false)

           try {

              ea.createNewFile();

           } catch (Exception e) {

              System.out.println(e.getMessage());

           }

       String str[] = new String[3];

       int i = 0;

       try {

           BufferedReader br = new BufferedReader(new FileReader(ea));

           while ((str[i] = new String(br.readLine())) != null) {

              System.out.print(str[i] + "/t");

              i++;

           }

           br.close();

       } catch (NullPointerException e) {

           // Null

       }

       int number1 = Integer.parseInt(str[0]);

       int number2 = Integer.parseInt(str[1]);

       System.out.println();

       Counting(number1 + number2);

       Counting(number1 - number2);

 

    }

}

 

 

抱歉!评论已关闭.