Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define TEST_TIMES 10000000
  6.  
  7. typedef struct node
  8. {
  9.     char* key;
  10.     int value;
  11.     struct node* next;
  12. } LinkList;
  13.  
  14. char* pick(LinkList* head);
  15.  
  16. // 产生一个精度为0.001的 0到1之间的浮点数,不会等于1
  17. float randf()
  18. {
  19.     return (float)(rand() % 1001) * 0.001f;
  20. }
  21.  
  22. int main()
  23. {
  24.     int i;
  25.     char* name;
  26.     char* found;
  27.     int weight;
  28.     LinkList* head = (LinkList*)malloc(sizeof(LinkList));
  29.     LinkList* statHead = (LinkList*)malloc(sizeof(LinkList));
  30.     LinkList* node = head;
  31.     LinkList* statNode = statHead;
  32.  
  33.     while(1)
  34.     {
  35.         name = (char*)malloc(sizeof(char) * 100);
  36.         scanf("%s", name);
  37.         if (strcmp(name, "0") == 0)
  38.         {
  39.             break;
  40.         }
  41.  
  42.         node->next = (LinkList*)malloc(sizeof(LinkList));
  43.         statNode->next = (LinkList*)malloc(sizeof(LinkList));
  44.         node = node->next;
  45.         statNode = statNode->next;
  46.         node->key = name;
  47.         statNode->key = name;
  48.  
  49.         scanf("%d", &weight);
  50.         node->value = weight;
  51.         statNode->value = 0;
  52.     }
  53.    
  54.     for (i = 0; i < TEST_TIMES; i++)
  55.     {
  56.         found = pick(head);
  57.         if (found != NULL)
  58.         {
  59.             statNode = statHead->next;
  60.             while (statNode!=NULL)
  61.             {
  62.                 if (strcmp(statNode->key,found) == 0)
  63.                 {
  64.                     statNode->value++;
  65.                     break;
  66.                 }
  67.                 statNode = statNode->next;
  68.             }
  69.         }
  70.     }
  71.  
  72.     statNode = statHead->next;
  73.     while (statNode != NULL)
  74.     {
  75.         printf("%s -> %.0f%%\n", statNode->key, (double) statNode->value / TEST_TIMES * 100);
  76.         statNode = statNode->next;
  77.     }
  78.  
  79.     return 0;
  80. }
  81.  
  82. //只能更改以下部分
  83. char* pick(LinkList* head)
  84. {
  85.     int sum = 0;
  86.     int total = 0;
  87.     LinkList* node = head->next;
  88.     float rnd = randf();
  89.  
  90.     while (node != NULL)
  91.     {
  92.         total+=node->value;
  93.         node = node->next;
  94.     }
  95.  
  96.     node = head->next;
  97.  
  98.     while (node != NULL)
  99.     {
  100.         sum += node->value;
  101.         if (sum / (double) total >= rnd)
  102.         {
  103.             return node->key;
  104.         }
  105.         node = node->next;
  106.     }
  107.     return NULL;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement