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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值