java acm练习题

1.第几天
 Description

 给定一个日期,输出这个日期是该年的第几天。

 Input

数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。

Output

输出一行,表示该日期是该年的第几天。

 Sample Input 1 

1985/1/20

Sample Output 1

20
import java.util.Scanner;
import static java.lang.Math.abs;
//input:1985/1/20  output:20
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String date = sc.nextLine();
        String[] array = date.split("/");
        int year = Integer.parseInt(array[0]);
        int month = Integer.parseInt(array[1]);
        int day = Integer.parseInt(array[2]);
        boolean flag = isleap(year);
        if(!flag){
            System.out.println(whatday(month,day));
        }else{
            if(month<=2){
                System.out.println(whatday(month,day));
            }else {
                System.out.println(whatday(month,day)+1);
            }
        }
    }
    public static int whatday(int month,int day) {
        if (month == 1) return day;
        if (month == 2) return day + 31;
        if (month == 3) return day + 31 + 28;
        if (month == 4) return day + 31 + 28 + 31;
        if (month == 5) return day + 31 + 28 + 31 + 30;
        if (month == 6) return day + 31 + 28 + 31 + 30 + 31;
        if (month == 7) return day + 31 + 28 + 31 + 30 + 31 + 30;
        if (month == 8) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31;
        if (month == 9) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
        if (month == 10) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
        if (month == 11) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
        if (month == 12) return day + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
        return 0;
    }
    public static boolean isleap(int year){
        boolean flag =false;
        if(year%4==0){
            flag=true;
            if(year%100==0){
                flag=false;
                if(year%400==0){
                    flag=true;
                }
            }
        }
        return flag;
    }
}

 

2.

求和

Description

计算 1 + 2 + 3 + ... + n

Input

输入将包含一系列整数n,每行一个整数。

Output

对于每种情况,在一行中输出答案, 结果将在32位整数的范围内。

Sample Input 1 

1
100

Sample Output 1

1
5050
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            long n = sc.nextLong();
            long sum = 0;
            for (long i = 1; i <= n; i++) {
                sum += i;
            }
            System.out.println(sum);
        }
    }
}

 

3.素数判断

Description

对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。

Input

输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。

Output

对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。

Sample Input 1 

0 1
0 0

Sample Output 1

OK
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int x = sc.nextInt();
            int y = sc.nextInt();
            if(x==0&&y==0) return;
            boolean flag = false;
            for(int i=x;i<=y;i++){
                int res = function(i);
                flag = isprime(res);
                if(!flag){
                    System.out.println("Sorry");
                    return;
                }
            }
                System.out.println("OK");
        }

    }
    public static boolean isprime(int num){
        if(num<2) return false;
        if(num==3||num==2) return true;
        int temp = (int) Math.sqrt(num);
        for(int i= 2;i <=temp; i++){
            if(num%i==0)
                return false ;
        }
        return true ;
    }
    public static int function(int x){
        return x*x+x+41;
    }
}

 

 

4.绝对值排序

Description

输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。

Input

输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。

Output

对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。

Sample Input 1 

3 3 -4 2
4 0 1 2 -3
0

Sample Output 1

-4 3 2
-3 2 1 0
import java.util.Scanner;
import static java.lang.Math.abs;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        while(sc.hasNext()){
            String[] strr = sc.nextLine().split(" ");
            String[] str = new String[strr.length-1];
            if (str.length >= 0) System.arraycopy(strr, 1, str, 0, str.length);
            int[] num = new int[str.length];
            for(int i=0;i<str.length;i++){
                num[i] = Integer.parseInt(str[i]);
            }
            for(int i=0;i<num.length-1;i++){
                for(int j=0;j<num.length-1-i;j++){
                    if(abs(num[j])<abs(num[j+1])){
                        int temp = num[j];
                        num[j] = num[j+1];
                        num[j+1] = temp;
                    }
                }
            }
            for(int n:num){
                System.out.print(n+" ");
            }
        }

    }
}

 

5.二进制数

Description

若将一个正整数化为二进制数,在此二进制数中,我们将数字1的个数多于数字0的个数的这类二进制数称为A类数,否则就称其为B类数。

例如:

(13)10=(1101)2

其中1的个数为3,0的个数为1,则称此数为A类数;

(10)10=(1010)2

其中1的个数为2,0的个数也为2,称此数为B类数;

(24)10=(11000)2

其中1的个数为2,0的个数为3,则称此数为B类数;

程序要求:

求出1~1000之中(包括1与1000),全部A、B两类数的个数。

Input

Output

在一行中输出两个整数A和B,A表示A类数的个数,B表示B类数的个数,AB之间由一个空格分隔,除此之外不要再输出其他多余的东西。

public class Main {
    public static void main(String[] args) {
        int countA=0,countB=0;
        for(int i=1;i<=1000;i++){
            int countone = 0,countzero = 0;
            String[] str = String.valueOf(Binary(i)).split("");
            for(String s:str){
                if("1".equals(s)){
                    countone++;
                }else{
                    countzero++;
                }
            }
            if(countone>countzero){
                countA++;
            }else{
                countB++;
            }
        }
        System.out.println(countA+" "+countB);
    }
    public static int Binary(int n){
        String result = Integer.toBinaryString(n);
        return Integer.parseInt(result);
    }
}

 

6.交换最小值和最大值

Description

输入一个正整数n,再输入n个整数,将最小值与第一个数交换,最大值与最后一个数交换,然后输出交换后的n个数。

Input

有多个测试用例。 每个案例包含一个正整数n,接着n个正整数。 n = 0时测试用例输入结束。 该测试用例不做处理。

Output

将每个测试用例输入的n个整数,将最小值与第一个数交换,最大值与最后一个数交换,然后输出交换后的n个数。

Sample Input 1 

5 4 3 5 1 2
4 1 5 6 7
5 5 4 3 2 1
0

Sample Output 1

1 3 2 4 5
1 5 6 7
1 4 3 2 5
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
       while(sc.hasNext()){
            int n = sc.nextInt();
            if(n == 0) return;
            int[] num = new int[n];
            for(int i=0;i<n;i++){
                num[i] = sc.nextInt();
            }
            //找最小值并交换
            int indexmin = 0;
            int min = num[0];
            int k;
            for(k=1;k<n;k++){
                if(num[k]<min) {
                    min=num[k];
                    indexmin = k;
                }
            }
            int temp = num[indexmin];
            num[indexmin] = num[0];
            num[0] = temp;
            //找最大值并交换
           int indexmax = 0;
           int max = num[0];
           int j;
           for(j=1;j<n;j++){
               if(num[j]>max) {
                   max=num[j];
                   indexmax = j;
               }
           }
           temp = num[indexmax];
           num[indexmax] = num[n-1];
           num[n-1] = temp;
           for(int c:num){
               System.out.print(c+" ");

           }
           System.out.println();
        }
    }
}

 

7.DNA排序

Description

逆序数可以用来描述一个序列混乱程度的量,例如,"DAABEC"的逆序数为5,其中D大于它右边的4个数,E大于它右边的1个数,4+1=5;又如,"ZWQM"的逆序数为3+2+1+0=6。

现在有许多长度一样的字符串,每个字符串里面只会出现四种字母(A,T,G,C)。请编写程序,将这些字符串按照它们的逆序数进行排序。

Input

输入由多组测试数据组成。每组测试数据的第一行为两个数n和m,0<n<=50,0<m<=100。n表示每个序列的长度(同一组测试数据中各序列的长度都为n),m表示此组测试数据中的序列个数。接下来有m行,每行为一个长度为n的DNA字母序列。

Output

对于每组测试数据,输出排序后的序列列表。在排序时,逆序数小的序列排在前面。如果两个序列的逆序数相等,那么它们在列表中出现的顺序和它们在输入中的顺序相同。

在每组测试数据后输出一行“********************”(二十个星号)。

Sample Input 1 

3 3
ACG
CGT
AGT
6 2
TAAAAA
ATAAAA

Sample Output 1

ACG
CGT
AGT
********************
ATAAAA
TAAAAA
********************
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int length = sc.nextInt();
            int count = sc.nextInt();
            sc.nextLine();
            String[] array = new String[count];
            for (int i = 0; i < count; i++) {
                String str = sc.nextLine();
                array[i] = str;
            }
            for (int i = 0; i < array.length - 1; i++) {
                for (int j = 0; j < array.length - 1 - i; j++) {
                    if (countReverse(array[j]) > countReverse(array[j + 1])) {
                        String temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
            for (String s : array) {
                System.out.println(s);
            }
            System.out.println("********************");
        }
    }
    public static int countReverse(String str){
        String[] array = str.split("");
        int length = array.length;
        int count=0;
        for(int i=0;i<length;i++){
            if("A".equals(array[i])) continue;
            if("C".equals(array[i])){
                for(int k=i+1;k<length;k++){
                    if("A".equals(array[k])) count++;
                }
            }
            if("G".equals(array[i])){
                for(int k=i+1;k<length;k++){
                    if("A".equals(array[k])||"C".equals(array[k])) count++;
                }
            }
            if("T".equals(array[i])){
                for(int k=i+1;k<length;k++){
                    if("A".equals(array[k])||"C".equals(array[k])||"G".equals(array[k])) count++;
                }
            }
        }
        return count;
    }
}

 

8.求整数的位数以及各位数之和

Description

输入一个正整数repeat(0<repeat<10),做repeat次下列运算:

输入一个整数,输出它的位数以及各位数之和。

Input

正整数repeat及repeat个整数

Output

整数的位数以及各位数之和

Sample Input 1 

4
123456 
-100 
-1 
99

Sample Output 1

number=6,sum=21
number=3,sum=1
number=1,sum=1
number=2,sum=18
import java.util.Scanner;
import static java.lang.Math.abs;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i=0;i<n;i++){
            int num = abs(sc.nextInt());
            String[] array = String.valueOf(num).split("");
            int count = array.length;
            int sum = 0;
            for(String s:array){
                sum+=Integer.parseInt(s);
            }
            System.out.println("number="+count+",sum="+sum);
        }
    }
}

 

9.输出Fibonacci序列

Description

输入一个正整数repeat(0<repeat<10),做repeat次下列运算:

输入2个正整数m和n(1<=m,n<=300000),输出m和n之间所有的Fibonacci数。

Fibonacci序列除第一个和第二个数外,任意一个数都可由前两个数相加得到,第一个数和第二个数的值均为1。

Fibonacci序列(第1项起):1 1 2 3 5 8 13 21 ......

Input

输入一个正整数repeat(0<repeat<10),代表做repeat次运算

输入repeat个正整数m和n

Output

输出

repeat次

m和n之间所有的Fibonacci数

每两个Fibonacci数之间用一个空格隔开,m和n之间的最后一个Fibonacci数后面也有一个空格

Sample Input 1 

3
1 10
20 100
1000 6000

Sample Output 1

1 1 2 3 5 8
21 34 55 89
1597 2584 4181
import java.util.ArrayList;
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();
        for(int i=0;i<n;i++){
            int start = sc.nextInt();
            int end = sc.nextInt();
            int[] res = Fibonacci(start,end);
            for(int num:res){
                System.out.print(num+" ");
            }
            System.out.println();
        }
    }
    public static int[] Fibonacci(int start,int end){
        int[] array = new int[30];
        array[0] = 1;
        array[1] = 1;
        for(int i=2;i<30;i++) {
            array[i]=array[i-1]+array[i-2];
            if(array[i] > end) break;
        }
        List<Integer>list = new ArrayList<>();
        for(int n:array) {
            if(n>=start && n<=end) {
                list.add(n);
            }
        }
        int[] fibonacci = new int[list.size()];
        for(int i = 0;i<list.size();i++){
            fibonacci[i] = list.get(i);
        }
        return fibonacci;
        }
}

 

10.请完成汇率和金额排序程序

Description

在国际机场,我们会发现有多个货币兑换的窗口,这是由于各国货币的单位价值是不一样的。

下面列出了某日国际货币的汇率表(相对于100人民币的各国货币值)。

货币  | CNY  |  HKD |  USD  |  EUR

汇率  | 100    | 118    | 15      | 13

例如,100元人民币可以兑换118香港币。请利用继承机制与Comparable接口实现不同数目货币对象价值的排序。

Input

输入3个数字

Output

金额价值从小到大

Sample Input 1 

100
100
100

Sample Output 1

HKD100
USD100
EUR100
import java.util.Arrays;
import java.util.Scanner;

class Currency {
    private String name;        //货币名称
    private int originalValue;  //原始值
    private int value;          //转换为人民币后的值
    public static String[] CURRENCY_NAME = { "CNY", "HKD", "USD", "EUR" };
    public static int[] CURRENCY_RATIO = { 100, 118, 15, 13 };
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getOriginalValue() {
        return originalValue;
    }
    public void setOriginalValue(int originalValue) {
        this.originalValue = originalValue;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
}
class HKD extends Currency implements Comparable {
// 实现你的构造函数与Comparable中的接口
    public HKD(int hkd){
        this.setName("HKD");
        this.setValue(hkd);
        this.setOriginalValue((int)(100.0/118.0*hkd));
    }

    @Override
    public int compareTo(Object o) {
        Currency oo = (Currency)o;
        if(this.getOriginalValue()<oo.getOriginalValue()){
            return -1;
        }else{
            return 0;
        }
    }
}

class USD extends Currency implements Comparable {
    public USD(int usd){
        this.setName("USD");
        this.setValue(usd);
        this.setOriginalValue((int)(100.0/15.0*usd));
    }

    @Override
    public int compareTo(Object o) {
        Currency oo = (Currency)o;
        if(this.getOriginalValue()<oo.getOriginalValue()){
            return -1;
        }else{
            return 0;
        }
    }
}

class EUR extends Currency implements Comparable {
    public EUR(int eur){
        this.setName("EUR");
        this.setValue(eur);
        this.setOriginalValue((int)(100.0/13.0*eur));
    }

    @Override
    public int compareTo(Object o) {
        Currency oo = (Currency)o;
        if(this.getOriginalValue()<oo.getOriginalValue()){
            return -1;
        }else{
            return 0;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Currency[] cs = new Currency[3];
//初始化
        Scanner sc = new Scanner(System.in);
//利用hasNextXXX()判断是否还有下一输入项
        int a = 0;
        int b = 0;
        int c = 0;
        if (sc.hasNext()) {
            a = sc.nextInt();
            cs[0] = new HKD(a);
        }
        if (sc.hasNext()) {
            b = sc.nextInt();
            cs[1] = new USD(b);
        }
        if (sc.hasNext()) {
            c = sc.nextInt();
            cs[2] = new EUR(c);
        }
        Arrays.sort(cs);
        for(int i=0;i<3;i++){
            System.out.println(cs[i].getName()+""+cs[i].getValue());
        }

    }
}

 

posted @ 2021-07-14 20:00  菅兮徽音  阅读(558)  评论(0编辑  收藏  举报