poj1002 总是Runtinme error,望各位高手帮忙解决一下

源码面前了无秘密 2011-07-25 12:16:59
#include<iostream>
#include<string>
using namespace std;
char ch[]="2223334445556667777888999";
char str[100],telephones[10000][9];
int sortchar(int j)
{
int i=-1,k=-1;
while(k<8)
{
i++;
if(str[i]=='-') continue;
k++;
if(k==3)
{
telephones[j][k]='-';
k++;
}
if(str[i]>='A' && str[i]<='Z')
{
telephones[j][k]=ch[str[i]-'A'];
continue;
}
telephones[j][k]=str[i];
}
telephones[j][k]='\0';
return 0;
}

int main()
{
int i,j=0,n,k,w=0;
char a[8];
cin>>n;
while(j<n)
{
cin>>str;
sortchar(j);
j++;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
w=0;
for(k=0;k<9;k++)
{
if(telephones[i][k]<telephones[j][k]) break;
if(telephones[i][k]>telephones[j][k])
{
w=1;
break;
}
}
if(w==1)
{
for(w=0;w<8;w++)
{
a[w]=telephones[i][w];
telephones[i][w]=telephones[j][w];
telephones[j][w]=a[w];
}
}
}
}
w=0;
for(i=0;i<n;)
{
j=i;
i++;
while(i<n && strcmp(telephones[j],telephones[i])==0) i++;
if(i-j>1)
{
cout<<telephones[j]<<" "<<i-j<<endl;
w=1;
}
}
if(w==0) cout<<"No duplicates.";
return 0;
}

...全文
83 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

结帖啦。。。。
至善者善之敌 2011-07-25
  • 打赏
  • 举报
回复
输入啥数报错!!?
  • 打赏
  • 举报
回复
问题解决了,多谢大家
  • 打赏
  • 举报
回复

题意为:对输入的带字符的电话号码转换为数字电话号码,注意可包含“-”符号详细的意思poj上有汉语翻译可看
#2楼回答有道理,但我之前用qsort函数报runing answer,有关qsort代码如下:

int cmp(const void*a,const void*b)
{
return(strcmp((char*)a,(char*)b));
}
将排序部分换为:
qsort(telephones,n,9,cmp);
帮忙指正 多谢
AndyZhang 2011-07-25
  • 打赏
  • 举报
回复
解释一下题目意思,懒得去看了
logiciel 2011-07-25
  • 打赏
  • 举报
回复
根据题意:
The first line of the input specifies the number of telephone numbers in the directory (up to 100,000)

char str[100],telephones[10000][9];
telephones[10000]不够大,所以Runtinme error。要改为
char str[100],telephones[100000][9];

但你的排序效率低,将出现超时。要改为用qsort。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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