POJ 1047 Round and Round We Go 最詳細的解題報告

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   java   for   

題目連結:Round and Round We Go

解題思路:用程式實現一個乘法功能,將給定的字串依次做旋轉,然後進行比較。由於題目比較簡單,所以不做過多的詳解。

具體演算法(java版,可以直接AC)

  1 import java.util.Scanner;  2   3 public class Main {  4   5     public static void main(String[] args) {  6         Scanner scanner = new Scanner(System.in);  7         String line;  8         while (scanner.hasNext()) {  9             line = scanner.next(); 10             MyNumber number = new MyNumber(line); 11             int count = 0; 12             for (int i = 2; i <= line.length(); i++) { 13                 NumNode[] result = number.multiply(i); 14                 if (number.isCycle(result)) { 15                     count++; 16                 } 17             } 18             if (count == line.length()-1) { 19                 System.out.println(line + " is cyclic"); 20             } else { 21                 System.out.println(line + " is not cyclic"); 22             } 23         } 24     } 25  26 } 27  28 class NumNode { 29     int value; 30     int carry; 31  32     public NumNode() { 33         this.value = 0; 34         this.carry = 0; 35     } 40 } 41  42 class MyNumber { 43     private NumNode[] data; 44     private int length; 45     private String[] rotation; 46     private boolean isRotated; 47  48     public MyNumber(String line) { 49         this.length = line.length(); 50         this.isRotated=false; 51         this.data = new NumNode[this.length]; 52         this.rotation = new String[this.length]; 53         for (int i = this.length - 1; i >= 0; i--) { 54             this.data[i] = new NumNode(); 55             this.data[i].value = line.charAt(i) - ‘0‘; 56         } 57     } 58  59     private void rotate() { 60         for (int i = 0; i < this.length; i++) { 61             StringBuffer buffer = new StringBuffer(); 62             for (int j = i; j < this.length; j++) { 63                 buffer.append(this.data[j].value); 64             } 65             for (int j = 0; j < i; j++) { 66                 buffer.append(this.data[j].value); 67             } 68             this.rotation[i] = buffer.toString(); 69         } 70     } 71  72     public NumNode[] multiply(int a) { 73         NumNode[] result = new NumNode[this.length]; 74         for (int i = this.length - 1; i >= 0; i--) { 75             int value = this.data[i].value * a; 76             result[i] = new NumNode(); 77             if (i + 1 < this.length) { 78                 value += result[i + 1].carry; 79             } 80             result[i].value = value % 10; 81             result[i].carry = value / 10; 82         } 83         return result; 84     } 85  86     public boolean equals(String s) { 87         for (String str : this.rotation) { 88             if (str.equals(s)) 89                 return true; 90         } 91         return false; 92     } 93  94     public boolean isCycle(NumNode[] num) { 95         if (num[0].carry > 0) 96             return false; 97         if(!this.isRotated){ 98             this.rotate(); 99             this.isRotated=true;100         }101         StringBuffer buffer = new StringBuffer();102         for (int i = 0; i < num.length; i++) {103             buffer.append(num[i].value);104         }105         return this.equals(buffer.toString());106     }107 108     public String toString() {109         StringBuffer buffer = new StringBuffer();110         for (int i = 0; i < this.length; i++) {111             buffer.append(this.data[i].value);112         }113         return buffer.toString();114     }115 }

 

POJ 1047 Round and Round We Go 最詳細的解題報告

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.