当前位置: 代码迷 >> C语言 >> 关于“水王争霸赛”的问题
  详细解决方案

关于“水王争霸赛”的问题

热度:382   发布时间:2008-03-22 11:28:32.0
关于“水王争霸赛”的问题
我自己编写了一个程序,在TC上运行很正常,而且结果也是正确的,可是到了VC及gcc-3.3上后,就不能运行了,实在郁闷!~~~(报错是无效内存引用)

我实在是不知道什么地方弄了个无效内存出来,所以想请大家帮着我一块儿找找看……


  程序如下:
#include<stdio.h>
#include<string.h>
int main()
{
    int sum,a,b,c,lenth[1500];
    char ID[1500][40],tiezi[1500][15000],zhuan[15000],m1[15000],m2[15000],IDzhuan[40];

    memset(ID,'\x0',sizeof(ID));
    memset(tiezi,'\x0',sizeof(tiezi));
    memset(lenth,0,sizeof(lenth));
    memset(zhuan,'\x0',sizeof(zhuan));

    scanf("%d",&sum);
    getchar();
    for(a=0;a<sum;a++)   
    {
        gets(ID[a]);
        gets(tiezi[a]);
        lenth[a]=strlen(tiezi[a]);
    }

    for(a=0;a<sum;a++)
        for(b=0;b<sum-1;b++)      
        {
            if(lenth[b]<lenth[b+1])
            {
                strcpy(IDzhuan,ID[b]);
                strcpy(ID[b],ID[b+1]);
                strcpy(ID[b+1],IDzhuan);
                memset(IDzhuan,'\x0',sizeof(IDzhuan));
                strcpy(zhuan,tiezi[b]);
                strcpy(tiezi[b],tiezi[b+1]);
                strcpy(tiezi[b+1],zhuan);
                memset(zhuan,'\x0',sizeof(zhuan));
                c=lenth[b];
                lenth[b]=lenth[b+1];
                lenth[b+1]=c;
            }

            else if(lenth[b]==lenth[b+1])
            {
                strcpy(m1,tiezi[b]);
                strcpy(m2,tiezi[b+1]);

                if(strcmp(m1,m2)<0)
                {
                strcpy(IDzhuan,ID[b]);
                strcpy(ID[b],ID[b+1]);
                strcpy(ID[b+1],IDzhuan);
                memset(IDzhuan,'\x0',sizeof(IDzhuan));
                strcpy(zhuan,tiezi[b]);
                strcpy(tiezi[b],tiezi[b+1]);
                strcpy(tiezi[b+1],zhuan);
                memset(zhuan,'\x0',sizeof(zhuan));
                c=lenth[b];
                lenth[b]=lenth[b+1];
                lenth[b+1]=c;
                }

                else if(strcmp(m1,m2)==0)
                {
                     strcpy(m1,ID[b]);
                     strcpy(m2,ID[b+1]);

                    if(strcmp(m1,m2)<0)
                    {
                strcpy(IDzhuan,ID[b]);
                strcpy(ID[b],ID[b+1]);
                strcpy(ID[b+1],IDzhuan);
                memset(IDzhuan,'\x0',sizeof(IDzhuan));
                strcpy(zhuan,tiezi[b]);
                strcpy(tiezi[b],tiezi[b+1]);
                strcpy(tiezi[b+1],zhuan);
                memset(zhuan,'\x0',sizeof(zhuan));
                c=lenth[b];
                lenth[b]=lenth[b+1];
                lenth[b+1]=c;
                    }
                }
            }
        }

    for(a=0;a<sum;a++)        
        printf("%s\n",ID[a]);

}

在线等待……
搜索更多相关的解决方案: 水王争霸赛  内存  memset  sizeof  int  

----------------解决方案--------------------------------------------------------
  相关解决方案