题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
#include <stdio.h> #include <math.h> #include <string.h> float getCharValue(char ch, double v) { int baseValue = 'z' - 'A' + 1; int beginValue = 'A' - 1; return ((ch - beginValue) / (1.0*baseValue)) * v; } typedef struct { short index; double value; }ORDER_NODE; int main() { char ch = 0; ORDER_NODE orderValue[1000] = {0}; char words[2000][128] = {0}; int num = 0; int indexWord = 0; int indexChar = 0; double CharValue = 0; scanf("%d\n",&num); for(int i = 0; i < 100; i++) { orderValue[i].index = i; } while(EOF != (ch = getchar())) { if(ch == '\n' ) { indexWord++; indexChar = 0; }else{ //CharValue = getCharValue(ch,pow(0.1,indexChar+1)); // orderValue[indexWord].value += CharValue; words[indexWord][indexChar++] = ch; } } ORDER_NODE temp; char tempW[101] = {0}; for(int i = 0; i < num; i++) { for(int j = i; j < num; j++) { // if(orderValue[i].value > orderValue[j].value) // { // temp = orderValue[i]; // orderValue[i] = orderValue[j]; // orderValue[j] = temp; // } if(strcmp(words[i], words[j]) > 0) { strcpy(tempW,words[i]); strcpy(words[i],words[j]); strcpy(words[j], tempW); } } } for(int i = 0; i < num; i++) { printf("%s\n",words[i]); } return 0; }