算法Day2-惡搞算法(網傳88萬代碼生成)java實現


起因

在群里閑逛看到這張圖
不看廢話-代碼見文末[點擊跳轉]

我覺得這是個段子,但有群友覺得這是真實故事

然后我就寫了個腳本生成這88萬行代碼反駁他
發現這算是入門算法題;

  • 1.計算數字位數
  • 2.倒序輸出

代碼

跳轉到的地方

class Scratch {
    public static void main(String[] args) {
        for(int i=1;i<100000;i++){
            int[] arr = transfer(i);
            int lth=arr.length;
            System.out.printf("case %d:",i);
            System.out.printf("\n");
            System.out.printf("count << \"是%d位數\"<<end1;",lth);
            System.out.printf("\n");
            System.out.printf("count << \"個位數是:%d\"<<end1;",arr[0]);
            System.out.printf("\n");
            if (lth>1){
                System.out.printf("count << \"十位數是:%d\"<<end1;",arr[1]);
                System.out.printf("\n");
                if(lth>2){
                    System.out.printf("count << \"百位數是:%d\"<<end1;",arr[2]);
                    System.out.printf("\n");
                    if(lth>3){
                        System.out.printf("count << \"千位數是:%d\"<<end1;",arr[3]);
                        System.out.printf("\n");
                        if(lth>4){
                            System.out.printf("count << \"萬位數是:%d\"<<end1;",arr[4]);
                            System.out.printf("\n");
                        }
                    }
                }
            }
            StringBuffer r=new StringBuffer();
            for(int c=arr.length-1; c>=0; c--){
                r.append(arr[c]);
            }

            System.out.printf("count << \"倒過來是:%s\"<<end1;",r.toString());
            System.out.println();
            System.out.println("break;");
        }
    }
    public static int[] transfer(int a ){
        String str =null;
        str = Integer.toString(a);
        int[] arr = new int[str.length()];
        for(int i=0; i<arr.length; i++){
            char c = str.charAt(i);
            String s = String.valueOf(c);
            int num = Integer.parseInt(s);
            arr[i] = num;
        }
        return arr;
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM