一个简单的acm竞赛题

马上要参加学校的acm竞赛,找题突击突击练练手
摘取题目的原网页:https://blog.csdn.net/monster_ayb/article/details/79204900
( 作者:monster_ayb )

题目:
There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had different colors. Stones in a row are considered neighboring if there are no other stones between them.

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of stones on the table.

The next line contains string s, which represents the colors of the stones. We’ll consider the stones in the row numbered from 1 to n from left to right. Then the i-th character s equals"R", if the i-th stone is red, “G”, if it’s green and “B”, if it’s blue.

Output

Print a single integer — the answer to the problem.

Examples

input

3
RRG

output

1

input

5
RRRRR

output

4

input

4
BRBG

output

0

我这里用Java解决

读题:
桌子上放一排只有红蓝绿的球。
从头往尾去掉球,去掉最少的个数能让后面的全部的两两相邻的球,异色

我的初步分析:
1、输入用字符串,然后转字符数组。
2、对于最少的球数量,用算法进行解决
3、给出输出

我的对于算法函数的分析:
1、输入函数的参数:转换后的字符数组
2、遍历一遍数组,找到最后一组相邻同色的球,然后把计算一下该组第一个球到首元素一共有多少个元素,就是要去掉的个数

代码实现:

public class CountRetrieveBall {
int position ;//最后一个相邻同色的第1个球的是从左往右第几个题
CountRetrieveBall(char[] s)
{
int i=0;
position=0;
for(;i<s.length-1;i++)
{
if(s[i]==s[i+1])
{
position=i+1;
}
}
System.out.println(position);
}
}

public class retrieve_ball
{
public static void main(String[] args)
{
Scanner reader= new Scanner(System.in);
int number =reader.nextInt();
String s =reader.next();
char[] ball=s.toCharArray();
CountRetrieveBall count = new CountRetrieveBall(ball);
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值