您的位置:首页 > 其它

UVa 755 - 487--3279 Or Poj 1002

2013-07-26 20:38 267 查看
UVa上没法提交这题,就去了Poj, poj上提交要略微修改。

方法一:电话号码转换为数字后排序。

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 100004;
const int BUFF_SIZE = 30;
int A[MAXN];
int vis[MAXN];
int n;
char buff[BUFF_SIZE];

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int T;
//scanf("%d", &T);
//while(T--) {
scanf("%d\n", &n);
int s, k;
char c;
for(int i=0; i<n; i++) {
s = 0;
while((c = getchar()) != '\n') {
if(c == '-') continue;
if(isalpha(c)) {
if(c <= 'R') k = (c-'A')/3 + 2;
else k = (c-'A'- 1)/3 + 2;
} else {
k = c - '0';
}
s = s*10 + k;
}
A[i] = s;
}
sort(A, A+n);
A
= A[n-1] + 1;
vis[0] = 1;
int found = 0;
for(int i=1; i<=n; i++) {
vis[i] = 1;
if(A[i] == A[i-1]) {
vis[i] += vis[i-1];
} else if(vis[i-1] > 1){
printf("%03d-%04d %d\n", A[i-1]/10000, A[i-1]%10000, vis[i-1]);
found = 1;
}
}
if(!found) printf("No duplicates.\n");
//if(T) printf("\n");
//}
return 0;
}


方法2: poj上比方法1快, 思想就是转换为数字后,计数排序

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
const int MAXN = 100004;
const int MAXM = 10000000;
int A[MAXN];
int cnt = 0;
int vis[MAXM];
int n;

int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif

scanf("%d\n", &n);
int s, k;
char c;
for(int i=0; i<n; i++) {
s = 0;
while((c = getchar()) != '\n') {
if(c == '-') continue;
if(isalpha(c)) {
if(c <= 'R') k = (c-'A')/3 + 2;
else k = (c-'A'- 1)/3 + 2;
} else {
k = c - '0';
}
s = s*10 + k;
}
++vis[s];
if(vis[s] == 2) {
A[cnt++] = s;
}
}
if(cnt) {
sort(A, A+cnt);
for(int i=0; i<cnt; i++)
printf("%03d-%04d %d\n", A[i]/10000, A[i]%10000, vis[A[i]]);
} else {
printf("No duplicates.\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: