那你叫我道长好了


私信TA

用户名:lajiao16

访问量:3375

签 名:

等  级
排  名 1380
经  验 2822
参赛次数 2
文章发表 2
年  龄 0
在职情况 学生
学  校 淄博技师学院
专  业 信息工程

  自我简介:

庐山烟雨浙江潮,未至千般恨不消。 到得还来别无事,庐山烟雨浙江潮。

用贪心的思想来解决这个问题:尽可能在增加砝码时使得砝码的重量最大。


要称重量为 1,需要砝码 1

重量为 2,增加砝码 3(使增加的砝码尽可能的大,所以不是 1 和 2 )


1 3 可以同时称 ‘1’=1,‘2’+1=3,‘3’=3,‘4’=3+1 ;

所以下一个是5,增加砝码 9(尽可能的大)


1 3 9 可以同时称(除去 1 和 3 能称的):

‘5’+3+1=9,‘6’+3=9,‘7’+3=9+1,‘8’+1=9,‘9’=9

‘10’=9+1,‘11’+1=9+3,‘12’=9+3,‘13’=1+3+9

下一个是14,增加砝码几呢?


n - w

1 - 3

2 - 1 3

5 - 1 3 9

大胆推测一下(或者接着演算),下一个增加的砝码是 9*3=27


由上面演算可知增加的砝码每次乘以三,

可测量的数字为上一轮的 n+w;


1.jpg

很容易推出公式

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <cstdio>
#include <map>

#define LEN(array) ((sizeof(array)) / (sizeof(array[0])))
#define mo 1e9 + 7
#define ll long long int

using namespace std;

int main(void){
    int n;
    cin >> n;
    int weight = 1;
    int count = 1;
    int total = 1;
    while (total < n){
        count++;
        weight *= 3;
        total += weight;
       
    }
    cout << count << endl;
    return 0;

}


 

0.0分

26 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

厉害
2022-03-30 21:33:20
看湿了
2022-03-27 18:28:30
大佬
2022-03-14 22:52:13
很厉害
2022-02-22 08:27:51
厉害
2022-01-13 15:52:24
  • «
  • 1
  • »