这两个程序在输出结果上难道有什么不同吗?

来源:百度知道 编辑:UC知道 时间:2024/06/04 21:34:42
这是一个ACM的题目
http://acm.hdu.edu.cn/showproblem.php?pid=2025
我用这个程序可以通过
/* Note:Your choice is C IDE */
#include "stdio.h"
#include <string.h>
int main()
{char s[100],c[5]="(max)",max; int i,k,j,t;
while(scanf("%s",s)!=EOF)
{ max=s[0];
t=strlen(s)-1;
for(i=0;i<=t;i++)
if(max<s[i])
max=s[i];
for(i=0;i<=t;i++)
if(max==s[i])
printf("%c(max)",s[i]);
else
printf("%c",s[i]);
printf("\n");
}
return 0;
}

用这个却显示wrong answer 但是就输出结果而言的话我实在没看出它们有什么不同啊:
/* Note:Your choice is C IDE */
#include "stdio.h"
#include <string.h>
int main()
{char s[666],c[5]="(max)",max; int i,k,j,t;
while(scanf("%s",s)!=EOF)
{ m

下面的ac了 应该是你把字符串后移的时候没有吧'\0'也已过去 在输出前又没加'\0',而且 c[5]="(max)"也错"(max)" 五个字符再加'\0'是六个应该是c[6]
#include "stdio.h"
#include <string.h>
int main()
{
char s[666],c[6]="(max)",max; int i,k,j,t;//修改2
while(scanf("%s",s)!=EOF)
{
max=s[0];
t=strlen(s)-1;
for(i=0;i<=t;i++)
if(max<s[i])
max=s[i];
for(i=0;i<=t;i++)
if(max==s[i])
{
for(t+=5,j=t+1;j>i+5;j--) //修改1把'\0'也移动
s[j]=s[j-5];
for(k=1;k<=5;k++)
s[i+k]=c[k-1];
i+=5;
}
puts(s);//把memset删了 没用了
}
return 0;
}