fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef char word_t[1024];
  5. word_t hash[1024];
  6.  
  7. int calc(char *word) {
  8. int sum = 0;
  9. while(*word) sum += *word++;
  10. return sum % 1024;
  11. }
  12.  
  13. char dir;
  14. void bubble_sort() {
  15. int i,d,f;
  16. while(1) {
  17. d = 0;
  18. for(i=0; i<1023; i++) {
  19. f = strcmp(hash[i], hash[i+1]);
  20. if(((dir & 1) && f < 0) || ((~dir & 1) && f > 0)) {
  21. word_t t;
  22. strcpy(t, hash[i]);
  23. strcpy(hash[i], hash[i+1]);
  24. strcpy(hash[i+1], t);
  25. d = 1;
  26. }
  27. }
  28. if(!d) break;
  29. }
  30. }
  31.  
  32. int main(int argc, char **argv){
  33. char buf[1024];
  34. int i;
  35.  
  36. //dir = argv[1][0];
  37. dir='0';
  38. while(fgets(buf, sizeof(buf), stdin)) {
  39. strcpy(hash[calc(buf)], buf);
  40. }
  41.  
  42. bubble_sort();
  43.  
  44. for(i=0; i<1024; i++) {
  45. if(hash[i][0]) printf("%s", hash[i]);
  46. }
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 11344KB
stdin
orange
banana
apple
peach
melon
daikon
stdout
apple
banana
daikon
melon
orange
peach