杭电OJ 2000—2011、2039题

安卓开发及安全交流QQ群:838650234,感兴趣的可以加群。

反思后,需要加强地方有两点:

(1)尽可能减少代码冗余,使得代码更为简练,尽量使代码“优美”,不要在网上找API。

(2)写完之后不能以Accept接受为标准,写完之后仍需借鉴查看他人代码。

2000:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner01 = new Scanner(System.in);
        while (scanner01.hasNext()) {
            //读取字符串
            String string01 = scanner01.next();
            char[] character = new char[string01.length()];
            //将字符串切割成单个字符
            for (int i = 0; i < string01.length(); i++) {
                character[i] = string01.charAt(i);
            }
            int count = 0;
            char tmp;
            //先获取最小值,很笨的排序方法
            for (int i = 0; i < string01.length(); i++) {
// char min = character[count];//赋给他最小值
                //双重循环进行判定
                for (int j = count; j < character.length; j++) {
                    if (character[j] < character[count]) {
                        tmp = character[count];
                        character[count] = character[j];
                        character[j] = tmp;
                    }
                }
                count++;
            }
            //按照格式输出。最后一个不加空格。
            for (int i = 0; i < character.length; i++) {
                System.out.print(character[i]);
                if(i<character.length-1){
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}
 


2001:

import java.text.DecimalFormat;
import java.util.*;
//百度了double的格式化输出的两种方式
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //double比float的精度要高。
        while (scanner.hasNext()) {
            float[] num = new float[4];
            for (int i = 0; i < num.length; i++) {
                num[i] = scanner.nextFloat();
            }
            double length = Math.sqrt((num[2] - num[0]) * (num[2] - num[0]) + (num[3] - num[1]) * (num[3] - num[1]));
//            //利用DecimalFormat实现格式化输出。
//            DecimalFormat df = new DecimalFormat("#.##");
//            System.out.println(df.format(length));
            //真的就是保存两位小数
            System.out.println(String.format("%.2f",length));
        }

    }
}
 


2002:

import java.util.*;
public class Main {
    static final double PI = 3.1415927;
    public static void main(String[] args) {
        Scanner scanner01 = new Scanner(System.in);
        while (scanner01.hasNext()) {
            double radius = scanner01.nextDouble();
            System.out.println(String.format("%.3f", 4.0 / 3 * PI * Math.pow(radius, 3)));
        }
    }
}


2003:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            double num_01 = sc_01.nextDouble();
            System.out.println(String.format("%.2f", Math.abs(num_01)));
        }
    }
}
 


2004:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            int score_01 = sc_01.nextInt();
            if (score_01 >= 0 && score_01 <= 59) {
                System.out.println("E");
            } else if (score_01 > 59 && score_01 <= 69) {
                System.out.println("D");
            } else if (score_01 > 69 && score_01 <= 79) {
                System.out.println("C");
            } else if (score_01 > 79 && score_01 <= 89) {
                System.out.println("B");
            } else if (score_01 > 89 && score_01 <= 100) {
                System.out.println("A");
            } else {
                System.out.println("Score is error!");
            }
        }
    }
}
 


2005:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            //获取输入的字符串
            String date = sc_01.next();
            //获取两次出现/的位置,并截取相应字符串。
            int t1 = date.indexOf("/");
            int t2 = date.lastIndexOf("/");
            //获取月日字符串
            String year = date.substring(0, t1);
            String month = date.substring(t1 + 1, t2);
            String day = date.substring(t2 + 1, date.length());
            //将其合并为数字
            int year1 = Integer.parseInt(year);
            int month1 = Integer.parseInt(month);
            int day1 = Integer.parseInt(day);
            int monthdays = 0;
            //使用两个数组将起存起来
            int []DateFormat1=new int[]{0,31,28,31,30,31,30,31,31,30,31,30};
            int []DateFormat2=new int[]{0,31,29,31,30,31,30,31,31,30,31,30};
            //判断是闰年。节省了很多代码不是吗
            if(year1 % 4 == 0 && year1 % 100 != 0 || year1 %400 ==0){
               for(int i = 0;i<month1;i++){
                   monthdays+=DateFormat2[i];
               }
            }else{
                for(int i = 0;i<month1;i++){
                    monthdays+=DateFormat1[i];
                }
            }
            System.out.println(monthdays + day1);
        }
    }
}
 


2006:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int num_length = scanner.nextInt();
            int[] num_01 = new int[num_length];
            int result = 1;
            for (int i = 0; i < num_length; i++) {
                num_01[i] = scanner.nextInt();
                if (num_01[i] % 2 == 1) {
                    result *= num_01[i];
                }
            }
            System.out.println(result);
        }
    }
}
 


******2007:

import java.util.Scanner;
//唉。。。。都听不懂人话了,没理解题意。,参考网上代码重新写的,仅此一次
public class Main{
    public static void main(String[] args){
        Scanner sc_01 = new Scanner(System.in);

        while(sc_01.hasNext()){
            int number_01 = sc_01.nextInt();
            int number_02 = sc_01.nextInt();
            // 还要考虑大于的情况。充分考虑一切情况。
            if(number_01>number_02){
                int tmp = number_01;
                number_01 = number_02;
                number_02 = tmp;
            }
            int result1 = 0,result2 = 0;
            for(int i = number_01;i<=number_02;i++){
                if(i%2==0){
                    result1+=Math.pow(i,2);
                }else{
                    result2+=Math.pow(i,3);
                }
            }
            System.out.println(result1+" "+result2);
        }
    }
}


2008:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            int nuber_amo = sc_01.nextInt();
            int positive_number = 0, negative_number = 0, zero = 0;
            if (nuber_amo == 0) {
            } else {
                for (int i = 0; i < nuber_amo; i++) {
                    double number_01 = sc_01.nextDouble();
                    if (number_01 == 0) {
                        zero++;
                    } else if (number_01 < 0) {
                        negative_number++;
                    } else {
                        positive_number++;
                    }
                }
                System.out.println(negative_number + " " + zero + " " + positive_number);
            }
        }
    }
}
 


2009:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);

        while (sc_01.hasNext()) {
            double result = 0;
            double UpdateValue;
            //初始值
            int Initial_value = sc_01.nextInt();
            UpdateValue = (double)Initial_value;
            //计算个数
            int number = sc_01.nextInt();
            for (int i = 0; i < number; i++) {
                result+=UpdateValue;
                UpdateValue =  Math.pow(UpdateValue, 1.0 / 2);
            }
            System.out.println(String.format("%.2f", result));
        }
    }
}
 


2010:

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            int begin_num = sc_01.nextInt();
            int end_num = sc_01.nextInt();
            ArrayList<Integer> arrayList01 = new ArrayList<Integer>();
            for (int i = begin_num; i <= end_num; i++) {
                int Percentile = i / 100;
                int Tenth_place = i % 100 / 10;
                int Quota = i % 100 % 10;
                if(i == Math.pow(Percentile,3)+Math.pow(Tenth_place,3)+Math.pow(Quota,3)){
                    arrayList01.add(i);
                }
            }
            if(arrayList01.size() == 0 ){
                 System.out.println("no");
            }else{
                for(int i= 0;i<arrayList01.size()-1;i++){
                    System.out.print(arrayList01.get(i)+" ");
                }
                System.out.println(arrayList01.get(arrayList01.size()-1));
            }
        }
    }
}
 


2011:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            int number_amount = sc_01.nextInt();
            //测试个数
            int[] test_num = new int[number_amount];
            //输入每个元素
            for (int number = 0; number < number_amount; number++) {
                test_num[number] = sc_01.nextInt();
            }
            //对每个元素分别求值。
            for (int number = 0; number < number_amount; number++) {
                double result = 0,fenmu = 0;
                for (int i = 0; i < test_num[number]; i++) {
                    fenmu = Math.pow(-1, i);
                    result += fenmu / (i+1);
                }
                System.out.println(String.format("%.2f", result));
            }
        }
    }
}
 


2039:

import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc_01 = new Scanner(System.in);
        while (sc_01.hasNext()) {
            int line = sc_01.nextInt();
            //你也没说必须是double类型啊,只是说数啊。fuck
            //有大佬用Arrays.sort()直接进行排序,将最小两个相加大于第三个就可以,无意简化太多。
            for (int i = 0; i < line; i++) {
                double num_01 = sc_01.nextDouble();
                double num_02 = sc_01.nextDouble();
                double num_03 = sc_01.nextDouble();
                if (num_01 + num_02 > num_03 && num_01 - num_02 < num_03
                        && num_01 + num_03 > num_02 && num_01 - num_03 < num_02
                        && num_02 + num_03 > num_01 && num_02 - num_03 < num_01) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38244174/article/details/81674346