洛谷 暴力 简化循环

P2089 烤鸡

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    int res=0;
    cin>>x;
    //时间复杂度可以支持暴力破解
    for(int a=1; a<=3; a++)
        for(int b=1; b<=3; b++)
            for(int c=1; c<=3; c++)
                for(int k=1; k<=3; k++)
                    for(int d=1; d<=3; d++)
                        for(int e=1; e<=3; e++)
                            for(int f=1; f<=3; f++)
                                for(int g=1; g<=3; g++)
                                    for(int h=1; h<=3; h++)
                                        for(int i=1; i<=3; i++)
                                        {
                                            if(a+b+c+d+e+f+g+h+i+k==x)
                                                res++;
                                        }
    cout<<res<<endl;
    for(int a=1; a<=3; a++)
        for(int b=1; b<=3; b++)
            for(int c=1; c<=3; c++)
                for(int k=1; k<=3; k++)
                    for(int d=1; d<=3; d++)
                        for(int e=1; e<=3; e++)
                       /     for(int f=1; f<=3; f++)
                                for(int g=1; g<=3; g++)
                                    for(int h=1; h<=3; h++)
                                        for(int i=1; i<=3; i++)
                                        {
                                            if(a+b+c+d+e+f+g+h+i+k==x)
                                                printf("%d %d %d %d %d %d %d %d %d %d\n",a,b,c,k,d,e,f,g,h,i);
                                        }
    return 0;
}

P1618 三连击(升级版)

#include <bits/stdc++.h>
using namespace std;
struct res
{
    int num1,num2,num3;
} x[10000];
int main()
{
    int a,b,c;
    cin>>a>>b>>c;
    int j,k;
    int t=0;
    int y[12];
    for(int i=100; i<=999; i++)
    {
        j=i*b/a;//计算两个数简化两重循环
        k=i*c/a;//计算不能先做除法,除不尽的情况下余数会产生误差
        y[0]=i%10;
        y[1]=(i/10)%10;
        y[2]=i/100;
        y[3]=j%10;
        y[4]=(j/10)%10;
        y[5]=j/100;
        y[6]=k%10;
        y[7]=(k/10)%10;
        y[8]=k/100;
        int f=0;
        for(int a=0; a<=8; a++)
            for(int b=a+1; b<=8; b++)
                if((y[a]==y[b])||y[a]==0||y[b]==0)
                {
                    f=1;
                    break;
                }
        if(j>=100&&j<=999&&k>=100&&k<=999&&f==0)
        {
            x[t].num1=i;
            x[t].num2=j;
            x[t].num3=k;
            t++;
        }
    }
    if(t!=0)
    {
        for(int i=0; i<t; i++)
        {
            printf("%d %d %d\n",x[i].num1,x[i].num2,x[i].num3);
        }
    }
    else
        printf("No!!!\n");
    return 0;
}

P1426 小鱼会有危险吗

#include <bits/stdc++.h>
using namespace std;
int main()
{
    double x,s;
    cin>>s>>x;
    double v=7.0;
    double d=s-x;
    if(x==0)
        printf("n");
    else
    {
        while(d>=0)
        {
            d=d-v;
            v=v*0.98;
        }
        if(d+2*x<=v*0.98)//注意循环结束时的v能否直接用于下面的比较
            printf("n");
        else
            printf("y");
    }
    return 0;
}

P1014 Cantor表

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    scanf("%d",&x);
    int i;
    for(i=1;; i++)
    {
        x=x-i;
        if(x<=0)
            break;
    }
    x=x+i;
    //注意看题,奇数和偶数行开头元素的累加方式不同
    if(i%2==0)
        printf("%d/%d",x,i-x+1);
    else
        printf("%d/%d",i-x+1,x);
    return 0;
}

P1307 数字反转

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int x;
    cin>>x;
    if(x<0)
    {
        x=-x;
        printf("-");
    }
    int a[12]= {0};
    int t=0;
    while(x!=0)
    {
        a[t]=x%10;
        x=x/10;
        t++;
    }
    int f=0;
    for(int i=0; i<t; i++)
        if(a[i]!=0)
        {
            f=i;
            break;
        }
    for(int i=f; i<t; i++)
        cout<<a[i];
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值