软件2018-2练习1 #Java#

7-1 一个整数各个位上的最大数字(10 分)/

考点

基本输入输出,流程控制(循环)

代码

import java.io.*;
import java.util.*;
public class Main {
    static final Scanner cin = new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) {
        String a = cin.next();
        int ans = 0;
        for (int i = 0; i < a.length(); ++i) {
            int tmp = a.charAt(i) - '0';
            ans = Math.max(tmp, ans);
        }
        System.out.println(ans);
    }
}

7-2 十进制转二进制 (10 分)。

考点

基本输入输出,流程控制(循环),二进制的计算方法

代码

import java.io.*;
import java.util.*;
public class Main {
    static final Scanner cin = new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) {
        int a = cin.nextInt();
        Vector<Integer> vec = new Vector<>();
        while (a != 0) {
            vec.add(a & 1);
            a >>= 1;
        }
        for (int i = vec.size() - 1; i >= 0; --i)
            System.out.print(vec.get(i));
    }
}

7-3 判断回文 (10 分)

考点

基本输入输出,流程控制(循环,分支),中途相遇法

代码

import java.io.*;
import java.util.*;
public class Main {
    static final Scanner cin = new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) {
        String str = cin.next();
        int i = 0, j = str.length() - 1;
        boolean ans = true;
        while (i <= j) {
            if (str.charAt(i) != str.charAt(j)) {
                ans = false;
                break;
            }
            ++i; --j;
        }
        System.out.print(ans ? "Yes" : "No");
    }
}

7-4 购买第3便宜的商品 (10 分)

考点

基本输入输出,流程控制(循环,分支),TreeSet的基本使用方法

代码

import java.io.*;
import java.util.*;
public class Main {
    static final Scanner cin = new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) {
        TreeSet<Integer> se = new TreeSet<>();
        int n = cin.nextInt();
        for (int i = 0; i < n; i++)
            se.add(cin.nextInt());
        Iterator it = se.iterator();
        int cnt = 0;
        while (it.hasNext()) {
            if (++cnt == 3) {
                System.out.println(it.next());
                break;
            } else it.next();
        }
        if (cnt != 3) System.out.println(-1);
    }
}

7-5 n个a数字求和 (15 分)

考点

基本输入输出,流程控制(循环)

代码

import java.io.*;
import java.util.*;
public class Main {
    static final Scanner cin = new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) {
        int a = cin.nextInt();
        int n = cin.nextInt();
        int ans = 0, tmp = a;
        for(int i = 0; i < n; ++i){
            ans += tmp;
            tmp *= 10;
            tmp += a;
        }
        System.out.print(ans);
    }
}

7-6 完数 (15 分)

考点

基本输入输出,流程控制(循环),静态函数的编写,解耦合思想,Vector的基本使用方法

代码

import java.io.*;
import java.util.*;

public class Main {
    static final Scanner cin = new Scanner(new BufferedInputStream(System.in));

    public static void main(String[] args) {
        int n = cin.nextInt();
        Vector<Integer> vec = new Vector<>();
        for(int i = 2; i <= n;++i)
            if(isPrefectNum(i))
                vec.add(i);
        Iterator<Integer> it = vec.iterator();
        boolean flag = true;
        while (it.hasNext())
        {
            if(flag) flag = false;
            else System.out.print(",");
            System.out.print(it.next());
        }
    }
    static public boolean isPrefectNum(int num) {
        int sum = 0;
        for (int i = 1; i < num; ++i)
            if (num % i == 0)
                sum += i;
        return sum == num;
    }
}

7-7 N个数的排序与查 (15 分)

考点

基本输入输出,流程控制(循环),数组/Vector的基本使用方法,排序库函数

代码

import java.io.*;
import java.util.*;
public class Main {
    static final Scanner cin = new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) {
        int n = cin.nextInt();
        Vector<Integer> vec = new Vector<>();
        for (int i = 0; i < n; ++i)
            vec.add(cin.nextInt());
        Collections.sort(vec);
        boolean NotFind = true;
        int num = cin.nextInt();
        for (int i = 0; i < n; ++i)
            if (vec.get(i) == num) {
                System.out.print(i + 1);
                NotFind = false;
            }
        if (NotFind) System.out.print(-1);
    }
}

7-8 集合求交 (15 分)

考点

基本输入输出,流程控制(循环),TreeSet的基本使用方法

代码

import java.io.*;
import java.util.*;
public class Main {
    static final Scanner cin = new Scanner(new BufferedInputStream(System.in));
    public static void main(String[] args) {
        Set<Integer> set1 = new TreeSet<>();
        Set<Integer> set2 = new TreeSet<>();
        for (int i = 0; i < 3; ++i)
            set1.add(cin.nextInt());
        for (int i = 0; i < 3; ++i)
            set2.add(cin.nextInt());
        set1.retainAll(set2);
        System.out.print(set1.size());
    }
}
posted @ 2019-09-08 16:12  YuKi_hahaha  阅读(546)  评论(0编辑  收藏  举报