poj-1002
很久不做oj,发现思维明显跟不上(大学时候也跟不上:)).一直在想文件输入的接口是什么来着,终于google到
freopen("data", "r", stdin);
程序设计重在思维:我总结有三点,一是项目管理思维(开发方法、人员结构、管理方式);二是软件工程思维(软件架构);三是解题逻辑思维(针对特定逻辑问题的思路)
思维需要锻炼,且要持续不断.oj做题正是锻炼解题逻辑思维的好方法.
POJ-1002 - 487-3279
问题描述:
电话号码有多种写法,010-8888,0108888,-010-8888,-0-1-0-8-8-8-8-等等.如果要把A-Z大写字母映射成数字的话可以出现类似AAA-ABCD,-A-A-AABCD等形式.
但是最终,都能映射成数字的形式,并且类似000-0000这样的3个数字-4个数字的形式是标准形式.问题就是:给定一系列的上面提到的电话号码,要求输入标准形式以及此电话号码出现的次数.电话号码输出按照字典序.
问题分析:
1. 需要理解输入,可能含有-,A-Z,0-9,并且每行输入是不定长的.这个可以先用合适的15(7个数字,8个-).不行再加长.
2. 输入格式:(1)字典序 (2)电话+个数 (3)一个重复电话也没有的情况下,要输出"No duplicates."
解题思路:
1. 直接的想法是把输入转换成数字,之后数组中标记数字出现的个数.之后遍历数组,进行输出.
AC代码:(效率不高,简单方法)
#include <stdio.h>
#include <stdlib.h>
//result
char g_str[8];
//map
int number_map[] = {
// 'A'
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6,
7,0,7,7,
8,8,8,
9,9,9,0
};
int result_map[10000000];
int str_to_number(char* str)
{
int i = 0;
int t = 0;
int n = 0;
for(i = 0; i < 48; ++i)
{
if (*(str + i) < '0' || *(str + i) > 'Z' || (*(str + i) > '9' && *(str + i) < 'A')) continue;
if(*(str + i) < 'A')
{
t = *(str + i) - '0';
}
else
{
t = number_map[*(str + i) - 'A'];
}
n *= 10;
n += t;
}
return n;
}
char* number_to_standstr(int n)
{
int i = 0, t = 0;
for (i = 7;i > 0; --i)
{
t = n%10;
n /= 10;
if (i > 3)
g_str[i] = '0' + t;
else
g_str[i-1] = '0' + t;
}
g_str[3] = '-';
return g_str;
}
int c = 0;
int main(void)
{
char str[48];
int r = 0, t = 0;
//freopen("data", "r", stdin);
memset(result_map, 0, sizeof(result_map));
// number count
scanf("%d", &c);
t = 0;
// deal with every number
while(c > 0)
{
memset(str, '-', sizeof(str));
scanf("%s", str);
--c;
r = str_to_number(str);
result_map[r] += 1;
}
c = 0;
while(t < 10000000)
{
if (result_map[t] > 1)
{
++c;
printf("%s %d\n", number_to_standstr(t), result_map[t]);
}
++t;
}
if (0 == c)
printf("No duplicates.\n");
return 0;
}
#include <stdlib.h>
//result
char g_str[8];
//map
int number_map[] = {
// 'A'
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6,
7,0,7,7,
8,8,8,
9,9,9,0
};
int result_map[10000000];
int str_to_number(char* str)
{
int i = 0;
int t = 0;
int n = 0;
for(i = 0; i < 48; ++i)
{
if (*(str + i) < '0' || *(str + i) > 'Z' || (*(str + i) > '9' && *(str + i) < 'A')) continue;
if(*(str + i) < 'A')
{
t = *(str + i) - '0';
}
else
{
t = number_map[*(str + i) - 'A'];
}
n *= 10;
n += t;
}
return n;
}
char* number_to_standstr(int n)
{
int i = 0, t = 0;
for (i = 7;i > 0; --i)
{
t = n%10;
n /= 10;
if (i > 3)
g_str[i] = '0' + t;
else
g_str[i-1] = '0' + t;
}
g_str[3] = '-';
return g_str;
}
int c = 0;
int main(void)
{
char str[48];
int r = 0, t = 0;
//freopen("data", "r", stdin);
memset(result_map, 0, sizeof(result_map));
// number count
scanf("%d", &c);
t = 0;
// deal with every number
while(c > 0)
{
memset(str, '-', sizeof(str));
scanf("%s", str);
--c;
r = str_to_number(str);
result_map[r] += 1;
}
c = 0;
while(t < 10000000)
{
if (result_map[t] > 1)
{
++c;
printf("%s %d\n", number_to_standstr(t), result_map[t]);
}
++t;
}
if (0 == c)
printf("No duplicates.\n");
return 0;
}
另附超级简洁代码一段,来自csdn
#include <stdio.h>
int main( )
{
static int i, j, k, n, hash[10000000]={0};
char tel[32], map[256];
map['A'] = map['B'] = map['C'] = 2;
map['D'] = map['E'] = map['F'] = 3;
map['G'] = map['H'] = map['I'] = 4;
map['J'] = map['K'] = map['L'] = 5;
map['M'] = map['N'] = map['O'] = 6;
map['P'] = map['R'] = map['S'] = 7;
map['T'] = map['U'] = map['V'] = 8;
map['W'] = map['X'] = map['Y'] = 9;
for (i='0'; i<='9'; i++)
map[i] = i-'0';
scanf("%d", &n);
while (n--) {
scanf("%s", tel);
for (i=k=0,j=1000000; tel[i]; i++) {
if (tel[i]=='-') continue;
k += map[tel[i]] * j;
j /= 10;
}
hash[k]++;
}
for (i=j=0; i<10000000; i++)
if (hash[i]>1) {
j++;
printf("%03d-%04d %d\n", i/10000, i%10000, hash[i]);
}
if (j==0)
puts("No duplicates.");
}
int main( )
{
static int i, j, k, n, hash[10000000]={0};
char tel[32], map[256];
map['A'] = map['B'] = map['C'] = 2;
map['D'] = map['E'] = map['F'] = 3;
map['G'] = map['H'] = map['I'] = 4;
map['J'] = map['K'] = map['L'] = 5;
map['M'] = map['N'] = map['O'] = 6;
map['P'] = map['R'] = map['S'] = 7;
map['T'] = map['U'] = map['V'] = 8;
map['W'] = map['X'] = map['Y'] = 9;
for (i='0'; i<='9'; i++)
map[i] = i-'0';
scanf("%d", &n);
while (n--) {
scanf("%s", tel);
for (i=k=0,j=1000000; tel[i]; i++) {
if (tel[i]=='-') continue;
k += map[tel[i]] * j;
j /= 10;
}
hash[k]++;
}
for (i=j=0; i<10000000; i++)
if (hash[i]>1) {
j++;
printf("%03d-%04d %d\n", i/10000, i%10000, hash[i]);
}
if (j==0)
puts("No duplicates.");
}