zoukankan      html  css  js  c++  java
  • 788-旋转数字

    我们称一个数 X 为好数, 如果它的每位数字逐个地被旋转 180 度后,我们仍可以得到一个有效的,且和 X 不同的数。要求每位数字都要被旋转。
    如果一个数的每位数字被旋转以后仍然还是一个数字, 则这个数是有效的。0, 1, 和 8 被旋转后仍然是它们自己;2 和 5 可以互相旋转成对方;6 和 9 同理,除了这些以外其他的数字旋转以后都不再是有效的数字。
    现在我们有一个正整数 N, 计算从 1 到 N 中有多少个数 X 是好数?
    示例:
    输入: 10
    输出: 4
    解释: 
    在[1, 10]中有四个好数: 2, 5, 6, 9。
    注意 1 和 10 不是好数, 因为他们在旋转之后不变。
    
    注意:
    N 的取值范围是 [1, 10000]。
    
    public static int rotatedDigits(int N) {
            int count=0;
            Integer num[]={0,1,2,5,6,8,9};
            Map<Integer,Integer> map=new HashMap<>();
            map.put(0,0);
            map.put(1,1);
            map.put(2,5);
            map.put(5,2);
            map.put(6,9);
            map.put(8,8);
            map.put(9,6);
            Set<Integer> set=new HashSet<>();
            for (int i=0;i<num.length;i++){
                set.add(num[i]);
            }
            for (int i=1;i<=N;i++){
                int temp=i;
                int z=1;
                int x=0;
                while (temp!=0) {
                    int c = temp % 10;
                    if (!set.contains(c)) {
                        x=i;
                       break;
                    }else {
                        c = map.get(c);
                        x += c * z;
                        z *= 10;
                        temp /= 10;
                    }
    
                }
                if (x!=i){
                    System.out.println(x);
                    count++;
                }
            }
            return count;
        }
  • 相关阅读:
    SPOJ-BRCKTS (括号序列,线段树)
    bzoj 2223 [Coci 2009]PATULJCI
    bzoj 1049 [HAOI2006]数字序列
    bzoj2091 [Poi2010]The Minima Game
    bzoj 1016 [JSOI2008]最小生成树计数
    bzoj5105 晨跑 数论lcm
    bzoj 1211 [HNOI2004]树的计数
    bzoj 1430 小猴打架 prufer 性质
    Swing界面组件的通用属性
    数字调节控件JSpinner的使用
  • 原文地址:https://www.cnblogs.com/dloading/p/10853723.html
Copyright © 2011-2022 走看看