TopCoder 300 points 24-SRM 155 DIV 2 75/250 30%
2013-07-13 16:21
357 查看
Problem Statement | ||||||||||||
The Incas used a sophisticated system of record keeping consisting of bundles of knotted cords. Such a bundle of cords is called a quipu. Each individual cord represents a single number. Surprisingly, the Incas used a base-10 positional system, just like we do today. Each digit of a number is represented by a cluster of adjacent knots, with spaces between neighboring clusters. The digit is determined by the number of knots in the cluster. For example, the number 243 would be represented by a cord with knots tied in the following pattern -XX-XXXX-XXX- where each uppercase 'X' represents a knot and each '-' represents an unknotted segment of cord (all quotes for clarity only). Unlike many ancient civilizations, the Incas were aware of the concept of zero, and used it in their quipus. A zero is represented by a cluster containing no knots. For example, the number 204003 would be represented by a cord with knots tied in the following pattern -XX--XXXX---XXX- ^^ ^^^ ^^ ^^^ ^^ two zeros between these three segments ^^ one zero between these two segments Notice how adjacent dashes signal the presence of a zero. Your task is to translate a single quipu cord into an integer. The cord will be given as a String knots containing only the characters 'X' and '-'. There will be a single '-' between each cluster of 'X's, as well as a leading '-' and a trailing '-'. The first cluster will not be empty. | ||||||||||||
Definition | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | knots contains between 3 and 50 characters, inclusive. | |||||||||||
- | knots contains only the characters 'X' and '-'. Note that 'X' is uppercase. | |||||||||||
- | The first and last characters of knots are '-'s. The second character is 'X'. | |||||||||||
- | knots does not contain 10 consecutive 'X's. | |||||||||||
- | knots will represent a number between 1 and 1000000, inclusive. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
| ||||||||||||
3) | ||||||||||||
| ||||||||||||
4) | ||||||||||||
|
还是要仔细啊,,,提交了两次
public class Quipu { public final static char X = 'X'; public final static char M = '-'; public static int readKnots(String knots) { StringBuilder sb = new StringBuilder(); int x = 0; int minus = 0; int len = knots.length(); boolean currentX = true; char ch; for (int i = 1; i < len - 1; i++) { ch = knots.charAt(i); if (currentX) { if (ch == X) { x++; if (i == len - 2) { sb.append(x); } } else { minus++; sb.append(x); if (i == len - 2) { for (int k = 0; k < minus; k++) sb.append('0'); } x = 0; currentX = false; } } else { if (ch == M) { minus++; if (i == len - 2) { for (int k = 0; k < minus; k++) sb.append('0'); } } else { x++; currentX = true; if (i == len - 2) { sb.append(x); } if (minus > 1) { for (int k = 0; k < minus - 1; k++) sb.append('0'); } minus = 0; } } } return Integer.parseInt(sb.toString()); } }
相关文章推荐
- TopCoder 300 points 27-SRM 157 DIV 1 90/300 30%
- TopCoder 300 points 1-SRM 144 DIV 1 90/300 30%
- TopCoder 250 points 9-SRM 148 DIV 1 75/250 30%
- TopCoder 300 points 23-SRM 155 DIV 1 116.71/300 38.90%
- TopCoder 250 points 28-SRM 157 DIV 2 185.60/250 74.24%
- TopCoder 300 points 25-SRM 156 DIV 1 90.77/300 30.26%
- TopCoder 250 points 14-SRM 150 DIV 2 86.25/250 34.50%
- TopCoder 350 points 21-SRM 154 DIV 1 105/350 30%
- TopCoder 250 points 15-SRM 151 DIV 1 121.73/250 48.69%
- TopCoder 250 points 16-SRM 151 DIV 2 97.63/250 39.05%
- TopCoder 250 points 3-SRM 145 DIV 1 84.71/250 33.88%
- TopCoder 250 points 4-SRM 145 DIV 2 128.72/250 51.49%
- TopCoder 300 points 5-SRM 146 DIV 1 109.50/300 36.5%
- TopCoder 250 points 13-SRM 150 DIV 1 82.63/250 33.05%
- TopCoder 250 points 17-SRM 152 DIV 1 106.51/250 42.60%
- TopCoder 250 points 6-SRM 146 DIV 2 162.67/250 65.07%
- TopCoder 250 points 18-SRM 152 DIV 2 167.05/250 66.82%
- TopCoder 250 points 19-SRM 153 DIV 1 84.72/250 33.89%
- TopCoder 300 points 26-SRM 156 DIV 2 78.06/300 26.02%
- TopCoder 250 points 20-SRM 153 DIV 2 216.58/250 86.63%