NYOJ 540 奇怪的排序(字符串)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=540

很简单的字符串处理题,因为不懂itoa是非标准C语言库函数,CompileError了一次。后来改成sprintf,就过了。

水题,直接上代码。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char *strrev(char *str)/*字符串转置*/
{
    int len=strlen(str);
    char *begin=str;
    char *end=str+len-1;
    while(begin<end)
    {
        char temp;
        temp=*begin;
        *begin=*end;
        *end=temp;
        ++begin;
        --end;
    }
    return str;
}
struct node
{
    int firstnum;
    int lastnum;
}w[100001];
bool comp(node x,node y)
{
    if(x.lastnum<y.lastnum) return true;
    return false;
}
char z[100001];
int main()
{
    int ncases,x,y;
    scanf("%d",&ncases);
    while(ncases--)
    {
        int cnt=0;
        scanf("%d %d",&x,&y);
        for(int i=x;i<=y;i++)
        {
            w[cnt].firstnum=i;
            sprintf(z,"%d",i);/*将int转化为char型并保存在z数组中*/
            strrev(z);/*转置*/
            w[cnt].lastnum=atoi(z);/*再将char型从新转化为int,并用结构体保存*/
            cnt++;
            memset(z,0,sizeof(z));/*最后不要忘了用一次就将数组清零一次*/
        }
        sort(w,w+y-x+1,comp);
        for(int i=0;i<y-x+1;i++)
        {
            if(i==0)
            {
                printf("%d",w[0].firstnum);
            }
            else
            {
                printf(" %d",w[i].firstnum);
            }
        }
        printf("\n");
    }
    return 0;
}        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值