1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| #include<iostream> #include<string.h> using namespace std;
void sort1(char* x, int size) { int count[50] = {}; char index[50][2] = {}; int i, j, k = 0, ti; char tc1,tc2; for (i = 0; i < size; i+=2) { for (j = i - 2; j >= 0; j-=2) { if (x[i] == x[j]&&x[i+1]==x[j+1]) break; } if (j == -2) { index[k][0] = x[i]; index[k][1] = x[i + 1]; k++; } } for (i = 0; i < k; i++) { for (j = 0; j < size; j+=2) { if (index[i][0] == x[j]&&index[i][1]==x[j+1]) count[i]++; } } for (i = 1; i < k; i++) { for (j = 0; j < k - i; j++) { if (count[j] > count[j + 1]) { ti = count[j], count[j] = count[j + 1], count[j + 1] = ti; tc1 = index[j][0], index[j][0] = index[j + 1][0], index[j + 1][0] = tc1; tc2 = index[j][1], index[j][1] = index[j + 1][1], index[j + 1][1] = tc2; } } } for (i = 0; i < k; i++) printf("%c%c:%d ", index[i][0],index[i][1], count[i]); }
void sort2(char* x, int size) { int count[50] = {}; char index[50][2] = {}; int i, j, k = 0, ti; char tc1, tc2; for (i = 0; i < size; i += 2) { for (j = i - 2; j >= 0; j -= 2) { if (x[i] == x[j] && x[i + 1] == x[j + 1]) break; } if (j == -2) { index[k][0] = x[i]; index[k][1] = x[i + 1]; k++; } } for (i = 0; i < k; i++) { for (j = 0; j < size; j += 2) { if (index[i][0] == x[j] && index[i][1] == x[j + 1]) count[i]++; } } for (i = 1; i < k; i++) { int max = 0; for (j = 1; j < k - i; j++) if (count[max] < count[j]) max = j; ti = count[max], count[max] = count[k - i], count[k - i] = ti; tc1 = index[max][0], index[max][0] = index[k - i][0], index[k - i][0] = tc1; tc2 = index[max][1], index[max][1] = index[k - i][1], index[k - i][1] = tc2; } for (i = 0; i < k; i++) printf("%c%c:%d ", index[i][0], index[i][1], count[i]); }
void sort(char index[][2], int count[], int low, int high) { int i = low, j = high+1; int key = count[low]; if (high <= low) return; while (true) { while (count[++i] < key) { if (i == high) break; } while (count[--j] > key) { if (j == low) break; } if (i >= j) break; char tc1, tc2; int ti; ti = count[j], count[j] = count[i], count[i] = ti; tc1 = index[j][0], index[j][0] = index[i][0], index[i][0] = tc1; tc2 = index[j][1], index[j][1] = index[i][1], index[i][1] = tc2; } int temp, tempc1, tempc2; temp = count[low], count[low] = count[j], count[j]=temp; tempc1 = index[low][0], index[low][0] = index[j][0], index[j][0] = tempc1; tempc2 = index[low][1], index[low][1] = index[j][1], index[j][1] = tempc2; sort(index, count, low, j - 1); sort(index, count, j + 1, high); }
void sort3(char* x, int size) { int count[50] = {}; char index[50][2] = {}; int i, j, k = 0; for(i = 0;i < size;i += 2) { for (j = i - 2; j >= 0; j -= 2) { if (x[i] == x[j] && x[i + 1] == x[j + 1]) break; } if (j == -2) { index[k][0] = x[i]; index[k][1] = x[i + 1]; k++; } } for (i = 0; i < k; i++) { for (j = 0; j < size; j += 2) { if (index[i][0] == x[j] && index[i][1] == x[j + 1]) count[i]++; } } sort(index, count, 0, k-1); for (i = 0; i < k; i++) printf("%c%c:%d ", index[i][0], index[i][1], count[i]); }
int main() { char s[100]; gets_s(s); sort1(s, strlen(s)); cout << endl; sort2(s, strlen(s)); cout << endl; sort3(s, strlen(s)); cout << endl; }
|