紫书第七章-----暴力求解法(简单枚举之例题7-1,7-2,7-3)

【例题7-1 Division UVA - 725 】

此题害我不浅,一道水题硬是坑我浪费几个小时时间!代码如下:

【代码一(超时)】

#include<iostream>

using namespace std;

int n;
bool flag;

void solve(){
    flag=0;
    for(int a=0;a<=9;a++){
        for(int b=0;b<=9;b++){
            if(b==a) continue;
            for(int c=0;c<=9;c++){
                if(c==b || c==a) continue;
                for(int d=0;d<=9;d++){
                    if(d==a || d==b || d==c) continue;
                    for(int e=0;e<=9;e++){
                        if(e==a || e==b || e==c || e==d) continue;
                        for(int f=0;f<=9;f++){
                            if(f==a || f==b || f==c || f==d || f==e) continue;
                            for(int g=0;g<=9;g++){
                                if(g==a || g==b || g==c || g==d || g==e || g==f) continue;
                                for(int h=0;h<=9;h++){
                                    if(h==a || h==b || h==c || h==d || h==e || h==f || h==g) continue;
                                    for(int i=0;i<=9;i++){
                                        if(i==a || i==b || i==c || i==d || i==e || i==f || i==g || i==h) continue;

                                        for(int j=0;j<=9;j++){
                                            if(j==a || j==b || j==c || j==d || j==e || j==f || j==g || j==h || j==i) continue;
                                            int x=a*10000+b*1000+c*100+d*10+e;
                                            int y=f*10000+g*1000+h*100+i*10+j;
                                            if(x==y*n){
                                                flag=1;
                                                cout<<a<<b<<c<<d<<e<<" / "<<f<<g<<h<<i<<j<<" = "<<n<<endl;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if(!flag) cout<<"There are no solutions for "<<n<<"."<<endl;
}

int main()
{
    int first=1;
    while(cin>>n){
        if(n==0) break;
        if(first) {first=0;solve();}
        else{
            cout<<endl;
            solve();
        }
    }
    return 0;
}

【代码二(accepted)】

#include<iostream>

using namespace std;

int n;
bool flag;
int first=1;

int arr[10];

bool not_equal(int a[],int n){
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(a[j]==a[i]) return false;
        }
    }
    return true;
}

void solve(){
    if(first) first=0;
    else cout<<endl;
    flag=0;
    for(int a=0;a<=9;a++){
        for(int b=0;b<=9;b++){
            for(int c=0;c<=9;c++){
                for(int d=0;d<=9;d++){
                    for(int e=0;e<=9;e++){
                        arr[0]=a;arr[1]=b;arr[2]=c;arr[3]=d;arr[4]=e;
                        if(not_equal(arr,5)){
                            int x=a*10000+b*1000+c*100+d*10+e;
                            int y=x*n;
                            arr[5]=y%10,arr[6]=y%100/10,arr[7]=y%1000/100,arr[8]=y%10000/1000;arr[9]=y/10000;
                            if(not_equal(arr,10) && y>=10000 && y<=99999){
                                flag=1;
                                for(int i=9;i>=5;i--) cout<<arr[i];
                                cout<<" / ";
                                for(int i=0;i<5;i++) cout<<arr[i];
                                cout<<" = "<<n<<endl;
                            }
                        }

                    }
                }
            }
        }
    }
    if(!flag) cout<<"There are no solutions for "<<n<<"."<<endl;
}

int main()
{
    while(cin>>n){
        if(n==0) break;
        solve();
    }
    return 0;
}

【例题7-2 Maximum Product UVA - 11059】

思路:枚举连续子序列的长度

#include<iostream>
#include<algorithm>

using namespace std;

long long int a[20];
int n;
int kase=0;

void solve(){
    while(cin>>n){
        for(int i=0;i<n;i++) cin>>a[i];
        long long int mmax=0;
        for(int i=1;i<=n;i++){  //枚举连续子序列的长度
            for(int j=0;j<=n-i;j++){    //枚举连续子序列的起点
                long long int t=1;
                for(int k=j;k<j+i;k++){ //累乘
                    t*=a[k];
                }
                mmax=max(mmax,t);
            }
        }
        cout<<"Case #"<<++kase<<": The maximum product is "<<mmax<<"."<<endl<<endl;
    }
}

int main()
{
    solve();
    return 0;
}

【例题7-3 Fractions Again?! UVA - 10976 】

#include<iostream>
#include<algorithm>

using namespace std;

const int maxn=20000+5;

typedef struct Node{
    int x,y;
    Node(int xx=0,int yy=0):x(xx),y(yy){}
}node;

int k,x,y;
node a[maxn];
int cnt;

void solve(){
    while(cin>>k){
        cnt=0;
        for(int y=1;y<=20000;y++){
            if(y>k && (y*k)%(y-k)==0){
                int x=(y*k)/(y-k);
                if(x>=y)
                    a[cnt++]=node(x,y);
            }
        }
        cout<<cnt<<endl;
        for(int i=0;i<cnt;i++){
            cout<<1<<"/"<<k<<" = "<<1<<"/"<<a[i].x<<" + "<<1<<"/"<<a[i].y<<endl;
        }
    }
}

int main()
{
    solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值