Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
不多解释,上代码,STL?希望可以帮到你#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> #include<string> #include<set> std::pair<std::string, int> five_digit[10000]; int fn; std::pair<std::string, int> three_digit[10000]; int tn; std::pair<std::string, int> first_class[10000]; int fcn; std::set<std::string> se; std::string invalid[10000]; int in; int mat[1000][100]; bool isdigit(const char* str) { if (strlen(str) == 5) { for (int i = 0; i < 5; ++i) { if (str[i] < '0' || str[i] > '9') return false; } } else { return false; } for (int i = 0; i < 5; ++i) { if (str[i] != '0') { return true; } } return false; } int main() { char temp[100]; while (scanf("%s", temp) != EOF) { if (isdigit(temp)) { mat[(temp[0] - '0') * 100 + (temp[1] - '0') * 10 + temp[2] - '0'][(temp[3] - '0') * 10 + temp[4] - '0']++; } else { if (se.find(temp) == se.end()) invalid[in++] = temp; se.insert(temp); } } for (int i = 0; i < 1000; ++i) { for (int j = 0; j < 100; ++j) { if (mat[i][j] >= 10) { sprintf(temp, "%03d%02d", i, j); /*if (mat[i][j] > 15 && mat[i][j] < 20) { five_digit[fn].first = temp; five_digit[fn].second = mat[i][j] - (mat[i][j] % 10 - 5); mat[i][j] = mat[i][j] % 10 - 5; } else { five_digit[fn].first = temp; five_digit[fn].second = mat[i][j]; mat[i][j] = 0; }*/ five_digit[fn].first = temp; five_digit[fn].second = mat[i][j] / 15 * 15; mat[i][j] %= 15; if (mat[i][j] >= 10) { five_digit[fn].second += mat[i][j]; mat[i][j] = 0; } fn++; } } } for (int i = 0; i < 1000; ++i) { int sum = 0; int pos = -1; for (int j = 0; j < 100; ++j) { sum += mat[i][j]; if (mat[i][j]) pos = j; } if (sum >= 10) { sprintf(temp, "%03dxx", i); /*if (sum >15 && sum < 20) { three_digit[tn].first = temp; three_digit[tn].second = sum - (sum % 10 - 5); sum = sum % 10 - 5; for (int j = 0; j < 100; ++j) { if (mat[i][j]) { if (sum > 0) { sum -= mat[i][j]; sprintf(temp, "%03d%02d", i,j); first_class[fcn].first = temp; first_class[fcn].second = sum > 0 ? mat[i][j] : sum; fcn++; } mat[i][j] = 0; } } } else { three_digit[tn].first = temp; three_digit[tn].second = sum; }*/ three_digit[tn].first = temp; three_digit[tn].second = sum / 15 * 15; sum %= 15; if (sum >= 10) { three_digit[tn].second += sum; sum = 0; } for (int j = 0; j < 100; ++j) { if (mat[i][j]) { if (sum > 0) { sum -= mat[i][j]; sprintf(temp, "%03d%02d", i, j); first_class[fcn].first = temp; first_class[fcn].second = sum > 0 ? mat[i][j] : sum; fcn++; } mat[i][j] = 0; } } ++tn; } else { for (int j = 0; j < 100; ++j) { if (mat[i][j]) { sprintf(temp, "%03d%02d", i, j); first_class[fcn].first = temp; first_class[fcn].second = mat[i][j]; ++fcn; } } } } std::sort(five_digit, five_digit + fn); std::sort(three_digit, three_digit + tn); std::sort(first_class, first_class + fcn); int c = 0, b = 0; printf("ZIP LETTERS BUNDLES\n\n"); for (int i = 0; i < fn; ++i) { c += five_digit[i].second; b += (five_digit[i].second + 7.5)/ 15; printf("%s%12d%12d\n",five_digit[i].first.c_str(), five_digit[i].second, int((five_digit[i].second + 7.5) / 15)); } printf("\n"); for (int i = 0; i < tn; ++i) { c += three_digit[i].second; b += (three_digit[i].second + 7.5)/ 15; printf("%s%12d%12d\n", three_digit[i].first.c_str(), three_digit[i].second, int((three_digit[i].second + 7.5)/ 15)); } printf("\n"); for (int i = 0; i < fcn; ++i) { c += first_class[i].second; b += (first_class[i].second + 7.5)/ 15; printf("%s%12d%12d\n", first_class[i].first.c_str(), first_class[i].second, int((first_class[i].second + 7.5) / 15)); } printf("\nTOTALS%11d%12d\n\n", c, b); printf("INVALID ZIP CODES\n\n"); for (int i = 0; i < in; ++i) { printf("%s\n", invalid[i].c_str()); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator