【C++】AOJ基础题 ITP1_9_B Shuffle

Shuffle

Your task is to shuffle a deck of n cards, each of which is marked by a alphabetical letter.

A single shuffle action takes out h cards from the bottom of the deck and moves them to the top of the deck.

The deck of cards is represented by a string as follows.

abcdeefab

The first character and the last character correspond to the card located at the bottom of the deck and the card on the top of the deck respectively.

For example, a shuffle with h = 4 to the above deck, moves the first 4 characters “abcd” to the end of the remaining characters “eefab”, and generates the following deck:

eefababcd

You can repeat such shuffle operations.

Write a program which reads a deck (a string) and a sequence of h, and prints the final state (a string).

Input

The input consists of multiple datasets. Each dataset is given in the following format:

A string which represents a deck
The number of shuffle m
h1
h2
  .
  .
hm

The input ends with a single character ‘-’ for the string.

Constraints

  • The length of the string ≤ 200
  • 1 ≤ m ≤ 100
  • 1 ≤ hi < The length of the string
  • The number of datasets ≤ 10

Output

For each dataset, print a string which represents the final state in a line.

Sample Input

aabc
3
1
2
1
vwxyz
2
3
4
-

Sample Output

aabc
xyzvw

問題を解く

  • 最基础的思路,后移再前移下标。
  • 这里一开始是用的string类,但是string不会检查下标越界,而且用[]只能访问已存在的字符,不能直接赋值,因为b的内部用来存储字符的空间还未被分配。
  • 如果要用string的话,换成加法或者push_back操作即可。
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

char deck[201];

void shuffle(int count)
{
	int len = strlen(deck);
	for (int i = 0; i < count; i++)
	{
		deck[i + len] = deck[i];
	}

	for (int i = 0; i < len; i++)
	{
		deck[i] = deck[count + i];
		deck[count + i] = 0;
	}
}

int main()
{
	int m = 0;
	int count = 0;

	while (true)
	{
		cin >> deck;

		if (deck[0] == '-') break;

		cin >> m;

		for (int i = 0; i < m; i++)
		{
			cin >> count;
			shuffle(count);
		}

		cout << deck << endl;
	}


	return 0;
}

A Good Try

  • 一个思路是模仿Ring那道题,复制连接很多份deck,然后累加后移位置得到下标即可;
  • 第二种思路是直接用substr方法,简单粗暴。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值