UVAoJ --401

UVAoJ –401
这是第一次写的代码,编写两个函数分别判断镜像和回文,然后就是两个结果的排列组合了,根据结果不同输出不同的判断,但是以下这段代码的结果却是WA。还在理解中

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

char buf[21];

int is_palindrome(char a[])
{
    int i,len=strlen(a);
    for(i=0;i<(len/2);i++)
        if(a[i]!=a[len-i-1])
            return 0;
    return 1;   
}
char func(char c)
{
    char string[36]="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
    char origin[36]="ABCDEFGHIGKLMNOPQRSTUVWXYZ123456789";
    int j;
    for(j=0;j<36;j++)
    {
        if(c==origin[j])
            break;
    }
    return string[j];
}
int is_mirror(char b[])
{

    int i,len=strlen(b);
    char temp;
    for(i=0;i<(len/2);i++)
        {
            temp=func(b[i]);
            if(temp==' '||temp!=b[len-i-1])
                return 0;
        }
    return 1;
}
int main()
{
    char buf[21];
    memset(buf,0,sizeof(buf));
    while(scanf("%s",&buf)!=EOF)
    {
        if(is_mirror(buf)&&is_palindrome(buf))

            printf("%s -- is a mirrored palindrome.\n",buf );

        else if ((!is_mirror(buf))&&is_palindrome(buf))

            printf("%s -- is a regular palindrome.\n",buf );

        else if ((!is_palindrome(buf))&&is_mirror(buf))

            printf("%s -- is a mirrored string.\n",buf);

        else printf("%s -- is not a palindrome.\n", buf);

        memset(buf,0,sizeof(buf));
    }
    return 0;

}

VERSION2

#pragma  warning(disable:4996)
#include <stdio.h>
#include <string.h>
//#define LOCAL

char buf[21];
int check_mid(char c)
{
    char midset[12]="AHIMOTUVWXY18";
    int i;
    for(i=0;i<12;i++)
        if(c==midset[i])
            return i;
    return i;
}
int is_palindrome(char a[])
{
    int i,len=strlen(a);
    for(i=0;i<(len/2);i++)
        if(a[i]!=a[len-i-1])
            return 0;
    return 1;   
}
char func(char c)
{
    char string[36]="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
    char origin[36]="ABCDEFGHIGKLMNOPQRSTUVWXYZ123456789";
    int j;
    for(j=0;j<36;j++)
    {
        if(c==origin[j])
            break;
    }
    return string[j];
}
int is_mirror(char b[])
{

    int i,len=strlen(b);
    char temp;
    for(i=0;i<(len/2);i++)
        {
            temp=func(b[i]);
            if(temp==' '||temp!=b[len-i-1])
                return 0;
        }
    if(len%2==1)
    {
        if(check_mid(b[i])==12)return 0;
    return 1;
    }
}

int main()
{

    #ifdef LOCAL

        freopen("data.in","r",stdin);
        freopen("data.out","w",stdout);
    #endif LOCAL

    char buf[21];
    int count=0;
    memset(buf,0,sizeof(buf));
    while(scanf("%s",&buf)!=EOF)
    {
        if(is_mirror(buf))count+=10;
        if(is_palindrome(buf))count+=20;
        if(count==30)

            printf("%s -- is a mirrored palindrome.\n",buf );

        else if (count==20)

            printf("%s -- is a regular palindrome.\n",buf );

        else if (count==10)

            printf("%s -- is a mirrored string.\n",buf);

        else printf("%s -- is not a palindrome.\n", buf);

        memset(buf,0,sizeof(buf));
        printf("\n");
        count=0;
    }
    return 0;

}

这个版本还是WA,我在测试mirror的时候已经考虑了中间的字符如果不是自对称的情况,但还是不能过,我还要继续思考,我发现UVAoj比较好的一个地方是,他提供相关题目的debug,自己可以编写一些输入进行测试。加油
今天我终于知道我的错误之处了,简直被自己蠢哭T^T.在字符串中在字符串origin里J携程G了。
以下是正确代码:

#pragma  warning(disable:4996)
#include <stdio.h>
#include <string.h>
//#define LOCAL

char buf[21];
int check_mid(char c)
{
    char midset[14]="AHIMOTUVWXY18";
    int i;
    for(i=0;i<14;i++)
        if(c==midset[i])
            return i;
    return i;
}
int is_palindrome(char a[])
{
    int i,len=strlen(a);
    for(i=0;i<(len/2);i++)
        if(a[i]!=a[len-i-1])
            return 0;
    return 1;   
}
char func(char c)
{
    char string[36]="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
    string[35]='\0';

    char origin[36]="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
    origin[35]='\0';
    int j;
    for(j=0;j<36;j++)
    {
        if(c==origin[j])
            break;
    }
    return string[j];
}
int is_mirror(char b[])
{

    int i,len=strlen(b);
    char temp;
    for(i=0;i<(len/2);i++)
        {
            temp=func(b[i]);
            if(temp==' '||temp!=b[len-i-1])
                return 0;
        }
    if(len%2==1)
    {
        if(check_mid(b[i])==14)return 0;

    }return 1;
}

int main()
{

    #ifdef LOCAL

        freopen("data.in","r",stdin);
        freopen("data.out","w",stdout);
    #endif LOCAL

    char buf[21];
    int count=0;
    memset(buf,0,sizeof(buf));
    while(scanf("%s",&buf)!=EOF)
    {
        if(is_mirror(buf))count+=10;
        if(is_palindrome(buf))count+=20;
        if(count==30)

            printf("%s -- is a mirrored palindrome.\n",buf );

        else if (count==20)

            printf("%s -- is a regular palindrome.\n",buf );

        else if (count==10)

            printf("%s -- is a mirrored string.\n",buf);

        else printf("%s -- is not a palindrome.\n", buf);

        memset(buf,0,sizeof(buf));
        printf("\n");
        count=0;
    }
    return 0;

}

终见AC,嗯。继续写下一题。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值