CSU——G: Game Rank

Description

The gaming company Sandstorm is developing an online two player game. You have been asked to implement the ranking system. All players have a rank determining their playing strength which gets updated after every game played. There are 25 regular ranks, and an extra rank, “Legend”, above that. The ranks are numbered in decreas- ing order, 25 being the lowest rank, 1 the second highest rank, and Legend the highest rank.

Each rank has a certain number of “stars” that one needs to gain before advancing to the next rank. If a player wins a game, she gains a star. If before the game the player was on rank 6-25, and this was the third or more consecutive win, she gains an additional bonus star for that win. When she has all the stars for her rank (see list below) and gains another star, she will instead gain one rank and have one star on the new rank.

For instance, if before a winning game the player had all the stars on her current rank, she will after the game have gained one rank and have 1 or 2 stars (depending on whether she got a bonus star) on the new rank. If on the other hand she had all stars except one on a rank, and won a game that also gave her a bonus star, she would gain one rank and have 1 star on the new rank. If a player on rank 1-20 loses a game, she loses a star. If a player has zero stars on a rank and loses a star, she will lose a rank and have all stars minus one on the rank below. However, one can never drop below rank 20 (losing a game at rank 20 with no stars will have no effect).

If a player reaches the Legend rank, she will stay legend no matter how many losses she incurs afterwards. The number of stars on each rank are as follows:

• Rank 25-21: 2 stars

• Rank 20-16: 3 stars

• Rank 15-11: 4 stars

• Rank 10-1: 5 stars

A player starts at rank 25 with no stars. Given the match history of a player, what is her rank at the end of the sequence of matches?

Input

There will be several test cases. Each case consists of a single line describing the sequence of matches. Each character corre- sponds to one game; ‘W’ represents a win and ‘L’ a loss. The length of the line is between 1 and 10 000 characters (inclusive).

Output

Output a single line containing a rank after having played the given sequence of games; either an integer between 1 and 25 or “Legend”.

Sample Input

WW
WWW
WWWW
WLWLWLWL
WWWWWWWWWLLWW
WWWWWWWWWLWWL

Sample Output

25
24
23
24
19
18


思路:其实这题就是一道模拟操作的题目,但是这题题意给的条件太多了,模拟起来会容易出错,有这几个需要注意的点,第一个连赢三场及以上的可获得两个星,但是只在rank名词在6—25之间有效;晋级赛赢了,不仅晋级而且还有一颗星;超过20级之后不会再掉段了,即使输了也不会去21,;到达Legend层次,无论输赢都一样等等。

所以这题本来是读完一串再判断,不知道为什么总是wa,反正看了很久觉得没有问题,就是wa,你说气人不气人。然后换了思路,每一个字符读进来我就进行判断,进行加减,就过了。反正我到现在都不知道那个为什么会错。先附上ac代码,再附上之前的代码,如果有小伙伴发现错误,希望能和我说下,不胜感激!

ac代码:

#include<iostream>
#include<cstring>
using namespace std;
int k[30] = {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2};
char s[10005];
int main() {
    int n;
    while (cin>>s)
    {
        n = strlen(s);
        int ans = 25, cnt = 0;
        for (int i = 0; i < n; i++)
        {
            if (s[i] == 'W')
            {
                cnt++;
                if (i > 1 && s[i - 1] == 'W' && s[i - 2] == 'W' && ans >= 6)
                    cnt++;
                if (cnt > k[ans])
                {
                    cnt -= k[ans];
                    ans--;
                }
            }
            else
            {
                if (ans > 20 || ans == 20 && cnt == 0)
                {
                    continue;
                }
                cnt--;
                if (cnt < 0)
                {
                    ans++;
                    cnt = k[ans] - 1;
                }
            }
            if (ans == 0)
                break;
        }
        if (ans)
        {
            cout<<ans<<endl;
        }
        else
        {
            cout<<"Legend"<<endl;
        }
    }
    return 0;

}

错误代码:

#include<stdio.h>
#include<iostream>
#include<string.h> 
using namespace std;

char c[10005];

int main()
{
	int f;
	while(scanf("%s", c) != EOF)
	{
		f = 0;
	int len, sum = 0, flag = 0;
	len = strlen(c);
	int l = 0, n, m;
	while(c[l] == 'L')
	l++;
	for( ; l < len; )
	{
		if(f == 1)
		break;
		n = 0;
		m = 0;
		while(c[l] == 'W')
		{
			n++;
			l++;
		}
		if(n > 0)
		{
			if(n > 2 && sum < 70)
			{
				sum += 2 * (n - 1);
				if(sum > 70)
				sum -= (sum - 70) / 2;
			}
			else
			sum += n; 
			if(sum > 95)
			{
				cout<<"Legend"<<endl;
				f = 1;
			}
			continue;
		}
		while(c[l] == 'L')
		{
			m++;
			l++;
		}
		if(m > 0)
		{
			if(sum > 95)
			{
				cout<<"Legend"<<endl;
				f = 1;
			}
			else if(sum > 10)
			{
				sum -= m;
				if(sum < 10)
				{
					sum = 10;
					if(l == len)
					flag = 1;
				}	
			 } 
			continue;
		}
	}
	if(f == 1)
	continue;
//	cout<<sum<<endl;
//	cout<<m<<endl;
	int k;
	if(sum <= 10)
	{
		k = (sum + 1) / 2;
		if(flag == 1)
		k++;
	}
	else if(sum <= 25)
	{
		sum -= 10;
		k = (sum + 2) / 3 + 5;
		if(m > 0 && sum % 3 == 0)
		k++;
	}
	else if(sum <= 45)
	{
		sum -= 25;
		k = (sum + 3) / 4 + 10;
		if(m > 0 && sum % 4 == 0)
		k++;
	}
	else if(sum <= 95)
	{
		sum -= 45;
		k = (sum + 4) / 5 + 15;
		if(m > 0 && sum % 5 == 0)
		k++;
	}
	else
	{
		cout<<"Legend"<<endl;
		continue;
	}
	cout<<25 - k + 1<<endl;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值