您的位置:首页 > 其它

POJ 1002

2014-09-18 13:39 155 查看
一种比较简单的方法,把输入字符串转换成数字,然后排序,相同的相邻排列,然后检查重复,并输出即可。

注意的问题是输出时,有可能010-0001,这种情况不能用除法或者取模输出。

AC代码,时间很慢,打算再尝试其他方法:

/*
POJ 1002
TIME 1422MS
MEMORY 676K
*/
#include <fstream>
#include <iostream>
#include <string>
#include <strstream>
using namespace std;

int trans(char c)
{
if(c >= 'A' && c <='C')
return 2;
if(c >= 'D' && c <='F')
return 3;
if(c >= 'G' && c <='I')
return 4;
if(c >= 'J' && c <='L')
return 5;
if(c >= 'M' && c <='O')
return 6;
if(c >= 'P' && c <='S')
return 7;
if(c >= 'T' && c <='V')
return 8;
if(c >= 'W' && c <='Y')
return 9;
}
int cmp(const void*a,const void*b)
{
return *(int*)a-*(int*)b;
}
int main()
{
ifstream file("1002.txt");
long number[100001];
memset(number,0,100001*sizeof(long));
int n;
string s;
//cin>>n;
file>>n;
int i,j,k,l;
j = 0;
int count = n;
while(n-- != 0)
{
//cin>>s;
file>>s;
l = s.length();
for(i = 0;i < l;i ++)
{
if(s.at(i) == 'Q' || s.at(i) == 'Z' || s.at(i) == '-')
continue;
if(s.at(i) >= '0' && s.at(i) <= '9')
number[j] = 10 * number[j] + (s.at(i) - '0');
else
number[j] = 10 * number[j] + trans(s.at(i));
}
j++;
}
qsort(number,count,sizeof(long),cmp);
int flag = 0;
int repeat = 1;
number[count] = -1; //end flag
for(i = 1;i <= count;i ++)
{
if(number[i] == number[i - 1])
repeat ++;
else
{
if(repeat > 1)//output
{
flag = 1;
//cout<<number[i - 1]/10000<<"-"<<number[i - 1]%10000<<" "<<repeat<<endl;
strstream ss;
string s;
ss << number[i-1];
ss >> s;
string str = "0000000";
j = 0;
for(k = s.length() - 1;k > -1;k --)
str[6-j++] = s.at(k);
for(k = 0;k < str.length();k ++)
{
if(k == 3)
cout<<"-";
cout<<str.at(k);
}
cout<<" "<<repeat<<endl;
repeat = 1;
}

}
}
if(flag == 0)
cout<<"No duplicates."<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: