首页 > 试题广场 >

数字游戏

[编程题]数字游戏
  • 热度指数:438 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
牛牛举办了一场数字游戏,有n个玩家参加这个游戏,游戏开始每个玩家选定一个数,然后将这个数写在纸上(十进制数,无前缀零),然后接下来对于每一个数字将其数位按照非递减顺序排列,得到新的数,新数的前缀零将被忽略。得到最大数字的玩家赢得这个游戏。

输入描述:
输入包括两行,第一行包括一个整数n(1 ≤ n ≤ 50),即玩家的人数
第二行n个整数x[i](0 ≤ x[i] ≤ 100000),即每个玩家写下的整数。


输出描述:
输出一个整数,表示赢得游戏的那个玩家获得的最大数字是多少。
示例1

输入

3
9638 8210 331

输出

3689
// 我是写个方法用List做的反转,然后用StringBuffer直接在后面加,再返回为int的反转值【Integer.parseInt()会自动去掉前缀0】 
// 接着 主方法写个取最大的反转值就行
import java.util.*; public class Main {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int num = sc.nextInt();         int max = 0;         while (num-- > 0) {             int cur = getReverse(sc.nextInt());             max = cur > max ? cur : max;         }         System.out.println(max);     }     static int getReverse(int x) {         List<Integer> temp = new ArrayList<Integer>();         while (x != 0) {             temp.add(x%10);             x /= 10;         }         Collections.sort(temp);         StringBuffer sb = new StringBuffer();         for (int i = 0; i < temp.size(); i++) {             sb.append(temp.get(i));         }         return Integer.parseInt(sb.toString());     } }

编辑于 2018-04-07 17:31:54 回复(0)
importjava.util.*;
publicclassMain {
        publicstaticvoidmain(String[] args) {
            Scanner sc = newScanner(System.in);
            intnum = Integer.parseInt(sc.nextLine());
            String input = sc.nextLine();
            int[] list = newint[num];
            for(inti = 0; i < num; i++) {
                char[] a = input.split(" ")[i].toCharArray();
                Arrays.sort(a);
                list[i] = Integer.parseInt(newString(a));
            }
            intmax = list[0];
            for(inti = 1; i < list.length; i++) {
                if(list[i] > max){
                    max = list[i];
                }
            }
            System.out.println(max);
        }
         
     
     
}

发表于 2018-01-22 15:48:30 回复(1)
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
         
        String[] nums = null;
        nums = sc.nextLine().split("\\s");
 
        int max = 0;
        for (int i = 0; i < n; i++) {
            char[] ch = nums[i].toCharArray();
            Arrays.sort(ch);
            int temp = Integer.parseInt(new String(ch));
            if (temp > max) {
                max = temp;
            }
        }
 
        System.out.println(max);
 
    }
 
}


发表于 2019-09-07 19:53:59 回复(0)
没看懂这题到底什么个意思
发表于 2019-01-24 10:09:36 回复(0)

import java.util.Arrays;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int num=sc.nextInt();
    int max=Integer.MIN_VALUE;
    char[] c;
    int max_user=Integer.MIN_VALUE;
    for(int i=0;i<num;i++){
        int str=sc.nextInt();
        String str_1=str+"";
        c=str_1.toCharArray();
        Arrays.sort(c);
        String str_2=new String(c);
        int n=Integer.parseInt(str_2);
        if(n>max){
            max=n;
            max_user=str;
        }
    }
    System.out.println(max);

}

}

发表于 2018-09-05 22:55:56 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc =new Scanner(System.in);
        int num = sc.nextInt();
        int[] nums = new int[num];
       for(int i=0;i<num;i++)
       {
           nums[i]=sc.nextInt();
       }
       System.out.println(getMinMax(nums));
        
    }
    public static int getMinMax(int[] nums)
    {
        int temp,max=0;
        for(int i=0;i<nums.length;i++)
        {
            ArrayList list = new ArrayList();
            while(nums[i]!=0)
            {
                list.add(nums[i]%10);
                nums[i]/=10;
            }
            Collections.sort(list);
            StringBuffer sb = new StringBuffer();
            for(int j=0;j<list.size();j++)
            {
                sb.append(list.get(j));
            }
            temp=Integer.parseInt(sb.toString());
            if(temp>max)
                max = temp;
        }
        return max;
    }
}

发表于 2018-08-08 21:37:49 回复(0)
def main():
    input()
    print(max([int(''.join(digits))
               for digits in map(sorted, input().split(' '))]))


if __name__ == '__main__':
    main()

发表于 2018-04-18 17:08:20 回复(0)
def themaxnumber():
    number=raw_input()
    mylist=raw_input().split()
    newlist=[sorted(i) for i in mylist]
    maxnumber=-float('inf')
    for i in newlist:
        maxnumber=max(maxnumber,int(''.join(i)))
    return maxnumber
print themaxnumber()
发表于 2018-02-28 22:43:17 回复(0)
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
importjava.util.Scanner;
 
publicclassMain {
    publicstaticvoidmain(String[] args) {
        booleanT = false;
        booleanflag = false;
        List<Integer> list = newArrayList<Integer>();
        List<Integer> list2 = newArrayList<Integer>();
        List<Integer> list3 = newArrayList<Integer>();
        StringBuilder sb = newStringBuilder();
        Scanner sc = newScanner(System.in);
        inta = 0, b = 0, c = 0;
        intx[] = newint[100];
        int[] y = newint[100];
        intn = Integer.parseInt(sc.nextLine());// 参加游戏的人数
        if(1<= n && n <= 50) {
            T = true;
        }
        for(inti = 0; i < n; i++) {
            x[i] = sc.nextInt();// 输入的数字
            if(0<= x[i] && x[i] <= 100000) {
                flag = true;
            }
        }
        // 数字变换
        if(T && flag) {
            for(inti = 0; i < n; i++) {
                while(x[i] > 0) {
                    a = x[i] % 10;
                    x[i] = x[i] / 10;
                    list.add(a);
                }
                ints = list.size();
                for(intj = 0; j < s; j++) {
                    Integer min = Collections.min(list);
                    list.remove(min);
                    list2.add(min);
                }
 
                for(Integer integer : list2) {
                    sb.append(integer);
                }
                y[i] = Integer.parseInt(sb.toString());
                sb.delete(0, s);
                list.removeAll(list);
                list2.removeAll(list2);
                list3.add(y[i]);
            }
        }
        Integer max = Collections.max(list3);
        System.out.println(max);
    }
}

发表于 2017-12-29 15:16:19 回复(0)