题解

69 条题解

  • 12
    @ 2018-09-03 18:48:31

    #include<iostream>
    using namespace std;

    int main()
    {
    cout<<"192 "<<"384 "<<"576"<<endl;
    cout<<"219 "<<"438 "<<"657"<<endl;
    cout<<"273 "<<"546 "<<"819"<<endl;
    cout<<"327 "<<"654 "<<"981"<<endl;
    return 0;
    }

  • 5
    @ 2017-03-12 08:56:53

    题解:
    如果顺着题目枚举的话很容易爆;
    所以要反过来想数据之间的关系;
    三个数一定是1:2:3的;
    那么我们只要循环一个基数作为3者公因数就OK,只需循环100~334;
    时间效率prrety高。
    主程序:
    pan判断函数就不贴了

    for i:= 100 to 334 do
    begin
    a1:=1*i; a2:=2*i; a3:=3*i;
    if pan then writeln(a1,' ',a2,' ',a3);
    end;

  • 3
    @ 2018-09-02 18:30:27

    致小白

    #include <iostream>
    using namespace std;
    int main()
    { 
       int x,a,a1,a2,a3,b,b1,b2,b3,c,c1,c2,c3;
       for(x=123;x<=333;x++) 
       {
       a=x;b=x*2;c=x*3;
       a1=a%10;
       a2=a/10%10;
       a3=a/100;
       b1=b%10;
       b2=b/10%10;
       b3=b/100;
       c1=c%10;
       c2=c/10%10;
       c3=c/100;
       if(a1*a2*a3*b1*b2*b3*c1*c2*c3==362880)
        cout<<a<<' '<<b<<' '<<c<<endl;
      }
    return 0;
    }
    
  • 3
    @ 2018-06-28 18:40:03

    这里提供一个码量较大的代码(可能是我没有圧行)

    思路:

    判断三个数字中是不是只含1~9

    只要排下序再一个个比对就可以啦QAQ

    AC code:

    #include<iostream>
    using namespace std;
    int main(){
        int i,j,k,h,s;
        int a[10];
        for(i=123;i*3<=987;i++){
            for(h=1;h<10;h++)a[h]=0;
            a[i/100]=1;
            a[i/10%10]=1;
            a[i%10]=1;
            j=i*2;
            a[j/100]=1;
            a[j/10%10]=1;
            a[j%10]=1;
            k=i*3;
            a[k/100]=1;
            a[k/10%10]=1;
            a[k%10]=1;
            for(s=0,h=1;h<10;h++)s=s+a[h];
            if(s==9)cout<<i<<" "<<j<<" "<<k<<endl;
        }
        return 0;
    }
    
    
    
  • 2
    @ 2018-05-25 17:42:44

    看了好多方法都比较麻烦,尤其是判断语句过于复杂,这里介绍一种比较简单的java实现

    import java.util.HashSet;
    import java.util.Set;
    
    public class Main {
    
        public static void main(String[] args) {
            Set set = new HashSet<>();
            //遍历第一个数(只需要遍历123-334即可)
            for (int i = 123; i <= 334; i++) {
                    int i2=2*i;
                    int i3=3*i;
                    String string = ""+i+i2+i3;//将3个数转换为一个字符串
                    char[] c=string.toCharArray();//将字符串转为字符数组
                    for (int j = 0; j < c.length; j++) {
                        //将不等于0的字符存放入set集合中
                        if (c[j]!='0') {
                            set.add(c[j]);
                        }else {
                            break;
                        }
                    }
                    //利用set集合不重复的原理,如果set集合长度等于9说明9个数字皆不重复,打印结果
                    if (set.size()==9) {
                        System.out.println(i+" "+i2+" "+i3);
                    }
                    //清除set集合
                    set.clear();
            }
        }
    
    }
    
    
  • 2
    @ 2017-07-22 11:27:28

    python 4行代码
    python
    for n in range(123, 330):
    s = str(n) + ' ' + str(2*n) + ' ' + str(3*n)
    if '1' in s and '2' in s and '3' in s and '4' in s and '5' in s and '6' in s and '7' in s and '8' in s and '9' in s:
    print s

  • 1
    @ 2019-07-26 18:21:03

    暴力枚举法:

    #include<iostream>
    using namespace std;
    int a[350],b[350],c[350];
    void f(int x,int y,int z);
    int main()
    {
        int n=100,t=1;
        while(n*3<1000)
        {
            a[t]=n*1;
            b[t]=n*2;
            c[t]=n*3;
            t++;
            n++;
        }
        for(int i=1;i<=n;i++)
          f(a[i],b[i],c[i]);
        return 0;
    }
    void f(int x,int y,int z)
    {
        int j[10]={0,0,0,0,0,0,0,0,0,0};
        int x1=x,y1=y,z1=z;
        for(int i=1;i<=3;i++)
        {
            j[x1%10]++;j[y1%10]++;j[z1%10]++;
            x1=(x1-x1%10)/10;
            y1=(y1-y1%10)/10;
            z1=(z1-z1%10)/10;
        }
        for(int i=1;i<=9;i++)
              if(j[i]>1||j[i]==0)  return;
        cout<<x<<" "<<y<<" "<<z<<endl;
    }
    
  • 1
    @ 2019-02-04 09:42:52

    利用集合做的

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <set>
    using namespace std;
    int a,b,c,i,j,k,i1,i2,j1,j2,k1,k2;
    void insertAll(set<int> &s){
        s.insert(i),s.insert(j),s.insert(k);
        s.insert(i1),s.insert(j1),s.insert(k1);
        s.insert(i2),s.insert(j2),s.insert(k2);
    }
    int main(){
        for(i=1; i<4; i++){
            for(j=1;j<=9;j++){
                for(k=1;k<=9;k++){
                    if(k!=i && k!=j && i!=j){
                        set<int> s;
                        a = i*100+j*10+k;
                        b = a*2; c = a*3;
                        if(c>=1000) break;
                        i1 = b/100; j1 = b%100/10; k1 = b%10;
                        i2 = c/100; j2 = c%100/10; k2 = c%10;
                        insertAll(s);
                        if(s.size()==9&&!s.count(0)){
                            printf("%d %d %d\n",a,b,c);
                        }
                    }
                }
            }
        }
    }
    
  • 1
    @ 2018-09-02 18:29:48

    致小白

  • 1
    @ 2018-07-10 17:36:55

    直接用C++的全排列函数next_permutation,然后超过333不可能直接跳出

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        do{
    //      for(int i = 0; i < 9; ++i) printf("%d ", arr[i]);
    //      printf("\n");
    
            int num[3];
            for(int i = 0;i < 3; ++i)
            {
                num[i] = arr[3 * i] * 100 + arr[3 * i + 1] * 10 + arr[3 * i + 2]; 
            }
            
            if(num[0] > 333) break;
            
            if(num[0] * 2 == num[1] && num[0] * 3 == num[2]) printf("%d %d %d\n", num[0], num[1], num[2]);
        }while(next_permutation(arr, arr + 9));
        return 0;
    }
    
  • 1
    @ 2018-04-02 13:15:56

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
    int r;
    for (int i=123;i<=987;i++)
    {
    if ((i%10!=i/100)&&(i%10!=(i-i/100*100)/10)&&(i/100!=(i-i/100*100)/10))
    {
    for (int j=2*i;j<=987;j+=i)
    {
    r=i*3;
    if (r<=987&&r==j*1.5&&(j%10!=j/100)&&(j%10!=(j-j/100*100)/10)&&(j/100!=(j-j/100*100)/10)&&(r%10!=r/100)&&(r%10!=(r-r/100*100)/10)&&(r/100!=(r-r/100*100)/10)&&j/100!=i/100&&j/100!=i%10&&j/100!=(i-i/100*100)/10&&r/100!=i/100&&r/100!=i%10&&r/100!=(i-i/100*100)/10&&j/100!=r/100&&j/100!=r%10&&j/100!=(r-r/100*100)/10&&j%10!=i/100&&j%10!=i%10&&j%10!=(i-i/100*100)/10&&r%10!=i/100&&r%10!=i%10&&r%10!=(i-i/100*100)/10&&j%10!=r/100&&j%10!=r%10&&j%10!=(r-r/100*100)/10&&(j-j/100*100)/10!=r/100&&(j-j/100*100)/10!=r%10&&(j-j/100*100)/10!=(r-r/100*100)/10&&(j-j/100*100)/10!=i/100&&(j-j/100*100)/10!=i%10&&(j-j/100*100)/10!=(i-i/100*100)/10&&j%10!=0&&j/100!=0&&(j-j/100*100)/10!=0&&r%10!=0&&r/100!=0&&(r-r/100*100)/10!=0&&i%10!=0&&i/100!=0&&(i-i/100*100)/10!=0)
    {
    cout<<i<<" "<<j<<" "<<r<<endl;
    }
    }
    }
    }
    return 0;
    }
    if语句中,全为判断

  • 1
    @ 2018-02-26 21:46:09
    #include<bits/stdc++.h>
    int main(){
        char s[10];
        bool flag[10]={0},flag2=0;
        for(int i=123;i<=329;++i){
            sprintf(s+1,"%d%d%d",i,i*2,i*3);
            flag[0]=1;//确保0不出现
            for(int j=1;j<=9;++j) if(1^flag[s[j]-'0']) flag[s[j]-'0']=1;else flag2=1;//用异或保证每个数字只出现一次
            if(!(flag2)) printf("%d %d %d\n",i,i*2,i*3);
            memset(flag,0,sizeof(flag));flag2=0;
        }
    }
    
  • 1
    @ 2018-02-09 22:36:34

    java来一波,感觉想复杂了。

    public class FillNumber {
    
        public static void main(String[] args) {
            for (int i = 100; i <= 334; i ++) {
                int num1 = i;
                int num2 = i * 2;
                int num3 = i * 3;
                if(isConform(num1,num2,num3)) {
                    System.out.println(num1 + " " + num2 + " " + num3);
                }
            }
            
    
        }
        public static boolean isConform(int a, int b, int c) {
            int[] arr = new int[9];
            boolean[] check = new boolean[9];
            for (int i = 0; i < 9; i ++) {
                arr[i] = i + 1;
                check[i] = true;
            }
            int a1 = a % 10; int a2 = (a / 10) % 10; int a3 = a / 100;
            int b1 = b % 10; int b2 = (b / 10) % 10; int b3 = b / 100;
            int c1 = c % 10; int c2 = (c / 10) % 10; int c3 = c / 100;
            for (int i = 0; i < arr.length; i ++) {
                if (check[i]) {
                    if (arr[i] == a1 || arr[i] == a2 || arr[i] == a3) {
                        check[i] = false;
                        continue;
                    }
                    if (arr[i] == b1 || arr[i] == b2 || arr[i] == b3) {
                        check[i] = false;
                        continue;
                    }
                    if (arr[i] == c1 || arr[i] == c2 || arr[i] == c3) {
                        check[i] = false;
                        continue;
                    }
                }
            }
            boolean flag = true;
            for (int i = 0; i < check.length; i ++) {
                if (check[i]) {
                    flag = false;
                }
            }
            if(flag) {
                return true;
            }
            return false;
        }   
    }
    
  • 1
    @ 2018-01-02 15:35:29

    #include <stdio.h>
    int main() {
    int min,i,j,flag;
    char all[10];
    for(min = 102; min <=329; min++) {
    sprintf(all,"%d%d%d",min,min*2,min*3);
    flag = 0;
    for(j = 1; j <= 9; j++) {
    for(i = 0; i <=8; i++) {
    if(all[i] == j+'0') {
    flag++;
    break;
    }
    }
    }
    //如果1到9都在字符串出现
    if(flag == 9) {
    printf("%d %d %d\n",min,2*min,3*min);
    }
    }
    return 0;
    }

  • 1
    @ 2017-08-22 19:42:49
    var f:array[1..9]of boolean;i:integer;
    procedure p(a:integer);
    begin
         f[a mod 10]:=false;
         f[a div 10 mod 10]:=false;
         f[a div 100]:=false;
    end;
    function pd(b:integer):boolean;
    var g,s,ba:integer;
    begin
        g:=b mod 10;s:=b div 10 mod 10;ba:=b div 100;
        if (g=s)or(g=ba)or(ba=s)or(f[g]=false)or(f[s]=false)or(f[ba]=false)then exit(true)
        else begin
             p(b);
             exit(false);
        end;
    end;
    begin
       for i:=102 to 329 do begin
           fillchar(f,sizeof(f),true);
           if pd(i) then continue;
           if pd(i*2) then continue;
           if pd(i*3) then continue;
           writeln(i,' ',i*2,' ',i*3);
       end;
    end.
    
    
  • 0
    @ 2019-08-23 16:42:33

    这题 最暴力的算法是:

    枚举每个位数上的所有可能。时间复杂度为\(9^9‬=‭387420489‬\),这显然不行,中间的剪枝加了可能优化为\(9!\)能过。

    但是,我们完全可以枚举第一个数。出于重复的考虑,第一个数在\(123\)和\(333\)之间。然后算出第二、三个数,一波验证即可。

    #include<bits/stdc++.h>
    using namespace std;
    
    inline bool check(int x,int y,int z) {
        int h[10]={0};
        while(x) h[x%10]++,x/=10;
        while(y) h[y%10]++,y/=10;
        while(z) h[z%10]++,z/=10;
        for(int i=1;i<=9;i++) if(h[i]!=1) return 0;
        return 1;
    }
    
    int main(){
        for(int i=123;i<=333;i++)
            if(check(i,2*i,3*i)) printf("%d %d %d\n",i,2*i,3*i);
        return 0;
    }
    
    

    给个赞!

  • 0
    @ 2019-08-01 21:53:44

    ##python
    ##验证的难度往往小于求解!!

    for i in range(123, 329):
    num_1 = i
    num_2 = 2 * i
    num_3 = 3 * i
    str_123 = str(num_1) + str(num_2) + str(num_3)
    set_123 = set(str_123)
    list_123 = list(set_123)
    if (len(list_123) == 9) and ('0' not in list_123):
    out_put = str(num_1) + ' ' + str(num_2) + ' ' + str(num_3)
    print(out_put)

  • 0
    @ 2019-05-21 13:22:48

    #include<iostream>
    using namespace std;
    int main(){
    cout<<"192 "<<"384 "<<"576"<<endl<<"219 "<<"438 "<<"657"<<endl<<"273 "<<"546 "<<"819"<<endl<<"327 "<<"654 "<<"981"<<endl;
    return 0;
    }

  • 0
    @ 2019-03-07 22:02:10

    ~~你们太菜啦~~
    #include<iostream>
    #include<cstring>
    using namespace std;
    bool used[10];
    int main(){
    for(int i = 100; i <= 999; i ++){
    int o = i * 1, oo = o;
    int j = i * 2, p = j;
    int k = i * 3, q = k;
    memset(used, false, sizeof(used));
    bool flag = false;
    while(o != 0){
    int s = o % 10;
    if(!used[s] && s != 0) used[s] = true;
    else {
    flag = true;
    break;
    }
    o /= 10;
    }
    if(flag) continue;
    flag = false;
    while(j != 0){
    int s = j % 10;
    if(!used[s] && s != 0) used[s] = true;
    else {
    flag = true;
    break;
    }
    j /= 10;
    }
    if(flag) continue;
    flag = false;
    while(k != 0){
    int s = k % 10;
    if(!used[s] && s != 0) used[s] = true;
    else {
    flag = true;
    break;
    }
    k /= 10;
    }
    if(flag) continue;
    if(p <= 999 && q <= 999){
    cout << oo << ' ' << p << ' ' << q << endl;
    }
    }
    return 0;
    }

  • 0
    @ 2018-08-27 15:56:55

    #include<cstdio>
    using namespace std;
    int a[10],book[10],n1,n2,n3;
    void output(){
    n1=a[1]*100+a[2]*10+a[3];
    n2=a[4]*100+a[5]*10+a[6];
    n3=a[7]*100+a[8]*10+a[9];
    if(n1*2==n2&&n1*3==n3)
    printf("%d %d %d\n",n1,n2,n3);
    return;
    }
    void dfs(int step){
    int i;
    if(step>9)output();
    for(i=1;i<=9;i++){
    if(book[i]==0){
    book[i]=1;
    a[step]=i;
    dfs(step+1);
    book[i]=0;
    }
    }
    }
    int main(){
    dfs(1);
    return 0;
    }

信息

ID
1772
难度
2
分类
搜索 点击显示
标签
递交数
2134
已通过
993
通过率
47%
上传者