Eva's Balance-OJ

Eva's Balance
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4213 Accepted: 2023

Description

Eva has a balance with 20 poises. The weights of the poises are 1, 3, 9, 27,...,3^19. Eva asserts that she has a way to measure any object whose weight is an integer from 1 to (3^20-1)/2. Assuming that Eva has placed an object with the weight in this range on the left tray of the balance, your task is to place the proper poises on the proper trays so as to weigh the object out.

Input

The first line is an integer T (1 <= T <= 20), which shows the number of the test cases. Each of the following T lines contains an integer W (1 <= W <= (3^20-1)/2), expressing the weight of an object.

Output

For each test case print a line, showing the weights of the poises on the left tray and the right tray. Use a space to separate the left tray and the right tray. The poises on the same tray are arranged in the increasing order, and a comma separates every two of them. If there is no poise on the tray, output "empty".

Sample Input

3
9
5
20

Sample Output

empty 9
1,3 9

1,9 3,27

个人理解:

1.根据题目理解题意,由于原题是英文而且太长,所以不好理解,下面是我的理解着大批的简单版。

天平砝码放置,有重量为1、3、9、27、81 .. 3^K... 3^19 砝码,天平左边给定一个重量值w (1=< w <= (3^20-1)/2 ),求如何摆放砝码使天平两端达到平衡。(题目文字太复杂了!)。

2. 注意题目对输出形式的要求,之前输出中有多加入“/n”,就不能通过。

3.对题目中的memset()函数,

 void *memset(void *s, int ch, size_t n); 函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。
(代码时我在百度搜索得到的结果,然后再自己根据题目意思一句一句理解来的,所以其中的一些我没有学习到的知识点,我都有自己百度。)
结果时间内存语言
Accepted16156C

代码:

#include<stdio.h> #include<string.h> void solve(int n) {     int i,k=n,left[100],right[100],tt;     int a[200];     memset(a,0,sizeof(a));//将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。     i=0;     while(k!=0)     {         a[i++]=k%3;         k=k/3;     }     for(k=0;k<i;k++)         if(a[k]==3)         { a[k]=0;a[k+1]++;}         else if(a[k]==2)         { a[k]=-1;a[k+1]++;}         tt=1;         int li=0,ri=0;         for(k=0;k<=i;k++)         {             if(a[k]==-1)                 left[++li]=tt;             else if(a[k]==1)                 right[++ri]=tt;             tt*=3;         }         if(li==0)         {             printf("empty ");             for(i=1;i<ri;i++)                 printf("%d,",right[i]);             printf("%d",right[ri]);         }         else {             for(i=1;i<li;i++)                 printf("%d,",left[i]);             printf("%d ",left[li]);             for(i=1;i<ri;i++)                 printf("%d,",right[i]);             printf("%d",right[ri]);         } } int main() {     int i,t;     int n;     scanf("%d",&t);     while(t--)     {         scanf("%d",&n);         solve(n);     }     return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值