关于段错误

焚寂六月 2017-01-06 01:23:02
题目:
PATA 1042. Shuffling Machine (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where employees collaborate with gamblers by performing inadequate shuffles, many casinos employ automatic shuffling machines. Your task is to simulate a shuffling machine.

The machine shuffles a deck of 54 cards according to a given random order and repeats for a given number of times. It is assumed that the initial status of a card deck is in the following order:

S1, S2, ..., S13, H1, H2, ..., H13, C1, C2, ..., C13, D1, D2, ..., D13, J1, J2

where "S" stands for "Spade", "H" for "Heart", "C" for "Club", "D" for "Diamond", and "J" for "Joker". A given order is a permutation of distinct integers in [1, 54]. If the number at the i-th position is j, it means to move the card from position i to position j. For example, suppose we only have 5 cards: S3, H5, C1, D13 and J2. Given a shuffling order {4, 2, 5, 3, 1}, the result will be: J2, H5, D13, S3, C1. If we are to repeat the shuffling again, the result will be: C1, H5, S3, J2, D13.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer K (<= 20) which is the number of repeat times. Then the next line contains the given order. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the shuffling results in one line. All the cards are separated by a space, and there must be no extra space at the end of the line.

大致意思就是将S1, S2, ..., S13, H1, H2, ..., H13, C1, C2, ..., C13, D1, D2, ..., D13, J1, J2,按给定的顺序排序K次,K和给定的顺序都是自己输入的。

代码如下:

#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

int main()
{
int arr[54],K,i,j;
string str1[54] = {"S1","S2","S3","S4","S5","S6","S7","S8","S9","S10","S11","S12","S13","H1","H2","H3","H4","H5","H6","H7","H8","H9","H10","H11","H12","H13","C1","C2","C3","C4","C5","C6","C7","C8","C9","C10","C11","C12","C13","D1","D2","D3","D4","D5","D6","D7","D8","D9","D10","D11","D12","D13","J1","J2"};
string str2[54];
memcpy(str2,str1,sizeof(str1));

scanf("%d",&K);
for(i = 0;i < 54;i++)
{
scanf("%d",&arr[i]);
}

for(j = 0;j < K;j++)
{
for(i = 0;i < 54;i++)
{
str2[arr[i]-1] = str1[i];
}
memcpy(str1,str2,sizeof(str2));
}

for(i = 0;i < 54;i++)
{
printf("%s", str2[i].c_str());
if(i != 53)
{
printf(" ");
}
}

return 0;
}

本机编译通过,运行输入题设测试用例出错,已停止工作,请问是什么地方出了错啊。而我用5个字符串的:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>

using namespace std;

int main()
{
int arr[5],K,i,j;
string str1[5] = {"S3","H5","C1","D13","J2"};
string str2[5];
//strcpy(str2,str1);
memcpy(str2,str1,sizeof(str1));
//str2[0].assign(str1[1]);
//str2[0] = str2[1];



scanf("%d",&K);
for(i = 0;i < 5;i++)
{
scanf("%d",&arr[i]);
}

for(j = 0;j < K;j++)
{
for(i = 0;i < 5;i++)
{
str2[arr[i]-1] = str1[i];
//str2[arr[i]-1].assign(str1[i]);
}
memcpy(str1,str2,sizeof(str2));
}

for(i = 0;i < 5;i++)
{
//cout << str2[i];
printf("%s", str2[i].c_str());


if(i != 4)
{
cout << " ";
}
}

return 0;
}
测试下能运行
...全文
81 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
焚寂六月 2017-01-07
  • 打赏
  • 举报
回复
没人回复吗/

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧