NYOJ 540

简介:   为了给学弟学妹讲课,我水了一道题…… import java.util.Arrays; import java.util.Scanner; public class NYOJ540 { public static void main(String[] args) { ...

  为了给学弟学妹讲课,我水了一道题……

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

public class NYOJ540 {

    public static void main(String[] args) {
        int from, to, T;
        Node node[];
        Scanner sc = new Scanner(System.in);
        T = sc.nextInt();
        int temp;
        while(T-->0) {
            from = sc.nextInt();
            to = sc.nextInt();
            node = new Node[to-from+1];
            int j = 0;
            for(int i=0; i<node.length; i++) {
                //q已经初始化为0了
                node[i] = new Node();
            }
            for(int i=from; i<=to; i++) {
                node[j].p = i;
                temp = i;
                while(temp>0) {
                    /*
                     * 必须在大while循环构造node数组
                     * 否则就第一组数据正确
                     * 因为下面这一句用到了以前的q值
                     */
                    node[j].q = node[j].q*10 + temp%10;
                    temp /= 10;
                }
                j++;
            }
            /*
             * 只看API函数,第三个参数是toIndex,以为是下标
             * 谁知道具体一看不包括,wa了n次
             */
            Arrays.sort(node,0,to-from+1);
            System.out.print(node[0].p);
            for(int i=1; i<to-from; i++) {
                System.out.print(" "+node[i].p);
            }
            System.out.println(" "+node[to-from].p);
        }
    }
}

class Node implements Comparable<Node>{
    int p;
    int q;
    
    public Node() {
        this.p = 0;
        this.q = 0;
    }

    @Override
    public int compareTo(Node o) {
        // TODO Auto-generated method stub
        Node other = o;
        return this.q - other.q;
    }
}
目录
相关文章
|
计算机视觉
NYOJ 289
  苹果 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 ctest有n个苹果,要将它放入容量为v的背包。给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值。
735 0
|
JavaScript
NYOJ&#160;17
时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 求一个字符串的最长递增子序列的长度 如:dabdbf最长递增子序列就是abdf,长度为4 输入 第一行一个整数0 随后的n行,每行有一个字符串,该字符串的长度不会超过10000 输出 输出字符串的最长递增子序列的长度 样例输入 3 aaa ababc abklmncdefg 样例输出 1 3 7 题目很经典,学习一下吧。
649 0
NYOJ 485
  A*B Problem 时间限制:1000 ms | 内存限制:65535 KB 难度:2   描述 设计一个程序求出A*B,然后将其结果每一位相加得到C,如果C的位数大于等于2,继续将C的各位数相加,直到结果是个一位数k。
993 0
NYOJ 205
  求余数 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 现在给你一个自然数n,它的位数小于等于一百万,现在你要做的就是求出这个数除10003之后的余数   输入 第一行有一个整数m(1T; 13 scanf("%*c")...
669 0
NYOJ 283
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 /* 9 bool cmp(char *a,char *b) 10...
473 0
NYOJ 93
  汉诺塔(三) 时间限制:3000 ms | 内存限制:65535 KB 难度:3   描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针。
579 0
|
人工智能
NYOJ 55
  懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3   描述 小明很想吃果子,正好果园果子熟了。在果园里,小明已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。
934 0
NYOJ 113
1 #include 2 #include 3 using namespace std; 4 5 int main() 6 { 7 int pos=-1; 8 string s; 9 while(getline(cin,s)) 10 { 11 while((pos=s.
664 0
NYOJ 27
  水池数目 时间限制:3000 ms | 内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上仅标识了此处是否是水池,现在,你的任务来了,请用计算机算出该地图中共有几个水池。
770 0