Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244810
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-08 23:33:00

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

--------------------
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>

  7. typedef struct _node {
  8.     char *name;
  9.     int    nr;
  10.     struct _node *next;
  11. } node;


  12. node *insert(node **head, node *new)
  13. {
  14.     node *node=*head, *pnode = NULL;

  15.     while (node) {
  16.         if (strcmp(node->name, new->name) > 0)
  17.             break;
  18.         else {
  19.             pnode = node;
  20.             node = node->next;
  21.         }
  22.     }

  23.     if (pnode) {
  24.         pnode->next = new;
  25.         new->next = node;
  26.     } else {    /* head */
  27.         *head = new;
  28.         new->next = node;
  29.     }

  30.     return new;
  31. }

  32. /* hash */
  33. int node_map(node **domain, char *name)
  34. {
  35.     int key;
  36.     node *new = malloc(sizeof(node));

  37.     memset(new, 0, sizeof(node));
  38.     new->name = strdup(name);
  39.     key = (name[0] - 'A') % 26;
  40.     insert(&domain[key], new);

  41.     return 0;
  42. }

  43. void node_unmap(node **domain, node *node)
  44. {
  45. }

  46. unsigned int score(char *name, int nr)
  47. {
  48.     int i, sum = 0;
  49.     for (i = 0; i < strlen(name); i++) {
  50.         sum += name[i] - '@';
  51.     }

  52.     return sum * nr;
  53. }

  54. node *domain[26];
  55. int main(int argc, const char *argv[])
  56. {
  57.     node *node;
  58.     int i, sum = 0, count = 0;
  59.     FILE *fp;
  60.     char buf[1024];
  61.     char *lineptr[1] ={buf};
  62.     int n;

  63.     fp = fopen(argv[1], "r");
  64.     if (!fp) {
  65.         perror("open error");
  66.         exit(-1);
  67.     }


  68.     while (getdelim(lineptr, &n, ',', fp) > 0) {
  69.         char *quote = strchr(buf+1, '"');
  70.         if (quote) *quote = '\0';
  71.         node_map(domain, buf+1);
  72.     }


  73.     for (i = 0; i < sizeof(domain)/sizeof(domain[0]); i++) {
  74.         node = domain[i];
  75.         while (node) {
  76.             count++;
  77.             sum += score(node->name, count);
  78.             node = node->next;
  79.         }
  80.     }

  81.     printf("sum: %d\n", sum);

  82.     return 0;
  83. }

  84. 用hash表排序.不知道有没有优化的办法.


阅读(1566) | 评论(0) | 转发(0) |
0

上一篇:euler21

下一篇:euler20

给主人留下些什么吧!~~