TopCoder 300 points 24-SRM 155 DIV 2 75/250 30%

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

 
Class:Quipu
Method:readKnots
Parameters:String
Returns:int
Method signature:int readKnots(String knots)
(be sure your method is public)
 
 

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) 
 
"-XX-XXXX-XXX-"
Returns: 243
The first example above.
1) 
 
"-XX--XXXX---XXX-"
Returns: 204003
The second example above.
2) 
 
"-X-"
Returns: 1
 
3) 
 
"-X-------"
Returns: 1000000
 
4) 
 
"-XXXXXXXXX--XXXXXXXXX-XXXXXXXXX-XXXXXXX-XXXXXXXXX-"
Returns: 909979
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     


还是要仔细啊,,,提交了两次

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());
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值