杨老师的游戏(牛客网+新大校赛)

题意:***×***   -   ****            (其中*为1~9的数字不重复)

  这题用到了next_permutation(a,a+9)        9!的复杂度。

刚才CSDN保存并发布时给我砍了一大半(怒)。。。害我要重写。。。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1)
            ans=ans*a;
        a*=a;
        b>>=1;
    }
    return ans;
}
int main()
{
    ll n,ans=0;
    scanf("%lld",&n);
    char a[10]={"123456789"};
    do{
        ll t1,t2,t3,tmp;
        sscanf(a,"%lld",&tmp);
        for(int i=1;i<9;i++){
            for(int j=i+1;j<9;j++){
                t2=(tmp)%(qpow(10,9-i));
                t3=(t2)%(qpow(10,(9-j)));
                t2=(t2)/(qpow(10,(9-j)));
                t1=tmp/(qpow(10,(9-i)));
                if(t1*t2-t3==n){
                    ans++;
                }
            }
        }
    }while(next_permutation(a,a+9));
    printf("%lld\n",ans);
    return 0;
}

其中还有GG-->Boy的代码实现:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long int n,x1=0,x2=0,x3=0,ans=0;
    int a[9]={1,2,3,4,5,6,7,8,9};
    scanf("%ld",&n);
    do
    {
        x1=0;
        for(int i=0;i<9;i++)
        {
            x1=x1*10+a[i];x2=0;
            for(int j=i+1;j<9;j++)
            {
                x2=x2*10+a[j];x3=0;
                for(int k=j+1;k<9;k++)
                {
                    x3=x3*10+a[k];
                     
                }
                if(n==x1*x2-x3)
                    ans++;
                //printf("%ld %ld %ld\n",x1,x2,x3);
            }
        }
    }while(next_permutation(a,a+9));
    printf("%ld\n",ans);
    return 0;  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值