读书人

数组定义位置不同引起的有关问题

发布时间: 2012-06-13 12:30:18 作者: rapoo

数组定义位置不同引起的问题
以下是uva 156题英文看不懂可以跳过有中文解释
Ananagrams

Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ.


Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE.


Write a program that will read in the dictionary of a restricted domain and determine the relative ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be ``rearranged'' at all. The dictionary will contain no more than 1000 words.


Input
Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken across lines. Spaces may appear freely around words, and at least one space separates multiple words on the same line. Note that words that contain the same letters but of differing case are considered to be anagrams of each other, thus tIeD and EdiT are anagrams. The file will be terminated by a line consisting of a single #.


Output
Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always be at least one relative ananagram.


Sample input

ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries
#


Sample output

Disk
NotE
derail
drIed
eye
ladder
soon

题意找出一段话中不重复的单词并且按字典序排序,排序区分大小写
所谓重复是所有字母一样,顺序不同无所谓,但不区分大小写。

我把用来标记的数组定义在堆区也就是主函数的外面就对了,
而定义在主函数内也就是栈区就runtime error了,
同一个数组定义的位置不同而已

奇怪()

以下是ac的过程序
求高手指教为什么把a数组定义在主函数里就runtime error


#include <stdio.h>
#include <string.h>
#define max 1001
char word[max][100],key[max][100],s[100],s1[100];
int a[max];[/color]
void change()
{
int l=strlen(s),i,j;
for (i=0;i<l;i++)
if (s[i]<='Z') s1[i]=s[i]+32;
else s1[i]=s[i];
for (i=0;i<l-1;i++)
for (j=i+1;j<l;j++)
if (s1[i]>s1[j]) {s1[l]=s1[i];s1[i]=s1[j];s1[j]=s1[l];}
s1[l]='\0';
};
int pk(char s1[],char s2[])
{int l1=strlen(s1),l2=strlen(s2),i;
l1=l1<l2 ? l1:l2;
for (i=0;i<l1;i++)
{if (s1[i]>s2[i]) return 1;
if (s1[i]<s2[i]) return 0;
}
};
void main ()
{int sum=0,i,j,f;
//定在这里就runtime error了
while (scanf("%s",&s))
{if (s[0]=='#') break;
change();
f=1;
for (i=1;i<=sum;i++)
if (strcmp(key[i],s1)==0) {f=0;a[i]=0;break;}
if (f) {++sum;a[sum]=1;strcpy(word[i],s);strcpy(key[i],s1);}
}
for (i=1;i<sum;i++)
for (j=i+1;j<=sum;j++)
if (pk(word[i],word[j]))
{strcpy(s,word[i]);strcpy(word[i],word[j]);strcpy(word[j],s);
a[0]=a[i];a[i]=a[j];a[j]=a[0];
}
for (i=1;i<=sum;i++)
if (a[i]) puts(word[i]);
}





[解决办法]
全局数据段和栈空间是不一样的
[解决办法]
建议你看看《C与指针》前三章,有对这块介绍的知识,在那里会找到答案。

读书人网 >C语言

热点推荐