• PAT Shuffling Machine


    题目: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 ? ' ' : '
    ');
      }
      //    for(int i=0;i<53;i++){
      //    	printf("%c%d ",c[end[i]/13], end[i]%13+1);//2 exit
      //	}
      //	printf("%c%d
    ",c[end[53]/13], end[53]%13+1);//bad format controll
      return 0;
    }
    
  • 相关阅读:
    136. Single Number
    125. Valid Palindrome
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    119. Pascal's Triangle II
    118. Pascal's Triangle
    集中式架构与分布式架构比较-copy
    分布式id产生-copy
    MySQL 5.7数据库参数优化-copy
    23个适合Java开发者的大数据工具和框架-copy
  • 原文地址:https://www.cnblogs.com/simon-chou/p/13427586.html
Copyright © 2020-2023  润新知