ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java acm练习题

2021-07-14 20:01:32  阅读:165  来源: 互联网

标签:练习题 java Scanner int acm sc import String


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(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+" ");
            }
        }

    }
}

 

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

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);
        }
    }
}

 

5.

输出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;
        }
}

 

标签:练习题,java,Scanner,int,acm,sc,import,String
来源: https://www.cnblogs.com/augenstern/p/15012706.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有