北大POJ 1002 在本地VS2010上运行结果没问题,为什么提交时会提示Runtime Error? 代码如下:
#include<iostream>usingnamespacestd;#include<string>#definemax1000stringletter_to_num...
#include<iostream>
using namespace std;
#include<string>
#define max 1000
string letter_to_num(string s){ //将字母字符串转换成数字字符串
for(unsigned int i=0;i<s.length();i++){
if(s[i]=='A'||s[i]=='B'||s[i]=='C')
s[i]='2';
else if(s[i]=='D'||s[i]=='E'||s[i]=='F')
s[i]='3';
else if(s[i]=='G'||s[i]=='H'||s[i]=='I')
s[i]='4';
else if(s[i]=='J'||s[i]=='K'||s[i]=='L')
s[i]='5';
else if(s[i]=='M'||s[i]=='N'||s[i]=='O')
s[i]='6';
else if(s[i]=='P'||s[i]=='R'||s[i]=='S')
s[i]='7';
else if(s[i]=='T'||s[i]=='U'||s[i]=='V')
s[i]='8';
else if(s[i]=='W'||s[i]=='X'||s[i]=='Y')
s[i]='9';
}
return s;
}
string changestr(string s){ //将数字字符串转换成标准形式
unsigned int i=0;
while(i<s.length()){
if(s[i]=='-'){
s.erase(i,1);
}
else
i++;
}
s.insert(3,"-");
return s;
}
int main(){
int input,*tag,r[max],finalnum[max];
string str[max];
bool result=false;
string output[max],final[max];
cin>>input;
tag=(int*)malloc(input*sizeof(int));
memset(tag,0,input*sizeof(int));
memset(r,0,input*sizeof(int));
for(int i=0;i<input;i++){ //输入,并转化为标准形式
cin>>str[i];
str[i]=letter_to_num(str[i]);
str[i]=changestr(str[i]);
}
for(int i=0;i<input;i++){ //将存在重复的字符串放入output[i]中,将重复的数目放入r[i]中
if(tag[i]!=1){
for(int j=i+1;j<input;j++){
if(tag[j]!=1){
if(str[i]==str[j]){
tag[i]=1;
tag[j]=1;
r[i]++;
output[i].assign(str[i]);
}
}
}
}
}
for(int i=0;i<input;i++){ //判断是否有结果输出
if(r[i]>0){
result=true;
break;
}
}
int l=0;
for(int i=0;i<input;i++){ //计算输出结果的数量l,同时将结果重新赋值到final和finalnum中
if(r[i]>0)
{
final[l].assign(output[i]);
finalnum[l]=r[i];
l++;
}
}
string tmp;
int temp;
for(int i=0;i<l;i++){ //按字典序排列
for(int j=i+1;j<l;j++){
if(final[i]>final[j]){
tmp=final[i];
final[i]=final[j];
final[j]=tmp;
temp=finalnum[i];
finalnum[i]=finalnum[j];
finalnum[j]=temp;
}
}
}
if(result){ //输出结果
for(int i=0;i<l;i++)
cout<<final[i]<<" "<<finalnum[i]+1<<endl;
}
else
cout<<"No duplicates."<<endl;
system("pause");
return 0;
} 展开
using namespace std;
#include<string>
#define max 1000
string letter_to_num(string s){ //将字母字符串转换成数字字符串
for(unsigned int i=0;i<s.length();i++){
if(s[i]=='A'||s[i]=='B'||s[i]=='C')
s[i]='2';
else if(s[i]=='D'||s[i]=='E'||s[i]=='F')
s[i]='3';
else if(s[i]=='G'||s[i]=='H'||s[i]=='I')
s[i]='4';
else if(s[i]=='J'||s[i]=='K'||s[i]=='L')
s[i]='5';
else if(s[i]=='M'||s[i]=='N'||s[i]=='O')
s[i]='6';
else if(s[i]=='P'||s[i]=='R'||s[i]=='S')
s[i]='7';
else if(s[i]=='T'||s[i]=='U'||s[i]=='V')
s[i]='8';
else if(s[i]=='W'||s[i]=='X'||s[i]=='Y')
s[i]='9';
}
return s;
}
string changestr(string s){ //将数字字符串转换成标准形式
unsigned int i=0;
while(i<s.length()){
if(s[i]=='-'){
s.erase(i,1);
}
else
i++;
}
s.insert(3,"-");
return s;
}
int main(){
int input,*tag,r[max],finalnum[max];
string str[max];
bool result=false;
string output[max],final[max];
cin>>input;
tag=(int*)malloc(input*sizeof(int));
memset(tag,0,input*sizeof(int));
memset(r,0,input*sizeof(int));
for(int i=0;i<input;i++){ //输入,并转化为标准形式
cin>>str[i];
str[i]=letter_to_num(str[i]);
str[i]=changestr(str[i]);
}
for(int i=0;i<input;i++){ //将存在重复的字符串放入output[i]中,将重复的数目放入r[i]中
if(tag[i]!=1){
for(int j=i+1;j<input;j++){
if(tag[j]!=1){
if(str[i]==str[j]){
tag[i]=1;
tag[j]=1;
r[i]++;
output[i].assign(str[i]);
}
}
}
}
}
for(int i=0;i<input;i++){ //判断是否有结果输出
if(r[i]>0){
result=true;
break;
}
}
int l=0;
for(int i=0;i<input;i++){ //计算输出结果的数量l,同时将结果重新赋值到final和finalnum中
if(r[i]>0)
{
final[l].assign(output[i]);
finalnum[l]=r[i];
l++;
}
}
string tmp;
int temp;
for(int i=0;i<l;i++){ //按字典序排列
for(int j=i+1;j<l;j++){
if(final[i]>final[j]){
tmp=final[i];
final[i]=final[j];
final[j]=tmp;
temp=finalnum[i];
finalnum[i]=finalnum[j];
finalnum[j]=temp;
}
}
}
if(result){ //输出结果
for(int i=0;i<l;i++)
cout<<final[i]<<" "<<finalnum[i]+1<<endl;
}
else
cout<<"No duplicates."<<endl;
system("pause");
return 0;
} 展开
2个回答
展开全部
在本地运行结果没问题,什么也不能说明.
你这代码太过冗杂。看看这个代码
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // STL sort function
using namespace std;
char map[] = "2223334445556667#77888999#";
//ABCDEFGHIJKLMNOPQRSTUVWXYZ
void visited(char &ch) // visit and format strings
{
if (ch >= 'A' && ch <= 'Z')
ch=map[ch-'A']; // ch equals to its real number
}
int main()
{
int N,i=0,j,flag=0;
string s;
vector<string> stored(100000); // be visited & stored (up to 100,000)
cin>>N;
vector<int> counter(N,1); // stored times
for (; i<N; i++)
{
cin>>s;
for (j=0; j<s.length(); j++) // MSDN
{
visited(s[j]);
if (s[j]!='-')
{
stored[i] += s[j];
if (stored[i].length()==3)
stored[i] += '-'; // 487 -[3] 3279
}
}
}
sort(stored.begin(),stored.begin()+N); // Quicker than QuickSort!
// should not used stored.end() !
i=0; j=1;
while (i<N)
{
while(stored[i] == stored[j])
{
counter[i]++;
j++;
flag=1;
}
i=j;
j++;
}
if (flag)
for (i=0; i<N; i++)
{
if (counter[i]>1)
cout<<stored[i]<<" "<<counter[i]<<endl;
} // must have { }
else cout<<"No duplicates."<<endl;
return 0;
}
希望对你有些帮助。
你这代码太过冗杂。看看这个代码
#include <iostream>
#include <string>
#include <vector>
#include <algorithm> // STL sort function
using namespace std;
char map[] = "2223334445556667#77888999#";
//ABCDEFGHIJKLMNOPQRSTUVWXYZ
void visited(char &ch) // visit and format strings
{
if (ch >= 'A' && ch <= 'Z')
ch=map[ch-'A']; // ch equals to its real number
}
int main()
{
int N,i=0,j,flag=0;
string s;
vector<string> stored(100000); // be visited & stored (up to 100,000)
cin>>N;
vector<int> counter(N,1); // stored times
for (; i<N; i++)
{
cin>>s;
for (j=0; j<s.length(); j++) // MSDN
{
visited(s[j]);
if (s[j]!='-')
{
stored[i] += s[j];
if (stored[i].length()==3)
stored[i] += '-'; // 487 -[3] 3279
}
}
}
sort(stored.begin(),stored.begin()+N); // Quicker than QuickSort!
// should not used stored.end() !
i=0; j=1;
while (i<N)
{
while(stored[i] == stored[j])
{
counter[i]++;
j++;
flag=1;
}
i=j;
j++;
}
if (flag)
for (i=0; i<N; i++)
{
if (counter[i]>1)
cout<<stored[i]<<" "<<counter[i]<<endl;
} // must have { }
else cout<<"No duplicates."<<endl;
return 0;
}
希望对你有些帮助。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询