ACDream 1417 - Numbers(枚举)

思路

从1一直枚举10, 100, 1000到不超过n的10000.....,算出最接近1、10、等等的倍数,然后维护最小值

代码写得不优雅。。

代码

  
  
  
  
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <ctime>
  13. #include <cstdlib>
  14. #include <fstream>
  15. #include <string>
  16. #include <sstream>
  17. #include <map>
  18. #include <cmath>
  19. #define LL long long
  20. #define SZ(x) (int)x.size()
  21. #define Lowbit(x) ((x) & (-x))
  22. #define MP(a, b) make_pair(a, b)
  23. #define MS(arr, num) memset(arr, num, sizeof(arr))
  24. #define PB push_back
  25. #define F first
  26. #define S second
  27. #define ROP freopen("input.txt", "r", stdin);
  28. #define MID(a, b) (a + ((b - a) >> 1))
  29. #define LC rt << 1, l, mid
  30. #define RC rt << 1|1, mid + 1, r
  31. #define LRT rt << 1
  32. #define RRT rt << 1|1
  33. #define BitCount(x) __builtin_popcount(x)
  34. const double PI = acos(-1.0);
  35. const int INF = 0x3f3f3f3f;
  36. using namespace std;
  37. const LL MAXN = 1e18;
  38. const int MOD = 20071027;
  39. typedef pair<int, int> pii;
  40. typedef vector<int>::iterator viti;
  41. typedef vector<pii>::iterator vitii;
  42. int main()
  43. {
  44. //ROP;
  45. LL n, k;
  46. int i, j;
  47. while (cin >> n >> k, n + k)
  48. {
  49. char ans[] = "9999999999999999999999";
  50. LL ori = 1;
  51. if (k == 1)
  52. {
  53. cout << "1" << endl;
  54. continue;
  55. }
  56. for (; ori > 0 && ori <= MAXN && ori <= n; ori *= 10)
  57. {
  58. if (ori % k == 0)
  59. {
  60. char str[20];
  61. sprintf(ans, "%lld", ori);
  62. break;
  63. }
  64. else
  65. {
  66. LL tmp = ori;
  67. tmp += (k - tmp % k);
  68. if (tmp > n) continue;
  69. char str[20];
  70. sprintf(str, "%lld", tmp);
  71. if (strcmp(str, ans) < 0) strcpy(ans, str);
  72. }
  73. }
  74. printf("%s\n", ans);
  75. }
  76. return 0;
  77. }

你可能感兴趣的:(ACDream 1417 - Numbers(枚举))