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 |
为什么我这个代码所有的数据和表打出来的数据是一模一样的,但是交题却是WA?#include <iostream> #include <functional> #include <algorithm> using namespace std; static int input[10]; static int length, cutA, cutB; static int zero_set[6] = { 1, 10, 100, 1000, 10000, 100000 }; int cal(const int, const int); void cal_odd(const int, const int); int fcmop(const void *a, const void *b) { return *(int *)a - *(int *)b; } int main(void) { int case_sum, tmp; scanf("%d", &case_sum); getchar(); for (int i = 0; i < case_sum; i++) { length = 0; while ((tmp = getchar()) != '\n') { if (tmp != ' ') input[length++] = tmp - '0'; } qsort(input, length, sizeof(int), fcmop); cutA = length / 2; cutA = cutA > length - cutA ? cutA : length - cutA; cutB = length - cutA; if (length == 0) cout << 0 << endl; else if (length == 1) cout << input[0] << endl; else if (length == 2) cout << abs(input[1] - input[0]) << endl; else { int min_delta, min_sum; if (input[0] != 0) { if (cutA == cutB)//如果两个子串的长度相同,则取差值最小的相邻数,大的作为n的最高位,小的作为m的最高位,n的剩下位从小的补齐,m从大的补齐 { min_sum = INT_MAX; min_delta = input[1] - input[0]; for (int i = 0; i < length - 1; i++) { if (input[i + 1] - input[i] < min_delta) { min_delta = input[i + 1] - input[i]; min_sum = min(min_sum, cal(i + 1, i)); } else if (input[i + 1] - input[i] == min_delta) min_sum = min(min_sum, cal(i + 1, i)); } cout << min_sum << endl; } else cal_odd(0, length - 1); } else//input[0]==0; { if (cutA == cutB)//如果字串长度相同,A-B则A的最高位要比B的最高位的差值要最小且比他大,且要从1开始 { min_sum = INT_MAX; min_delta = input[2] - input[1]; for (int i = 1; i < length - 1; i++) { if (input[i + 1] - input[i] < min_delta) { min_delta = input[i + 1] - input[i]; min_sum = min(min_sum, cal(i + 1, i)); } else if (input[i + 1] - input[i] == min_delta) min_sum = min(min_sum, cal(i + 1, i)); } cout << min_sum << endl; } else cal_odd(1, length - 1);//如果cutA!=cutB,那么直接选一头一尾的最小 } } } return 0; } int cal(const int posAS, const int posBS) { //专门算偶数重复的情况 int sumA = 0, sumB = 0, lenA = 1, lenB = 1; sumA += input[posAS] * zero_set[cutA - lenA]; sumB += input[posBS] * zero_set[cutB - lenB]; for (int i = 0; lenA <= cutA; i++) { if (i != posAS && i != posBS) { lenA++; sumA += input[i] * zero_set[cutA - lenA]; } } for (int i = length - 1; lenB <= cutB; i--) { if (i != posAS && i != posBS) { ++lenB; sumB += input[i] * zero_set[cutB - lenB]; } } return sumA - sumB; } void cal_odd(const int posAS, const int posBS) { int lenA = 1, lenB = 1, sumA = 0, sumB = 0; sumA += input[posAS] * zero_set[cutA - lenA]; sumB += input[posBS] * zero_set[cutB - lenB]; for (int i = 0; lenA <= cutA; i++) { if (i != posAS && i != posBS) { ++lenA; sumA += input[i] * zero_set[cutA - lenA]; } } for (int i = length - 2; lenB <= cutB; i--) { if (i != posAS && i != posBS) { ++lenB; sumB += input[i] * zero_set[cutB - lenB]; } } cout << sumA - sumB << endl; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator