当前位置: 移动技术网 > 移动技术>移动开发>IOS > 思维入门2

思维入门2

2020年07月09日  | 移动技术网移动技术  | 我要评论

写出规律发现是(n+1)/2

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int n,t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        cout<<(n+1>>1)<<endl;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int t,x,y,n;
    cin>>t;
    while(t--)
    {
        int sum=0;
        cin>>x>>y>>n;
        int a=n/x;
        int b=n%x;
        if(b<y)
            sum=(a-1)*x+y;
        else
            sum=a*x+y;
        cout<<sum<<endl;
    }
    return 0;
}

题意:
给你一个数,现在你可以选择乘2或者除6(前提是能被6整除),现在问你最少需要多少次能使得操作后的数等于1?如果不可能输出-1.

【思路】

直接分解因子2和3,看它能被多少个2和3乘起来,最后2的个数要小于3的个数,因为3多了可以用2去乘等于6,2多了就没办法了,而且最后的n要等于1,否则输出-1.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int a=0,b=0;
        while(n%2==0)
        {
            n/=2;
            a++;
        }
        while(n%3==0)
        {
            n/=3;
            b++;
        }
        if(a>b||n!=1)
            cout<<-1<<endl;
        else
            cout<<b*2-a<<endl;
    }
    return 0;
}

 

本文地址:https://blog.csdn.net/qq_43956340/article/details/107216715

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网