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;
}        


你可能感兴趣的:(c,struct,语言)