首页 > 代码库 > POJ 1047 Round and Round We Go 最详细的解题报告

POJ 1047 Round and Round We Go 最详细的解题报告

题目链接: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 = http://www.mamicode.com/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 = http://www.mamicode.com/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 = http://www.mamicode.com/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 = http://www.mamicode.com/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 = http://www.mamicode.com/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 最详细的解题报告