POJ 1047 Round and Round We Go 最详细的解题报告
解题思路:用程序实现一个乘法功能,将给定的字符串依次做旋转,然后进行比较。由于题目比较简单,所以不做过多的详解。
具体算法(java版,可以直接AC)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line;
while (scanner.hasNext()) {
line = scanner.next();
MyNumber number = new MyNumber(line);
int count = 0;
for (int i = 2; i <= line.length(); i++) {
NumNode[] result = number.multiply(i);
if (number.isCycle(result)) {
count++;
}
}
if (count == line.length()-1) {
System.out.println(line + " is cyclic");
} else {
System.out.println(line + " is not cyclic");
}
}
}
}
class NumNode {
int value;
int carry;
public NumNode() {
this.value = 0;
this.carry = 0;
} }
class MyNumber {
private NumNode[] data;
private int length;
private String[] rotation;
private boolean isRotated;
public MyNumber(String line) {
this.length = line.length();
this.isRotated=false;
this.data = new NumNode[this.length];
this.rotation = new String[this.length];
for (int i = this.length - 1; i >= 0; i--) {
this.data[i] = new NumNode();
this.data[i].value = line.charAt(i) - '0';
}
}
private void rotate() {
for (int i = 0; i < this.length; i++) {
StringBuffer buffer = new StringBuffer();
for (int j = i; j < this.length; j++) {
buffer.append(this.data[j].value);
}
for (int j = 0; j < i; j++) {
buffer.append(this.data[j].value);
}
this.rotation[i] = buffer.toString();
}
}
public NumNode[] multiply(int a) {
NumNode[] result = new NumNode[this.length];
for (int i = this.length - 1; i >= 0; i--) {
int value = this.data[i].value * a;
result[i] = new NumNode();
if (i + 1 < this.length) {
value += result[i + 1].carry;
}
result[i].value = value % 10;
result[i].carry = value / 10;
}
return result;
}
public boolean equals(String s) {
for (String str : this.rotation) {
if (str.equals(s))
return true;
}
return false;
}
public boolean isCycle(NumNode[] num) {
if (num[0].carry > 0)
return false;
if(!this.isRotated){
this.rotate();
this.isRotated=true;
}
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < num.length; i++) {
buffer.append(num[i].value);
}
return this.equals(buffer.toString());
}
public String toString() {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < this.length; i++) {
buffer.append(this.data[i].value);
}
return buffer.toString();
}
}
POJ 1047 Round and Round We Go 最详细的解题报告的更多相关文章
- CH Round #55 - Streaming #6 (NOIP模拟赛day2)解题报告
T1九九归一 描述 萌蛋在练习模n意义下的乘法时发现,总有一些数,在自乘若干次以后,会变成1.例如n=7,那么5×5 mod 7=4,4×5 mod 7=6,6×5 mod 7=2,2×5 mod 7 ...
- CH Round #54 - Streaming #5 (NOIP模拟赛Day1)解题报告
最近参加了很多CH上的比赛呢~Rating--了..题目各种跪烂.各种膜拜大神OTZZZ T1珠 描述 萌蛋有n颗珠子,每一颗珠子都写有一个数字.萌蛋把它们用线串成了环.我们称一个数字串是有趣的,当且 ...
- Valentine's Day Round 1001.Ferries Wheel(hdu 5174)解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5174 题目意思:给出 n 个人坐的缆车值,假设有 k 个缆车,缆车值 A[i] 需要满足:A[i−1] ...
- Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)解题报告
对于这道水题本人觉得应该应用贪心算法来解这道题: 下面就贴出本人的代码吧: #include<cstdio> #include<iostream> using namespac ...
- POJ 1046 Color Me Less 最详细的解题报告
题目来源:POJ 1046 Color Me Less 题目大意:每一个颜色由R.G.B三部分组成,D=Math.sqrt(Math.pow((left.red - right.red), 2)+ M ...
- POJ 1057 File Mapping 最详细的解题报告
题目来源:POJ 1057 File Mapping 题目大意:像我的电脑那样显示文件夹和文件信息,其中在同一级目录内,文件夹排在文件的前面并且文件夹的顺序不变,同一级目录中文件按字母序排列.文件以‘ ...
- POJ 1063 Flip and Shift 最详细的解题报告
题目来源:Flip and Shift 题目大意:一个椭圆形的环形容器中有黑色和白色两种盘子,问你是否可以将黑色的盘子连续的放在一起.你可以有以下两种操作: 1.顺时针旋转所有的盘子 2.顺时针旋转3 ...
- POJ 1050 To the Max 最详细的解题报告
题目来源:To the Max 题目大意:给定一个N*N的矩阵,求该矩阵中的某一个矩形,该矩形内各元素之和最大,即最大子矩阵问题. 解题方法:最大子序列之和的扩展 解题步骤: 1.定义一个N*N的矩阵 ...
- POJ 1095 Trees Made to Order 最详细的解题报告
题目来源:Trees Made to Order 题目大意:根据下面的规则给一棵二叉树编号: 规则1:如果二叉树为空,则编号为0: 规则2:如果二叉树只有一个节点,则编号为1: 规则3:所有含有m个节 ...
随机推荐
- pip速度过慢解决方法
国内源: 新版ubuntu要求使用https源,要注意. 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.c ...
- 如何从二进制文件中读取int型序列
使用的主要函数是int.from_bytes 代码如下: f = open('./T26.dat', 'rb') for i in range(20): A = f.read(2) A = int.f ...
- cb52a_c++_STL_堆排序算法make_push_pop_sort_heap
cb52a_c++_STL_堆排序算法make_push_pop_sort_heapheapsort堆排序算法make_heap()-特殊的二叉树,每一个节点都比根小,根就是最大的数.大根堆,也可以做 ...
- 数据库事务(2)---ACID与并发问题
事务 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit).在计算机术语中,事务通常就是指数据库事务. 概念 一 ...
- java scoket Blocking 阻塞IO socket通信一
package bhz.bio; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; p ...
- 计算机网络之ARP协议
ARP ARP(Address Resolution Protocol),即地址解析协议,是根据IP地址解析物理地址的一个TCP/IP协议.主机将包含目标IP地址信息的ARP请求广播到网络中的所有主机 ...
- BOM问题-对于php的影响
甲.BOM说明 BOM(Byte Order Mark),是UTF编码方案里用于标识编码的标准标记.这个标记是可选的,UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明当前编码方式.但如果文件 ...
- el-switch 初始值(默认值)不能正确显示状态问题
<el-table-column align="center" label="状态"> <template slot-scope= ...
- 1166 - Unknown error 1166[mysql 错误
错误码 1166 原因 字段名因为是复制过来的, 末尾存在了一个空格换行
- CSS选择器使用
今天要对CSS选择器的使用方法做一个全面的总结(几乎全部是从这篇文章摘抄的 https://blog.csdn.net/qq_39241986/article/details/82185697) CS ...