【HDOJ】1271 整数对
枚举,假设这个数x=a*10^(i+1)+b*10^i+c,去掉b后y=a*10^i+c,x+y=n,则x+y=n(mod10^i),求出c,注意c<10^i,但2*c有可能大于10^i,因此分类后,加一些限制条件枚举可得。
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 #define MAXN 105 6 int buf[MAXN]; 7 8 int comp(const void *a, const void *b) { 9 return *(int *)a - *(int *)b; 10 } 11 12 int main() { 13 int m, n; 14 int i, j, k, tmp; 15 int a, b, c; 16 17 while (scanf("%d", &n)!=EOF && n) { 18 k = 1; 19 for (m=0,i=0; i<10; ++i) { 20 j = n%k; 21 if ((j&1) == 0) { 22 c = j>>1; 23 // 11a+b = tmp; 24 tmp = n/k; 25 for (b=0; b<10; ++b) { 26 if ((tmp||b) && (tmp-b)%11 == 0) { 27 a = (tmp-b)/11; 28 buf[m++] = n-a*k-c;; 29 } 30 } 31 } 32 if (((j+k)&1) == 0) { 33 c = (j+k)>>1; 34 // 11a+b+1 = tmp 35 tmp = n/k-1; 36 for (b=0; b<10; ++b) { 37 if ((tmp||b) && (tmp-b)%11 == 0) { 38 a = (tmp-b)/11; 39 buf[m++] = n-a*k-c; 40 } 41 } 42 } 43 k *= 10; 44 } 45 if (m == 0) { 46 printf("No solution.\n"); 47 } else { 48 qsort(buf, m, sizeof(int), comp); 49 printf("%d", buf[0]); 50 for (i=1; i<m; ++i) { 51 if (buf[i] != buf[i-1]) 52 printf(" %d", buf[i]); 53 } 54 printf("\n"); 55 } 56 #ifndef ONLINE_JUDGE 57 fflush(stdout); 58 #endif 59 } 60 61 return 0; 62 }