ZJU 1225 Scramble Sort

分析:不是难题,只是折磨人的题,还好要变通变通,运用STL容易解决。

 

ContractedBlock.gif ExpandedBlockStart.gif Code
#include <iostream>
#include 
<deque>
#include 
<string>
#include 
<algorithm>
using namespace std;

deque
<string> word;
deque
<int> number;
deque
<int> offset;
int sum;
void ToLow(const string &s,char c[])
ExpandedBlockStart.gifContractedBlock.gif
{
    
int len = s.length();
    
for(int i=0;i<len;++i)
        
if(s[i]>='A' && s[i]<='Z')
            c[i] 
= s[i] + 32;
        
else
            c[i] 
= s[i];
    c[len] 
= '\0';
}

bool lessString(const string &l,const string &r)
ExpandedBlockStart.gifContractedBlock.gif
{
    
char l1[100],r1[100];
    ToLow(l,l1);
    ToLow(r,r1);
    
return strcmp(l1,r1)<0;
}

bool IsNumber(char str[])
ExpandedBlockStart.gifContractedBlock.gif
{
    
int len = strlen(str);
    
if(str[len-1]>='0' && str[len-1]<='9')
        
return true;
    
else
        
return false;
}

bool Input()
ExpandedBlockStart.gifContractedBlock.gif
{
    
char str[100];
    
int myoffset = 0;
    
bool exit=false;
    word.clear();
    number.clear();
    offset.clear();

    
while(!exit)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        cin
>>str;
        
if(str[0]=='.')
            
return false;
        
if(str[strlen(str)-1]=='.')
            exit 
= true;
        str[strlen(str)
-1= '\0';
        
if(IsNumber(str))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            number.push_back(atoi(str));
            offset.push_back(myoffset);
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string s = str;
            word.push_back(s);
        }

        
++myoffset;
    }

    sum 
= myoffset;
    
return true;
}

void Display()
ExpandedBlockStart.gifContractedBlock.gif
{
    
for(int i=0;i<sum;++i)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
if(!offset.empty() &&offset.front()==i)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            cout
<<number.front();
            number.pop_front();
            offset.pop_front();
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            cout
<<word.front();
            word.pop_front();
        }

        
if(i!=sum-1)
            cout
<<"";
        
else
            cout
<<"."<<endl;
    }

}

int main()
ExpandedBlockStart.gifContractedBlock.gif
{
    
while(Input())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        sort(word.begin(),word.end(),lessString);
        sort(number.begin(),number.end());

        Display();
    }

    
return 0;
}

转载于:https://www.cnblogs.com/jaskist/archive/2009/05/09/1453188.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值