c++ 北大acm1002题 总是runtime erro,高手麻烦看一下
原题地址http://acm.pku.edu.cn/JudgeOnline/problem?id=1002我的代码先集中输入,同时转换成标准格式,然后快排,字符串比较。最...
原题地址http://acm.pku.edu.cn/JudgeOnline/problem?id=1002
我的代码
先集中输入,同时转换成标准格式,
然后快排,字符串比较。
最后输出,总是runtime error ,高手帮忙看一下是哪里的问题,谢谢了。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
char *swp="22233344455566677778889999";
char telnum[30];
char standard_no[100010][10];
void translate_tel(char* tel_input,char* standard_no)
{
int j = 0;
for (int i = 0;i < tel_input[i] != '\0';i++)
{
if (3 == j)
{
standard_no[j] = '-';
j++;
//continue;
}
if (tel_input[i] == '-')
{
continue;
}
if (tel_input[i] >= 'A' && tel_input[i] < 'Z')
{
int no_index = tel_input[i] - 'A';
standard_no[j] = swp[no_index];
j++;
continue;
}
//if (tel_input[i] >= '0' && tel_input[i] <= '9')
//{
// j++;
// continue;
//}
standard_no[j] = tel_input[i];
j++;
}
standard_no[j] = '\0';
return;
}
int comp(const void* leftnum,const void* rightnum)
{
return strcmp((char*)leftnum,(char*)rightnum);
}
int main()
{
int num_of_tel = 0;
cin>>num_of_tel;
for (int i = 0; i < num_of_tel ; i++)
{
cin>>telnum;
//scanf("%s",telnum);
translate_tel(telnum,standard_no[i]);
}
qsort(standard_no,num_of_tel,10,comp);
bool NoDuplicated = true;
int DupTimes = 1;
for (int i = 1; i < num_of_tel; i++)
{
if (strcmp(standard_no[i],standard_no[i-1]) == 0)
{
DupTimes++;
NoDuplicated = false;
}
else
{
if (DupTimes != 1)
{
cout<<standard_no[i-1]<<" "<<DupTimes<<endl;
//printf("%s %d\n",standard_no[i-1],DupTimes);
DupTimes = 1;
}
}
}
if (DupTimes != 1)
{
cout<<standard_no[num_of_tel-1]<<" "<<DupTimes<<endl;
//printf("%s %d\n",standard_no[num_of_tel-1],DupTimes);
NoDuplicated = false;
}
if (NoDuplicated)
{
cout<<"No duplicates."<<endl;
//printf("No duplicates.");
}
return 0;
} 展开
我的代码
先集中输入,同时转换成标准格式,
然后快排,字符串比较。
最后输出,总是runtime error ,高手帮忙看一下是哪里的问题,谢谢了。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
char *swp="22233344455566677778889999";
char telnum[30];
char standard_no[100010][10];
void translate_tel(char* tel_input,char* standard_no)
{
int j = 0;
for (int i = 0;i < tel_input[i] != '\0';i++)
{
if (3 == j)
{
standard_no[j] = '-';
j++;
//continue;
}
if (tel_input[i] == '-')
{
continue;
}
if (tel_input[i] >= 'A' && tel_input[i] < 'Z')
{
int no_index = tel_input[i] - 'A';
standard_no[j] = swp[no_index];
j++;
continue;
}
//if (tel_input[i] >= '0' && tel_input[i] <= '9')
//{
// j++;
// continue;
//}
standard_no[j] = tel_input[i];
j++;
}
standard_no[j] = '\0';
return;
}
int comp(const void* leftnum,const void* rightnum)
{
return strcmp((char*)leftnum,(char*)rightnum);
}
int main()
{
int num_of_tel = 0;
cin>>num_of_tel;
for (int i = 0; i < num_of_tel ; i++)
{
cin>>telnum;
//scanf("%s",telnum);
translate_tel(telnum,standard_no[i]);
}
qsort(standard_no,num_of_tel,10,comp);
bool NoDuplicated = true;
int DupTimes = 1;
for (int i = 1; i < num_of_tel; i++)
{
if (strcmp(standard_no[i],standard_no[i-1]) == 0)
{
DupTimes++;
NoDuplicated = false;
}
else
{
if (DupTimes != 1)
{
cout<<standard_no[i-1]<<" "<<DupTimes<<endl;
//printf("%s %d\n",standard_no[i-1],DupTimes);
DupTimes = 1;
}
}
}
if (DupTimes != 1)
{
cout<<standard_no[num_of_tel-1]<<" "<<DupTimes<<endl;
//printf("%s %d\n",standard_no[num_of_tel-1],DupTimes);
NoDuplicated = false;
}
if (NoDuplicated)
{
cout<<"No duplicates."<<endl;
//printf("No duplicates.");
}
return 0;
} 展开
2个回答
展开全部
for (int i = 0;i < tel_input[i] != '\0';i++) 虽然没错,但是语义含糊不清,要写成这样for (int i = 0;tel_input[i] != '\0';i++)
char standard_no[100010][10]; 是二维数组,你再void translate_tel(char* tel_input,char* standard_no) 中是按一维数组用的,所以不对
qsort(standard_no,num_of_tel,10,comp); 这么写也是不对的,第三个参数应该是sizeof(char*)
你在看看吧,应该还有些问题。
char standard_no[100010][10]; 是二维数组,你再void translate_tel(char* tel_input,char* standard_no) 中是按一维数组用的,所以不对
qsort(standard_no,num_of_tel,10,comp); 这么写也是不对的,第三个参数应该是sizeof(char*)
你在看看吧,应该还有些问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询