题目:https://pintia.cn/problem-sets/17/problems/264

题解:https://www.liuchuo.net/archives/2019

自解(一次通过,但是代码显得繁琐。关键词:vector的初始化、遍历。):

#include <iostream>
#include <string>
#include <vector>
#define Total 54
using namespace std;
vector <string> cardSet;
string kind[]={"S","H","C","D"};
void init(vector<string> &cardSet){
	for(int j=0; j<4;j++){
		for(int i =1; i<=13; i++){
			string temp = kind[j];
			cardSet.push_back(temp.append(to_string(i)));
		}
	}
	cardSet.push_back("J1");
	cardSet.push_back("J2");
}
void dispVec(vector<string> cardSet){
	for(int i =0; i<cardSet.size()-1; i++){
		cout<<cardSet[i]<<" ";
	}
	cout<<cardSet[cardSet.size()-1]<<endl;
}
int main()
{
    init(cardSet);
    int x;
    cin>>x;
    int loc[54];
    for(int i=0;i<Total;i++){
    	cin>> loc[i];
	}//input finished	
	vector<string> dest(Total);
	for(int i = 1; i <= x; i++) {
		for(int j=0; j < Total; j++) {			
			dest[loc[j] - 1] = cardSet[j];
		}
		cardSet = dest;
	}
	dispVec(cardSet);
	return 0;
}

 题解代码:通过迭代求出终态,然后用整除/、模%获得打印结果,很巧妙。

#include <cstdio>
using namespace std;
int main() {
  int cnt;
  scanf("%d", &cnt);
  int start[54], end[54], scan[54];
  for (int i = 0; i < 54; i++) {
    scanf("%d", &scan[i]);
    end[i] = i;
  }
  for (int i = 0; i < cnt; i++) {
    for (int j = 0; j < 54; j++) {
      start[j] = end[j];
    }
    for (int j = 0; j < 54; j++) {
      end[scan[j] - 1] = start[j];
    }
  }
  char c[6] = "SHCDJ";
  for (int i = 0; i < 54; i++) {
    printf("%c%d", c[end[i] / 13], end[i] % 13 + 1);
    printf("%c", i < 53 ? ' ' : '\n');
  }
  //    for(int i=0;i<53;i++){
  //    	printf("%c%d ",c[end[i]/13], end[i]%13+1);//2 exit
  //	}
  //	printf("%c%d\n",c[end[53]/13], end[53]%13+1);//bad format controll
  return 0;
}

 

 posted on 2020-08-03 17:29  邢涌芝  阅读(59)  评论(0编辑  收藏  举报