京东2017编程题(1)

作者: Taoyongpan | 来源:发表于2018-04-08 11:01 被阅读19次

    第一题

    [编程题] 进制均值
    时间限制:1秒

    空间限制:32768K

    尽管是一个CS专业的学生,小B的数学基础很好并对数值计算有着特别的兴趣,喜欢用计算机程序来解决数学问题,现在,她正在玩一个数值变换的游戏。她发现计算机中经常用不同的进制表示一个数,如十进制数123表达为16进制时只包含两位数7、11(B),用八进制表示为三位数1、7、3,按不同进制表达时,各个位数的和也不同,如上述例子中十六进制和八进制中各位数的和分别是18和11,。 小B感兴趣的是,一个数A如果按2到A-1进制表达时,各个位数之和的均值是多少?她希望你能帮她解决这个问题? 所有的计算均基于十进制进行,结果也用十进制表示为不可约简的分数形式。
    输入描述:
    输入中有多组测试数据,每组测试数据为一个整数A(1 ≤ A ≤ 5000).

    输出描述:
    对每组测试数据,在单独的行中以X/Y的形式输出结果。

    输入例子1:
    5
    3

    输出例子1:
    7/3
    2/1

    import java.util.Scanner;
    
    /**
     * @Author: Taoyongpan
     * @Date: Created in 9:10 2018/4/8
     * 进制均值
     */
    public class Test02 {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()){
                int n = sc.nextInt();
                int sum = 0;
                for (int i = 2 ; i <= n-1;i++){
                    sum+=sum(n,i);
                }
                int num = n-2;
                int k = t(sum,num);
                System.out.println(sum/k+"/"+num/k);
            }
        }
    
        public static int sum(int n,int i){
            int sum = 0;
            while (n!=0){
                sum+=n%i;
                n/=i;
            }
            return sum;
        }
        public static int t(int a,int b){
            int t = 0;
            while (b!=0){
                t = a%b;
                a = b;
                b = t;
            }
            return a;
        }
    }
    
    

    第二题

    [编程题] 幸运数
    时间限制:1秒

    空间限制:32768K

    小明同学学习了不同的进制之后,拿起了一些数字做起了游戏。小明同学知道,在日常生活中我们最常用的是十进制数,而在计算机中,二进制数也很常用。现在对于一个数字x,小明同学定义出了两个函数f(x)和g(x)。 f(x)表示把x这个数用十进制写出后各个数位上的数字之和。如f(123)=1+2+3=6。 g(x)表示把x这个数用二进制写出后各个数位上的数字之和。如123的二进制表示为1111011,那么,g(123)=1+1+1+1+0+1+1=6。 小明同学发现对于一些正整数x满足f(x)=g(x),他把这种数称为幸运数,现在他想知道,小于等于n的幸运数有多少个?
    输入描述:
    每组数据输入一个数n(n<=100000)

    输出描述:
    每组数据输出一行,小于等于n的幸运数个数。

    输入例子1:
    21

    输出例子1:
    3

    import java.util.Scanner;
    
    /**
     * @Author: Taoyongpan
     * @Date: Created in 9:10 2018/4/8
     */
    public class Test03 {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()){
                int n = sc.nextInt();
                int count = 0;
                for (int i = 1 ; i<=n;i++){
                    if (tenSum(i)==twoSum(i)){
                        count++;
                    }
                }
                System.out.println(count);
            }
        }
        public static int tenSum(int n){
            int sum = 0;
            while (n!=0){
                sum+=n%10;
                n/=10;
            }
            return sum;
        }
        public static int twoSum(int n){
            int sum = 0;
            while (n!=0){
                sum+=n%2;
                n/=2;
            }
            return sum;
        }
    }
    
    

    第三题

    [编程题] 集合
    时间限制:1秒

    空间限制:32768K

    给你两个集合,要求{A} + {B}。 注:同一个集合中不会有两个相同的元素。
    输入描述:
    每组输入数据分为三行,第一行有两个数字n,m(0 ≤ n,m ≤ 10000),分别表示集合A和集合B的元素个数。后两行分别表示集合A和集合B。每个元素为不超过int范围的整数,每个元素之间有个空格隔开。

    输出描述:
    针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开,行末无空格。

    输入例子1:
    3 3
    1 3 5
    2 4 6

    输出例子1:
    1 2 3 4 5 6

    import java.util.Iterator;
    import java.util.Scanner;
    import java.util.TreeSet;
    
    /**
     * @Author: Taoyongpan
     * @Date: Created in 9:11 2018/4/8
     */
    public class Test04 {
        public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()){
                int n = sc.nextInt();
                int m = sc.nextInt();
                TreeSet<Integer> treeSet = new TreeSet<>();
                for (int i = 0 ; i < n+m;i++){
                    treeSet.add(sc.nextInt());
                }
                Iterator<Integer> it = treeSet.iterator();
                int index = 1;
                while (it.hasNext()){
                    if (index==1){
                        System.out.print(it.next());
                        index = -1;
                    }else {
                        System.out.print(" " +it.next());
                    }
                }
                System.out.println();
            }
        }
    }
    
    

    相关文章

      网友评论

        本文标题:京东2017编程题(1)

        本文链接:https://www.haomeiwen.com/subject/lnjphftx.html