(北大ACM题目)1002--"487-3279" ___________runtime error

烟水暖 2011-07-23 08:24:11
问题的具体内容请在北大ACM的题库里可以找到……
下面的代码在提交之后出现“runtime error”
请问出现这类问题的一般原因是什么??????
请各位帮帮忙哈……O(∩_∩)O谢谢


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void changestring(char str[],int length)
{
int i,j;
for(i=0;i<length;i++)
{
//如果连续输入2个‘-’则出现错误
if(str[i]=='-')
{
j=i;
do
{
str[j]=str[j+1];
j++;
}while(str[j]);
}
if(str[i]>='A'&&str[i]<='C')
str[i]='2';
if(str[i]>='D'&&str[i]<='F')
str[i]='3';
if(str[i]>='G'&&str[i]<='I')
str[i]='4';
if(str[i]>='J'&&str[i]<='L')
str[i]='5';
if(str[i]>='M'&&str[i]<='O')
str[i]='6';
if(str[i]>='P'&&str[i]<='S')
str[i]='7';
if(str[i]>='T'&&str[i]<='V')
str[i]='8';
if(str[i]>='W'&&str[i]<='Y')
str[i]='9';
}
}

void main()
{
int n,i,j,k,length,count[100],flag=0;
char str[100][20],rank[]="0123456789";
//freopen("bb.txt","r",stdin);
while(scanf("%d",&n)!=EOF && n!=0)
{
//输入部分
getchar();
for(i=0;i<n;i++)
{
scanf("%s",&str[i]);
length=strlen(str[i]);
changestring(str[i],length);
}
for(i=0;i<n;i++)
{
count[i]=1;
for(j=i+1;j<n;j++)
{
if(!strcmp(str[i],str[j]))
{
count[i]++;
for(k=j;k<n-1;k++)
strcpy(str[k],str[k+1]);

n--;
j--;
}
}
}
for(i=0;i<n;i++)
{
if(count[i]!=1)
flag=1;
}
if(flag==0)
printf("No duplicates. \n");

/* for(i=0;i<10;i++)
for(j=0;j<n;j++)
if(count[j]>1 && rank[i]==str[j][0])
printf("%c%c%c%c%c%c%c%c %d\n",str[j][0],str[j][1],str[j][2],'-',str[j][3],str[j][4],str[j][5],str[j][6],count[j]);
*/
for(i=0;i<10;i++)
for(j=0;j<n;j++)
if(count[j]>1 && rank[i]==str[j][0])
{
for(k=0;k<3;k++)
printf("%c",str[j][k]);
printf("-");
for(k=3;k<strlen(str[j]);k++)
printf("%c",str[j][k]);
printf(" %d\n",count[j]);
}

}
}
...全文
383 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
烟水暖 2011-08-09
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 yuerzm 的回复:]

C/C++ code

if ( !strcmp(str[i],str[j]) )
count++;
//strcmp()函数是比较字符串是否相同。而你比较的是字符
[/Quote]

我定义的是char str[1000][8];那个是字符串的比较……
在那个转换后的字符串后面添加‘\0'就可以避免了……
谢谢了

LucEaspe 2011-08-08
  • 打赏
  • 举报
回复

if ( !strcmp(str[i],str[j]) )
count++;
//strcmp()函数是比较字符串是否相同。而你比较的是字符
紫回蓝 2011-07-23
  • 打赏
  • 举报
回复
哈哈,持续关注中
烟水暖 2011-07-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yuerzm 的回复:]
你的算法有问题。
[/Quote]
那个图片不知怎么弄出现,显示的还只是地址,下面是修改后的代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int compare( const void *a,const void *b)
{
return strcmp( (char *)a,(char *)b );
}
void main()
{
int n,i,j,k,flag,l;
char s[20];
char str[1000][8];
char rank[]="0123456789";
char into[27]="2223334445556667_77888999_";
freopen("bb.txt","r",stdin);
while(scanf("%d",&n)!=EOF && n!=0)
{
getchar();
for(i=0;i<n;i++)
{
gets(s);
l=strlen(s);
for(j=0,k=0;k<l;j++,k++)
{
if(s[k]>='0' && s[k]<='9')
str[i][j]=s[k];
else
if(s[k]>='A' && s[k]<='P'|| s[k]>='R'&&s[k]<='Y')
str[i][j]=into[s[k]-'A'];
else
j--;
}//整理输入
// printf("%s\n",str[i]);
}
qsort(str,n,8,compare);
flag=0;
for (i=0;i<n-1;)
{
int count=1;
for (j=i+1;j<n;j++)
{
if ( !strcmp(str[i],str[j]) )
count++;
else
break;
}
if ( count>=2 )
{
printf("%c%c%c%c%c%c%c%c %d\n",str[i][0],str[i][1],str[i][2],'-',str[3],str[4],str[5],str[6],count);
flag++;
}
i=j;
}
if( !flag )
printf("No duplicates. \n");
}
}
烟水暖 2011-07-23
  • 打赏
  • 举报
回复
http://ww4.sinaimg.cn/bmiddle/6c8c19bdgw1djg0hw7smmj.jpg
烟水暖 2011-07-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yuerzm 的回复:]
你的算法有问题。
[/Quote]
谢谢你的解答。
我没有学过c++,你的意思我明白,我套用了你的思路重新改写了,目前有个问题,就是输出部分,
if ( !strcmp(str[i],str[j]) )
count++;
这步没有执行count++;在调试的时候看到原本该是相同的两个字符串后面多了一些乱码
http://ww4.sinaimg.cn/bmiddle/6c8c19bdgw1djg0hw7smmj.jpg

这类问题我经常遇到,有些调整代码就可以消除,但是不清楚具体原因。了解到那些乱码是空格来的……请问这是为什么呢
勤奋的小游侠 2011-07-23
  • 打赏
  • 举报
回复
你连问题都不想贴出来,还“请”别人去题库找,我还没见过像你这种人的!!!
虽然我知道出这种错原因的答案,但我不想告诉你!
烟水暖 2011-07-23
  • 打赏
  • 举报
回复
不懂……
bruceteen 2011-07-23
  • 打赏
  • 举报
回复
"问题的具体内容请在北大ACM的题库里可以找到……"
------ 我觉得你也别在CSDN里求答案了,死后见到上帝时,上帝自然会给你解答。
LucEaspe 2011-07-23
  • 打赏
  • 举报
回复
你的算法有问题。
LucEaspe 2011-07-23
  • 打赏
  • 举报
回复
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int compare( const void *a,const void *b)
{
return strcmp( (char *)a,(char *)b );
}

char str[100000][8];
char into[27]="22233344455566677778889999";

int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n,i,j,k;
char a[20];
cin>>n;
for (i=0;i<n;i++)
{
cin>>a;
for ( k=j=0;j<strlen(a);j++,k++ )
{
if ( a[j]>='0' && a[j]<='9' )
str[i][k]=a[j];
else if ( a[j]>='A' && a[j]<='Z' )
str[i][k]=into[ a[j]-'A' ];
else
k--;
}
}
qsort(str,n,8,compare);
int t=0;
for (i=0;i<n-1;)
{
int count=1;
for (j=i+1;j<n;j++)
{
if ( !strcmp(str[i],str[j]) )
count++;
else
break;
}
if ( count>=2 )
{
cout<<str[i][0] <<str[i][1] <<str[i][2] <<"-"<< str[i]+3<<" " <<count <<endl;
t++;
}
i=j;
}
if( !t )
cout<<"No duplicates."<<endl;
return 0;
}

银蝈蝈 2011-07-23
  • 打赏
  • 举报
回复
用快排吧~~ 应该可以过

我的只用了这么些时间:
Memory: 1348K Time: 829MS
Language: C++ Result: Accepted
zhangxfeng112 2011-07-23
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 menghan2010 的回复:]
额……我只是觉得问题的内容很长,贴上去人家会看得烦……既然你这样认为,我也没办法……
[/Quote]
看到 连问题都懒得贴的人更烦。
还懒得google去找问题。
更懒得分析回答LZ提出的问题。

吃饱了撑的
烟水暖 2011-07-23
  • 打赏
  • 举报
回复
请各位原谅了……问题如下:
487-3279
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 165092 Accepted: 28193
Description

Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:

A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9

There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.

Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)

Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.

Input

The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.
Output

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.
Sample Input

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
Sample Output

310-1010 2
487-3279 4
888-4567 3
烟水暖 2011-07-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lovesmiles 的回复:]

你连问题都不想贴出来,还“请”别人去题库找,我还没见过像你这种人的!!!
虽然我知道出这种错原因的答案,但我不想告诉你!
[/Quote]
额……我只是觉得问题的内容很长,贴上去人家会看得烦……既然你这样认为,我也没办法……
zhangxfeng112 2011-07-23
  • 打赏
  • 举报
回复
靠,还有这样请教问题的。

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧