用户登录
用户注册

分享至

思维入门2

  • 作者: 啊一一长城
  • 来源: 51数据库
  • 2021-09-03

A

写出规律发现是(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;
}

B

#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;
}

C

题意:
给你一个数,现在你可以选择乘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;
}

?

软件
前端设计
程序设计
Java相关