Chinaunix首页 | 论坛 | 博客
  • 博客访问: 474257
  • 博文数量: 117
  • 博客积分: 3195
  • 博客等级: 中校
  • 技术积分: 1156
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-04 01:44
文章分类

全部博文(117)

文章存档

2012年(5)

2011年(5)

2010年(46)

2009年(61)

我的朋友

分类:

2009-10-11 21:53:58

解题思路

 

题意:

    比较两个串,遇到数字的话根据数字大小来比较。

1,            所有的小写字母看成大写字母;

2,            如果有+或者-,且前面是非数字的字符,后面是数字,那么把+- 看成是后面那个数字的一部分,就是正几或者负几,而如果前面后面都是数字,那么就看成一般的符号,

3,            其他的就根据他们的ASCII码来比较大小。

 

思路:

有点麻烦的模拟题,我是先把里面的可以看成是数字的一部分的+-0都去掉。如果是’ - ’,就在那位标记一下。然后就一个一个相比较,是字符的话直接比大小,是数字的话把它们改成整型数(int就够了)再比大小。

PS:本来以为过不了的,因为我把剩下的两个串看成是一样长的了,两个串的字符数一样,数字个数也一样,所以万一遇到又不一样的就肯定wa了。后来一试竟然出乎意料地返回一个yes,刷到第一了,那个激动啊~

还好那时把题意看明白了,把数据形式也猜对了。那些没过的队就是因为题意没明白或者想到可能出现字符跟数字比较这种情况把题目搞复杂了才没yes

又一次证明了英语好才是硬道理!

源程序

 

#include <stdio.h>
#include <string.h>
#include <ctype.h>
//#include

#define N 1006

int main()
{
    int i, j, k, kk, jj, p;
    int t, len2, len1, flag, m, num1, num2, maxlen;
    int mark1[N], mark2[N];
    char ch1[N], ch2[N];
    //freopen("in.txt", "r", stdin);

    scanf("%d", &t);
    getchar();
    for(i=1; i<=t; i++)
    {
        scanf("%s", ch1);
        scanf("%s", ch2);
        len1 = strlen(ch1);
        len2 = strlen(ch2);

        memset(mark1, 0, sizeof(mark1));
        memset(mark2, 0, sizeof(mark2));

        //去掉那些没用的字符跟0

        for(j=0; j<=len1; j++)
        {
            if((j == 0 || !isdigit(ch1[j-1])) && ch1[j] == '+' && isdigit(ch1[j+1]))
                ch1[j] = '0';
            if((j == 0 || !isdigit(ch1[j-1])) && ch1[j] == '-' && isdigit(ch1[j+1]))
            {
                mark1[j] = 1;
                ch1[j] = '0';
            }
            if((j == 0 || !isdigit(ch1[j-1])) && ch1[j] == '0')
            {
                while(ch1[j] == '0')
                {
                    for(k=j+1; k<=len1; k++)
                    {
                        ch1[k-1] = ch1[k];
                    }
                }
            }
        }
        for(j=0; j<=len2; j++)
        {
            if((j == 0 || !isdigit(ch2[j-1])) && ch2[j] == '+' && isdigit(ch2[j+1]))
                ch2[j] = '0';
            if((j == 0 || !isdigit(ch2[j-1])) && ch2[j] == '-' && isdigit(ch2[j+1]))
            {
                mark2[j] = 1;
                ch2[j] = '0';            
            }
            if((j == 0 || !isdigit(ch2[j-1])) && ch2[j] == '0')
            {
                while(ch2[j] == '0')
                {
                    for(k=j+1; k<=len2; k++)
                    {
                        ch2[k-1] = ch2[k];
                    }
                }
            }
        }
        for(j=0; j<=len1; j++)
        {
            ch1[j] = toupper(ch1[j]);

        }

        for(j=0; j<=len2; j++)
        {
            ch2[j] = toupper(ch2[j]);
            
        }
        flag = 0;
        if(strcmp(ch1, ch2) != 0)
        {
            len1 = strlen(ch1);
            len2 = strlen(ch2);
            for(j=0; j<=len1; j++)
            {
                if(!isdigit(ch1[j])) //处理一般字符

                {
                    if(ch1[j] > ch2[j])
                    {
                        flag = 1;
                        break;
                    }
                    if(ch1[j] < ch2[j])
                    {
                        flag = -1;
                        break;
                    }
                }
                else
                {
                    k = j;
                    num1 = 0;
                    m = 1;
                    while(isdigit(ch1[k])) //处理数字

                    {
                        num1 = num1 * m + ch1[k] - '0';
                        m = 10;
                        k++;
                    }
                    if(mark1[j])
                    {
                        num1 *= -1;
                    }
                    j = k;
                    k = j;
                    num2 = 0;
                    m = 1;
                    while(isdigit(ch2[k]))
                    {
                        num2 = num2 * m + ch2[k] - '0';
                        m = 10;
                        k++;
                    }
                    if(mark2[j])
                    {
                        num2 *= -1;
                    }
                    if(num1 > num2)
                    {
                        flag = 1;
                        break;
                    }
                    if(num1 < num2)
                    {
                        flag = -1;
                        break;
                    }
                }
            }
        }
        printf("%d %d", i, flag);

        printf("\n");
    }
//    getch();

    return 0;
}


阅读(2139) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~