[hdu5255]枚举

思路:这题与csu1392题目类似,方法类似。枚举最高位,最低位和中间数字的长度,然后列等式,计算中间的数字,看长度是不是跟枚举的一致,需要注意的是中间数字可以有前导0,如果根据等式算出来的中间数字为K,枚举的长度为L,也就是说需要满足length(K)<=L。

csu1392: http://www.cnblogs.com/jklongint/p/4419007.html

代码:

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
#pragma comment(linker, "/STACK:10240000,10240000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <queue>
using  namespace  std;
 
long  long  ans[1234], MI[12], X;
int  cnt;
int  length( long  long  x) {
     int  ans = 0;
     while  (x) {
         ans ++;
         x /= 10;
     }
     return  ans;
}
void  solve( long  long  L,  long  long  a,  long  long  b) {
     long  long  ga = (a * X - b * MI[6]) * MI[L + 1] + b * X - a * MI[6];
     long  long  gb = MI[7] - X * 10, K = ga / gb;
     if  (ga % gb == 0 && K >= 0 && length(K) <= L) ans[cnt ++] = a * MI[L + 1] + K * 10 + b;
}
int  main() {
#ifndef ONLINE_JUDGE
     freopen ( "in.txt" "r" , stdin);
#endif // ONLINE_JUDGE
     MI[0] = 1;
     for  ( int  i = 1; i < 12; i ++) MI[i] = MI[i - 1] * 10;
     int  T, cas = 0;
     cin >> T;
     while  (T --) {
         printf ( "Case #%d:\n" , ++ cas);
         double  x;
         cin >> x;
         X = ( long  long )(x * MI[6] + 0.1);
         if  (X == MI[6]) {
             cout << 0 << endl;
             continue ;
         }
         cnt = 0;
         for  ( int  L = 0; L < 9; L ++) {
             for  ( int  a = 1; a < 10; a ++) {
                 for  ( int  b = 0; b < 10; b ++) {
                     solve(L, a, b);
                 }
             }
         }
         cout << cnt << endl;
         for  ( int  i = 0; i < cnt; i ++) {
             printf ( "%I64d%c" , ans[i], i == cnt - 1?  '\n'  ' ' );
         }
     }
     return  0;
}

 

你可能感兴趣的:(HDU)