救救80分的孩子吧???

P1618 三连击(升级版)

weiyi8910 @ 2024-08-22 21:31:07

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a,b,c,v=0;
    int A,B,C;
    cin>>A>>B>>C;
    if(A==0||B==0||C==0){
        cout<<"No!!!";
    }
    for(a = 123; a<=1000; a++) {
        b=a/A*B;
        c=a/A*C;
        if((a/100+a/10%10+a%10+b/100+b/10%10+b%10+c/100+c/10%10+c%10==1+2+3+4+5+6+7+8+9)&&((a/100)*(a/10%10)*(a%10)*(b/100)*(b/10%10)*(b%10)*(c/100)*(c/10%10)*(c%10)==1*2*3*4*5*6*7*8*9)) {
            v=1;
            cout<<a<<" "<<b<<" "<<c<<endl;
        }
    }
    if(v==0){
        cout<<"No!!!";
    }
    return 0;
}

by szrgjxms @ 2024-08-22 21:45:58

它要求比例是 a:b:c 所以存在精度不够的情况,因为 int 是会自动舍去小数点后的数的,改double 试试


by szrgjxms @ 2024-08-22 21:50:32

#include<bits/stdc++.h>
using namespace std;
int a,b,c,yi,er,san;
int e[10];
double v,r,t; 
int you=0;
int main()
{
    cin>>a>>b>>c;
    for(int i=1;i<=9;i++)
        e[i]=i;
    do{
        yi=e[1]*100+e[2]*10+e[3];
        er=e[4]*100+e[5]*10+e[6];
        san=e[7]*100+e[8]*10+e[9];
        v=yi*1.0/a;//就在这里要用double不然精度不够会多判
        r=er*1.0/b;
        t=san*1.0/c;
        if(v==r&&r==t)
        {
            printf("%d %d %d\n",yi,er,san);
            you=1;
        }
    }while(next_permutation(e+1,e+10));
    if(you==0)
        cout<<"No!!!";
    return 0;
}

这是我的做法,next_permutation() 是 STL 里面的一个全排列函数。


by weiyi8910 @ 2024-08-22 21:50:36

解决了谢谢


by szrgjxms @ 2024-08-22 21:51:03

ok


by weiyi8910 @ 2024-08-22 21:51:32

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a,b,c,v=0;
    int A,B,C;
    cin>>A>>B>>C;
    if(A==0||B==0||C==0) {
        cout<<"No!!!";
        return 0;
    }
    for(a = 0; a<=1000000; a++) {
        b=a/1.0/A*B;
        c=a/1.0/A*C;
        if((a/100+a/10%10+a%10+b/100+b/10%10+b%10+c/100+c/10%10+c%10==1+2+3+4+5+6+7+8+9)&&((a/100)*(a/10%10)*(a%10)*(b/100)*(b/10%10)*(b%10)*(c/100)*(c/10%10)*(c%10)==1*2*3*4*5*6*7*8*9)) {
            v=1;
            cout<<a<<" "<<b<<" "<<c<<endl;
        }
    }
    if(v==0) {
        cout<<"No!!!";
    }
    return 0;
}

by shtian @ 2024-08-23 12:02:28

@weiyi8910 你的代码

(a / 100 + a / 10 % 10 + a % 10 + b / 100 + b / 10 % 10 + b % 10 + c / 100 + c / 10 % 10 + c % 10 == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) && ((a / 100) (a / 10 % 10) (a % 10) (b / 100) (b / 10 % 10) (b % 10) (c / 100) (c / 10 % 10) (c % 10) == 1 2 3 4 5 6 7 8 9)

并不能判定是1~9之间的数


by shtian @ 2024-08-23 12:03:20

154294794就不行


by shtian @ 2024-08-23 12:09:00

@weiyi8910

#include <bits/stdc++.h>
using namespace std;
bool ok[100];

void f(int a) {
    ok[a / 100] = ok[a / 10 % 10] = ok[a % 10] = 1;
}

int main() {
    int a, b, c, v = 0;
    int A, B, C;
    cin >> A >> B >> C;
    if (A == 0 || B == 0 || C == 0) {
        cout << "No!!!";
        return 0;
    }
    for (a = 0; a < 1000; a++) {////////////////////
        b = a / 1.0 / A * B;
        c = a / 1.0 / A * C;
        memset(ok, 0, sizeof(ok));
        f(a);
        f(b);
        f(c);
        if (ok[1] && ok[2] && ok[3] && ok[4] && ok[5] && ok[6] && ok[7] && ok[8] && ok[9]) {//////////////////////////////////
            v = 1;
            cout << a << " " << b << " " << c << endl;
        }
    }
    if (v == 0) {
        cout << "No!!!";
    }
    return 0;
}

by weiyi8910 @ 2024-08-23 19:30:33

人才


by weiyi8910 @ 2024-08-23 19:31:29

不愧是我孙哥


|