7989买砝码

7989买砝码
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description

7989学长热爱做实验,为了实验,她要精确的知道物品的质量,天平的任意一边都能放置砝码,她想自己设计一套砝码,每一个砝码的质量都是正整数 (质量由你来定,保证每个砝码的质量<1e5),她想测出n(n<1e5)以内所有正整数的质量,众所周知7989学长是一个懒的学生,她想要知道最少需要的砝码个数m。
Input

输入n 。
Output

输出m
Sample Input

4

Sample Output

2

Hint

样例解释: 选则 质量为分别为1,3砝码各一个 1=1 ,2=3-1 ,3=3,4=1+3。
Source
QYN

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;

    int d=0;
    int m=0;
    if(n==0)
    {
        cout<<0<<endl;
    }
    else
    {
        for(int i=0; i<1000; i++)
        {
            d+=pow(3,i);
            if(d<n)
                m++;
            else
            {
                break;
            }
        }
        cout<<m+1<<endl;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;

    int d=0;
    int m=0;
   
   
        for(int i=0; i<1000; i++)
        {
            d+=pow(3,i);
            if(d<n)
                m++;
            else
            {
                break;
            }
        }
        cout<<m+1<<endl;
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43820242/article/details/88082025
今日推荐