首页 > 代码库 > Problem B: Ternarian Weights
Problem B: Ternarian Weights
大致题意:使用三进制砝码采取相应的措施衡量出给定的数字
主要思路:三进制,如果 大于 2 向前进位,之前一直没写好放弃了,这次终于写好了……
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <map>#include <cmath>#include <cstring>#include <string>#include <queue>#include <stack>#include <cctype>const double Pi = atan(1) * 4;using namespace std;long long base[] = {1,1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441,1594323,4782969,14348907,43046721,129140163,387420489,1162261467,3486784401};int a[100];int tt[110];int main(){ //freopen("input.in","r",stdin); //freopen("output.in","w",stdout); int n; cin >> n; while(n--){ int x; cin >> x; memset(a,0,sizeof(a)); memset(tt,0,sizeof(tt)); int tmp = x; while(tmp){ a[++a[0] ] = tmp % 3; tmp /= 3; } for(int i = 1;i <= a[0];i++){ if(a[i] == 2){ a[i+1]++; a[0] = max(i+1,a[0]); tt[ ++tt[0] ] = i; a[i] = 0; } if(a[i] == 3){ a[i+1]++; a[0] = max(i+1,a[0]); a[i] = 0; } } cout << "left pan:"; for(int i = tt[0];i > 0;i--){ cout << " " << base[ tt[i] ]; } cout << endl; cout << "right pan:"; for(int i = a[0];i > 0;i--){ if(!a[i]) continue; cout << " " << base[i]; } cout << endl; if(n) cout << endl; } return 0;}
Problem B: Ternarian Weights
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。