为什么runtime error???

posa88 2010-03-30 08:25:54
#include<iostream>
#include<cstring>
#include<memory>
using namespace std;
int main()
{
int n;
cin>>n;
char pre[105][105];
int *preNum = new int[n];
for(int i = 0; i < n; i++)
preNum[i] = 1;
for(int i = 0; i < n; i++)
{
char *text = new char[105];
memset(text,'\0', sizeof(text));
cin>>text;
int j=0, k =0;
while(text[j] != '\0')
{
if(text[j] == '-')
{
j++;
continue;
}
switch(text[j++])
{
case'0': pre[i][k++] = '0';break;
case'1': pre[i][k++] = '1';break;
case'A':case'B':case'C':case'2': pre[i][k++] = '2';break;
case'D':case'E':case'F':case'3': pre[i][k++] = '3';break;
case'G':case'H':case'I':case'4': pre[i][k++] = '4';break;
case'J':case'K':case'L':case'5': pre[i][k++] = '5';break;
case'M':case'N':case'O':case'6': pre[i][k++] = '6';break;
case'P':case'R':case'S':case'7': pre[i][k++] = '7';break;
case'T':case'U':case'V':case'8': pre[i][k++] = '8';break;
case'W':case'X':case'Y':case'9': pre[i][k++] = '9';break;
default:break;
}
if(k == 3)
{
pre[i][k++] = '-';
}
}
pre[i][k] = '\0';
}

for(int i = 0; i < n-1;i++)
{
for(int j = i + 1; j < n;j++)
{
if(strcmp(pre[i], pre[j]) > 0)
{
char stmp[105];
strcpy(stmp, pre[i]);
strcpy(pre[i],pre[j]);
strcpy(pre[j], stmp);
}
}
}
for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
if(strcmp(pre[i],pre[j]) == 0)
{
preNum[j]++;
break;
}
}
}
bool flag = false;
for(int i = 0; i < n; i++)
{

if(preNum[i]>1)
{
flag = true;
cout<<pre[i]<<" "<<preNum[i]<<endl;
}
}
if (flag == false)
cout<<"No duplicates."<<endl;
return 0;
}

pku acm 1002题,runtime error了,自己测试没错。谁能帮我看看为什么?只剩35分了
...全文
191 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
posa88 2010-04-01
  • 打赏
  • 举报
回复
无语了。。。
posa88 2010-03-31
  • 打赏
  • 举报
回复
没人来解决。。。。。。。。。。。。。。
posa88 2010-03-31
  • 打赏
  • 举报
回复
求救中。。。。。。
lgz1989cn 2010-03-30
  • 打赏
  • 举报
回复
为什么vs2005不报错?!
我是新手哈
posa88 2010-03-30
  • 打赏
  • 举报
回复
开到1000*1000也是错阿(这里也有改成1000:char *text = new char[30];)
irenekay 2010-03-30
  • 打赏
  • 举报
回复
RUNTIME ERROR,一般都是数组越界,你把数组开大了试试看。
如果实在不行,那就得换种算法了
posa88 2010-03-30
  • 打赏
  • 举报
回复
而且我还全部改成300的大小了。。。所以不会是大小的问题
posa88 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ztz0223 的回复:]

你只分配了30个字节,估计acm的玩意给你测试的时候写越界了。用string吧,动态的。

#include<iostream>
#include<cstring>
#include<memory>
#include <string>
using namespace std;
int main()
{
int n;
cin>>n;
char pre[30][30];
s……
[/Quote]按楼上这样还是runtime error.....
附题:
487-3279
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 124546 Accepted: 21264

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
就呆在云上 2010-03-30
  • 打赏
  • 举报
回复
你只分配了30个字节,估计acm的玩意给你测试的时候写越界了。用string吧,动态的。

#include<iostream>
#include<cstring>
#include<memory>
#include <string>
using namespace std;
int main()
{
int n;
cin>>n;
char pre[30][30];
string text;
int *preNum = new int[n];
for(int i = 0; i < n; i++)
preNum[i] = 1;
for(int i = 0; i < n; i++)
{
cin>>text;
int j=0, k =0;
while(text[j] != '\0')
{
if(text[j] == '-')
{
j++;
continue;
}
switch(text[j++])
{
case'0': pre[i][k++] = '0';break;
case'1': pre[i][k++] = '1';break;
case'A':case'B':case'C':case'2': pre[i][k++] = '2';break;
case'D':case'E':case'F':case'3': pre[i][k++] = '3';break;
case'G':case'H':case'I':case'4': pre[i][k++] = '4';break;
case'J':case'K':case'L':case'5': pre[i][k++] = '5';break;
case'M':case'N':case'O':case'6': pre[i][k++] = '6';break;
case'P':case'R':case'S':case'7': pre[i][k++] = '7';break;
case'T':case'U':case'V':case'8': pre[i][k++] = '8';break;
case'W':case'X':case'Y':case'9': pre[i][k++] = '9';break;
default:break;
}
if(k == 3)
{
pre[i][k++] = '-';
}
}
pre[i][k] = '\0';

}

for(int i = 0; i < n-1;i++)
{
for(int j = i + 1; j < n;j++)
{
if(strcmp(pre[i], pre[j]) > 0)
{
char stmp[30];
strcpy(stmp, pre[i]);
strcpy(pre[i],pre[j]);
strcpy(pre[j], stmp);
}
}
}
for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
if(strcmp(pre[i],pre[j]) == 0)
{
preNum[j]++;
break;
}
}
}
bool flag = false;
for(int i = 0; i < n; i++)
{

if(preNum[i]>1)
{
flag = true;
cout<<pre[i]<<" "<<preNum[i]<<endl;
}
}
if (flag == false)
cout<<"No duplicates."<<endl;
delete [] preNum ;
return 0;
}
posa88 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 selooloo 的回复:]

memset(text,'\0', sizeof(text));
sizeof(text)值是4,这个地方应该是105吧

还有你只申请内存,没释放内存
[/Quote]
可是这样也错。。。。pku acm上报的,就只是runtime error。
#include<iostream>
#include<cstring>
#include<memory>
using namespace std;
int main()
{
int n;
cin>>n;
char pre[30][30];
int *preNum = new int[n];
for(int i = 0; i < n; i++)
preNum[i] = 1;
for(int i = 0; i < n; i++)
{
char *text = new char[30];
//memset(text,'\0', sizeof(*text));
cin>>text;
int j=0, k =0;
while(text[j] != '\0')
{
if(text[j] == '-')
{
j++;
continue;
}
switch(text[j++])
{
case'0': pre[i][k++] = '0';break;
case'1': pre[i][k++] = '1';break;
case'A':case'B':case'C':case'2': pre[i][k++] = '2';break;
case'D':case'E':case'F':case'3': pre[i][k++] = '3';break;
case'G':case'H':case'I':case'4': pre[i][k++] = '4';break;
case'J':case'K':case'L':case'5': pre[i][k++] = '5';break;
case'M':case'N':case'O':case'6': pre[i][k++] = '6';break;
case'P':case'R':case'S':case'7': pre[i][k++] = '7';break;
case'T':case'U':case'V':case'8': pre[i][k++] = '8';break;
case'W':case'X':case'Y':case'9': pre[i][k++] = '9';break;
default:break;
}
if(k == 3)
{
pre[i][k++] = '-';
}
}
pre[i][k] = '\0';
delete [] text;
}

for(int i = 0; i < n-1;i++)
{
for(int j = i + 1; j < n;j++)
{
if(strcmp(pre[i], pre[j]) > 0)
{
char stmp[30];
strcpy(stmp, pre[i]);
strcpy(pre[i],pre[j]);
strcpy(pre[j], stmp);
}
}
}
for(int i = 1; i < n; i++)
{
for(int j = 0; j < i; j++)
{
if(strcmp(pre[i],pre[j]) == 0)
{
preNum[j]++;
break;
}
}
}
bool flag = false;
for(int i = 0; i < n; i++)
{

if(preNum[i]>1)
{
flag = true;
cout<<pre[i]<<" "<<preNum[i]<<endl;
}
}
if (flag == false)
cout<<"No duplicates."<<endl;
delete [] preNum ;
return 0;
}
suanyuan 2010-03-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 selooloo 的回复:]
memset(text,'\0', sizeof(text));
sizeof(text)值是4,这个地方应该是105吧

还有你只申请内存,没释放内存
[/Quote]

高手
昵称很不好取 2010-03-30
  • 打赏
  • 举报
回复
2楼查挺细~~
昵称很不好取 2010-03-30
  • 打赏
  • 举报
回复
我这也没有runtime error
不过你的内存没有释放
selooloo 2010-03-30
  • 打赏
  • 举报
回复
memset(text,'\0', sizeof(text));
sizeof(text)值是4,这个地方应该是105吧

还有你只申请内存,没释放内存
ypb362148418 2010-03-30
  • 打赏
  • 举报
回复
什么编译器,报错的东西全部贴上来,建议用05,查查你的编译器设置,你自己测试没有错,那这个runtime error是哪里来的

64,654

社区成员

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

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