4 条题解
-
5
勤劳做法:
#include<bits/stdc++.h> using namespace std; int main(){ long long n,q[99]={47,74,4477,4747,4774,7447,7474,7744,444777,447477,447747,447774,474477,474747,474774,477447,477474,477744,744477,744747,744774,747447,747474,747744,774447,774474,774744,777444,44447777,44474777,44477477,44477747,44477774,44744777,44747477,44747747,44747774,44774477,44774747,44774774,44777447,44777474,44777744,47444777,47447477,47447747,47447774,47474477,47474747,47474774,47477447,47477474,47477744,47744477,47744747,47744774,47747447,47747474,47747744,47774447,47774474,47774744,47777444,74444777,74447477,74447747,74447774,74474477,74474747,74474774,74477447,74477474,74477744,74744477,74744747,74744774,74747447,74747474,74747744,74774447,74774474,74774744,74777444,77444477,77444747,77444774,77447447,77447474,77447744,77474447,77474474,77474744,77477444,77744447,77744474,77744744,77747444,77774444,4444477777}; cin>>n; cout<<q[lower_bound(q,q+99,n)-q]; return 0; }
-
2
我用的是广搜做法: 1:先将4和7放入队列,依次在后面加4和7(如果要在x后加y,可以用x*10+y的方式)。 2:在添加数(代码中的now)的过程中,判断数是否满足条件(now >= n && lucky(now)),如果可以这个数就是答案,直接输出即可。lucky(now)表示判断now 4的位数和7的位数是否相等。
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,a,b,c) for (int i = a;i <= b;i += c) int n; queue<int> q; bool lucky(int x) { int n4=0,n7=0; while (x > 0) { if (x % 10 == 4) n4++; else if (x % 10 == 7) n7++; x /= 10; } return (n4 == n7); }//判断now 4的位数和7的位数是否相等 void bfs() { q.push(4); q.push(7); while (!q.empty()) { int now = q.front(); q.pop(); if (now >= n && lucky(now)) { cout << now; break; } q.push(now*10+4); q.push(now*10+7); } }//广搜答案 signed main() { cin >> n; bfs(); return 0; }
- 1
信息
- ID
- 1909
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- 递交数
- 148
- 已通过
- 66
- 上传者