当前位置:网站首页>H.Happy Number (进制转换/第n个特殊数)(2021牛客暑期多校训练营9 )
H.Happy Number (进制转换/第n个特殊数)(2021牛客暑期多校训练营9 )
2022-04-22 06:17:00 【S atur】

题意: 寻找第n个只包含 ‘2’ ‘3’ 和 ‘6’的数字。
思路:有点类似与二进制,只不过这里是把 n 转换为三进制数 x ,再通过 x 转换为对应的数字。特别需要注意的是 x 中为 0 的位置 其实代表需要向前借一位,再将余下的数对应为相应的数字。
代码实现:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9+7;
const int N = 2e5 + 5;
inline void read(int &x){
char t=getchar();
while(!isdigit(t)) t=getchar();
for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}
int T = 1, n;
int work(int n) //十进制转三进制
{
int flag = 1, y = 0, res;
while(1){
res = n%3;
n /= 3;
y += res*flag;
flag *= 10;
if(n==0)break;
}
return y;
}
signed main()
{
IOS;
// cin >> T;
while(T --){
cin >> n;
int x = work(n);
// if(n%3==0) cout << x << " " << n << endl;
// else{
// cout << "x: " << x << " ";
string s;
int tmp = x, cnt = 0;
while(tmp){ //统计x共有多少位
tmp /= 10;
cnt ++;
}
while(x){
int y = x%10;
x /= 10;
cnt --;
if(tmp==-1){ //如果之前有向当前位置借数
if(y==0) y = 2;
else y --, tmp = 0;
}
if(y==0&&cnt){ //如果当前位置为0且不是最高位,便还可以向前借数
y = 3;
tmp = -1;
}
if(y==1) s += '2';
if(y==2) s += '3';
if(y==3) s += '6';
}
reverse(s.begin(), s.end());
cout << s << endl;
// }
}
return 0;
}
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Satur9/article/details/119709995
边栏推荐
- LaTex中插入图片报错Unknown graphics extension: .1.jpg. }
- C.Ducky Debugging(简单判断/签到)(2021年度训练联盟热身训练赛第五场 )
- Win10 Anaconda install cocotb
- The problem of interval summation -- difference
- 在类加载的过程中,类变量的分配区域和实例变量的分配区域不一样
- [solution] Luogu p6186 [noi online 1 improvement group] bubble sorting: [bubble sorting] and [reverse order pair] problems
- This关键字详细概述
- 384 · 最长无重复字符的子串
- Codeforces Round #623
- CF1547E Air Conditioners
猜你喜欢

L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)

LeetCode - 1 - (树的子结构、组合、螺旋矩阵、全排列<ⅠⅢ>)

Add author photos and author profiles to latex

The thought and code of eight sorting

Some mechanisms of synchronized lock optimization (lock upgrade)

1005 Monopoly 同余求解(2021中国大学生程序设计竞赛CCPC-网络选拔赛重赛)

Two algorithm questions for Microsoft intern interview -- 20220119

双向循环链表(详)

(4) Character set in SQL Server (collation)

LeetCode - 2 - (括号生成、最长回文串、环形链表、反转链表、两两交换链表中的节点)
随机推荐
Virtual machine disk space shrinks
【数论】素数(一):基本概念、性质、猜想、定理
要是我再犯这种憨憨错误就.../(ㄒoㄒ)/~~
B.Cutting Corners (简单几何/签到)(2021年度训练联盟热身训练赛第五场)
为什么非主键索引的叶子结点存放的数据是主键值
The only storage area in the JVM where GC and oom will not occur
This关键字详细概述
SecureCRT infinite loop script
Leetcode - 7 - (nearest common ancestor of binary tree, rotation array, direct of binary tree, next permutation, combined sum)
L2-004 这是二叉搜索树吗?(先序输入&判断搜索二叉树&后序输出)
Redis Advanced
[number theory] congruence (V): multivariate linear congruence equation
[solution] Luogu p6186 [noi online 1 improvement group] bubble sorting: [bubble sorting] and [reverse order pair] problems
278 · 绘制填充
队列(详解)——手撕队列习题
八大排序的思想及其代码
The problem of interval summation -- difference
Host cannot Ping virtual machine in bridging mode
SQL review, grammar notes, fresh out
(5) Use Navicat to create database data table and set ID auto increment