zoukankan      html  css  js  c++  java
  • hdu4550

    卡片游戏

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 924    Accepted Submission(s): 298


    Problem Description
      小明最近宅在家里无聊,于是他发明了一种有趣的游戏,游戏道具是N张叠在一起的卡片,每张卡片上都有一个数字,数字的范围是0~9,游戏规则如下:
      首先取最上方的卡片放到桌子上,然后每次取最上方的卡片,放到桌子上已有卡片序列的最右边或者最左边。当N张卡片全部都放到桌子上后,桌子上的N张卡片构成了一个数。这个数不能有前导0,也就是说最左边的卡片上的数字不能是0。游戏的目标是使这个数最小。
      现在你的任务是帮小明写段程序,求出这个最小数。
     
    Input
    第一行是一个数T,表示有T组测试数据;
    然后下面有T行, 每行是一个只含有0~9的字符串,表示N张叠在一起的卡片,最左边的数字表示最上方的卡片。

    [Technical Specification]
    T<=1000
    1 <= N <= 100
     
    Output
    对于每组测试数据,请在一行内输出能得到的最小数。
     
    Sample Input
    3
    565
    9876543210
    9876105432
     
    Sample Output
    556
    1234567890
    1678905432
     
    Source
     
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int main()
    {
        string str,card;
        char t;
        int T,i,j,len;
        scanf("%d",&T);
        while(T--)
        {
            cin>>str;
            len=str.size();
            t='9';
            for(i=len-1; i>=0; i--)
            {
                if(str[i]!='0'&&str[i]<t)
                {
                    j=i;
                    t=str[i];
                }
            }
            card=str[0];
            for(i=1; i<len; i++)
            {
                if(i==j)
                card=str[i]+card;
                else if(i<j)
                {
                    if(str[i]<=card[0])
                        card=str[i]+card;
                    else
                        card+=str[i];
                }
                else
                    card+=str[i];
            }
            cout<<card<<endl;
        }
        return 0;
    }
  • 相关阅读:
    LC.225. Implement Stack using Queues(using two queues)
    LC.232. Implement Queue using Stacks(use two stacks)
    sort numbers with two stacks(many duplicates)
    LC.154. Find Minimum in Rotated Sorted Array II
    LC.81. Search in Rotated Sorted Array II
    LC.35.Search Insert Position
    前后端分离:(一)
    Redis基本使用(一)
    GIT篇章(二)
    GIT篇章(一)
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3330727.html
Copyright © 2011-2022 走看看