A.平衡三进制Ⅱ
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
using namespace std;
#define DEBUG 0
vector<int> Zhengshu;
vector<int> Xiaoshu;
void function1(long long zhengshu,long long fenzi,int jieshu){
while(zhengshu != 0){
Zhengshu.push_back(zhengshu % 3);
zhengshu /= 3;
}
while(fenzi != 0){
Xiaoshu.push_back(fenzi % 3);
fenzi /= 3;
}
int tian0 = jieshu - Xiaoshu.size();
Xiaoshu.insert(Xiaoshu.end(),tian0,0);
}
int function2(){
int len_xiaoshu = Xiaoshu.size();
int len_zhengshu = Zhengshu.size();
int len_1 = len_xiaoshu + len_zhengshu;
int flag = 0;
if(len_xiaoshu > 0){
for(int i = 0;i < Xiaoshu.size(); ++i){
Xiaoshu[i] = flag + Xiaoshu[i] + 1;
if(Xiaoshu[i] > 2){
Xiaoshu[i] %= 3;
flag = 1;
} else {
flag = 0;
}
}
}
for(int i = 0;i < Zhengshu.size();++i){
Zhengshu[i] = flag + Zhengshu[i] + 1;
if(Zhengshu[i] > 2){
Zhengshu[i] %= 3;
flag = 1;
} else {
flag = 0;
}
}
if(flag == 1){
Zhengshu.push_back(1);
}
return len_1;
}
void functon3(int len_1){
int len_left_1 = len_1 - Xiaoshu.size();
for(int i = 0; i < len_left_1; i++){
Zhengshu[i] = Zhengshu[i] - 1;
if(Zhengshu[i] == -1) Zhengshu[i]=2;
}
for(int i = 0; i < Xiaoshu.size(); ++i){
Xiaoshu[i] = Xiaoshu[i] - 1;
if(Xiaoshu[i] == -1) Xiaoshu[i]=2;
}
}
void function4(){
for(int i = 0;i<Zhengshu.size();i++){
if(Zhengshu[i] == 2){
Zhengshu[i] = 1;
} else if(Zhengshu[i] == 1){
Zhengshu[i] = 2;
} else {
continue;
}
}
for(int i = 0;i<Xiaoshu.size();i++){
if(Xiaoshu[i] == 2){
Xiaoshu[i] = 1;
} else if(Xiaoshu[i] == 2){
Xiaoshu[i] = 1;
} else {
continue;
}
}
}
int main(){
long long A;
long long B;
cin >> A >> B;
int panduan = 0;
if(A < 0){
A = -1*A;
panduan = 1;
}
long long zhengshu = A/B;
long long fenzi = A%B;
int jieshu = log(B)/log(3);
function1(zhengshu,fenzi,jieshu);
int len_1 = function2();
functon3(len_1);
#if DEBUG
for(int i = Zhengshu.size()-1; i>=0; i--){
cout << Zhengshu[i];
}
for(int i = Xiaoshu.size()-1; i>=0 ; i--){
cout << Xiaoshu[i];
}
cout << endl;
#endif
if(panduan == 1){
function4();
}
if(Zhengshu.size() == 0 ){
Zhengshu.push_back(0);
}
for(int i = Zhengshu.size()-1; i>=0; i--){
cout << Zhengshu[i];
}
if(Xiaoshu.size() != 0){
cout<<".";
for(int i = Xiaoshu.size()-1; i>=0 ; i--){
cout << Xiaoshu[i];
}
cout << endl;
}
return 0;
}