19 条题解

  • 0
    @ 2025-1-24 23:16:59

    MXOJ B1 三连击

    思路

    这题的数据范围极小,暴力就能过。但是有几个坑点:

    1. 99 个数用且仅用一次。
    2. 答案从小到大排列。

    然后我们枚举第一个数,再通过比例求出后两个数,判断合法即可。

    代码里有详细注释。

    代码

    #include <iostream>
    using namespace std;
    
    bool func(int x) // 判断每个数用且仅用一次
    {
        bool used[15] = {};
        int i;
        while (x) // 分解数位
        {
            used[x % 10] = true;
            x /= 10;
        }
        for (i = 1; i <= 9; i++)
        {
            if (!used[i])
            {
                return false;
            }
        }
        return true;
    }
    
    int main()
    {
        int i, j, k, tmp;
        for (i = 123; i <= 333; i++) // 要求每个数用且仅用一次,所以从 123 开始枚举,而且有三个数,第一个数最大 333
        {
            j = i * 2; // 第二个数
            k = i * 3; // 第三个数
            tmp = i * 1000000 + j * 1000 + k; // 三个数连接起来,方便判断
            if (func(tmp))
            {
                cout << i << " " << j << " " << k << endl;
            }
        }
        return 0;
    }
    

    交上去,不出意外的 WA 了。因为这是一道提交答案题,把代码复制到本地,再将运行结果输出即可。

    如果你懒得这么做,也可以去看其他人的题解。

    • 0
      @ 2024-9-28 16:45:10

      代码题解

      思路

      直接暴力,这个数据范围怕毛线

      解题方法

      如思路,暴力。

      复杂度

      时间复杂度:

      O(n3)O(n^3)

      Code

      注:由于是答案题所以马蜂有点随意,不成问题。

      #include<bits/stdc++.h>
      using namespace std;
      
      bool usd[10];
      
      int main(){
      	for(int i=1;i<=9;i++){
      		usd[i]=true;
      		for(int j=1;j<=9;j++){
      			if(usd[j])continue;
      			usd[j]=true;
      			for(int k=1;k<=9;k++){
      				if(usd[k])continue;
      				usd[k]=true;
      				int er=2*(i*100+j*10+k);
      				if(er<1000&&er/100>0&&er/10%10>0&&er%10>0){
      					if(!usd[er/100]){
      						usd[er/100]=true;
      						if(!usd[er/10%10]){
      							usd[er/10%10]=true;
      							if(!usd[er%10]){
      								usd[er%10]=true;
      								int san=3*(i*100+j*10+k);
      								if(san<1000&&san/100>0&&san/10%10>0&&san%10>0){
      									if(!usd[san/100]){
      										usd[san/100]=true;
      										if(!usd[san/10%10]){
      											usd[san/10%10]=true;
      											if(!usd[san%10]){
      												cout<<i<<j<<k<<" ";
      												cout<<er<<" "<<san<<endl;
      											}
      											usd[san/10%10]=false;
      										}
      										usd[san/100]=false;
      									}
      								}
      								usd[er%10]=false;
      							}
      							usd[er/10%10]=false;
      						}
      						usd[er/100]=false;
      					}
      				}
      				usd[k]=false;
      			}
      			usd[j]=false;
      		}
      		usd[i]=false;
      	}
      	return 0;
      } 
      
      • 0
        @ 2024-9-15 7:41:37

        B1题解

        题目:

        将1,2,…,9共9个数分成三组,分别组成三个三位数;
        且使这三个三位数构成1:2:3的比例;
        试求出所有满足条件的三个三位数。
        例如:三个三位数:
        192、384、576
        满足以上条件。
        

        相信大家跟我一样,读完题先是一愣

        当然,由于我是看到了1:2:3的比例; 所以先想到的就是用for循环

        #include<bits/stdc++.h>
        using namespace std;
        int main(){
            for(int i=100;i<=999;i++){
                /*
                这里呢,就是判断一下,是否可以构成1:2:3
                So,只需要这样输出:
                */
                cout<<i<<" "<<i*2<<" "<<i*3<<endl;
            }
            return 0;
        }
        

        然后遇到了问题~~

        哈哈,直接不对!!!

        emm..那就再看看,因为for循环中要用到i*3,所以 我们可以直接让i<=333,但只要仔细一读题,你会发现 题目中的第二的条件:

        用1-9这9个数!!

        有种被骗了的感觉

        ok,所以我们还要排除0 这是个伟大的难点 所以我们可以这样做:

        1. 让for循环的i 初始值为 111 纯粹较少时间,不改也行,好像没有大问题

        2. 也可以将i和i * 2,i * 3这几个数的每一位判断是不是0

        因为给了是个三位数所以

        1. 百位:i/100
        2. 十位:i/10%10
        3. 个位:i%10

        所以得到以下代码:

        #include<bits/stdc++.h>
        using namespace std;
        int main(){
            int one,two,three;
            for(int i=111;i<=333;i++){
                one=i;
                two=i*2;
                three=i*3;
                if((one/100)!=0 && (one/10%10)!=0 && (one%10)!=0){
                    if((two/100)!=0 && (two/10%10)!=0 && (two%10)!=0){
                        if((three/100)!=0 && (three/10%10)!=0 && (three%10)!=0){
                            cout<<one<<" "<<two<<" "<<three<<endl;
                        }
                    }
                }
            }
            return 0;
        }
        

        感觉良好,但是

        过不了,哈哈

        emmmm.... 所以我们再次读题,发现是每个数只能用一次

        这就好做了,只要判断每个数位乘起来是不是9!

        和加起来是不是45(从1+到9)

        所以得到以下代码:

        #include<bits/stdc++.h>
        using namespace std;
        int main(){
            int one,two,three;
            for(int i=111;i<=333;i++){
                one=i;
                two=i*2;
                three=i*3;
                if((one/100)!=0 && (one/10%10)!=0 && (one%10)!=0){
                    if((two/100)!=0 && (two/10%10)!=0 && (two%10)!=0){
                        if((three/100)!=0 && (three/10%10)!=0 && (three%10)!=0){
                            if((one/100) * (one/10%10) * (one%10) * (two/100) * (two/10%10) * (two%10) * (three/100) * (three/10%10) * (three%10) == 1*2*3*4*5*6*7*8*9){
                                if((one/100) + (one/10%10) + (one%10) + (two/100) + (two/10%10) + (two%10) + (three/100) + (three/10%10) + (three%10) == 45){
                                    cout<<one<<" "<<two<<" "<<three<<endl;
                                }
                            }
                        }
                    }
                }
            }
            return 0;
        }
        

        你以为结束了??

        不可能!!!绝对不可能!!!

        这样虽然能过,可是大家有没有想过一个问题:

        既然我们都已判断了三个数每个数的数位相加和相乘 那还有没有必要判断0呢???

        对,没有必要

        1.相乘,如有0,就是0 2.相加如果有,那么就会小于45吧!! 所以

        删了吧!!

        最后的代码如下

        Code

        #include <bits/stdc++.h>
        using namespace std;
        int main(){
            int one, two, three;
            for (int i = 111; i <= 333; i++){
                one = i;
                two = i * 2;
                three = i * 3;
                if ((one / 100) * (one / 10 % 10) * (one % 10) * (two / 100) * (two / 10 % 10) * (two % 10) * (three / 100) * (three / 10 % 10) * (three % 10) == 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9){
                    if ((one / 100) + (one / 10 % 10) + (one % 10) + (two / 100) + (two / 10 % 10) + (two % 10) + (three / 100) + (three / 10 % 10) + (three % 10) == 45){
                        cout << one << " " << two << " " << three << endl;
                    }
                }
            }
            return 0;
        }
        

        最后完结撒花,开心

        热知识

        这个题目,有点... emmmmm..

        • 0
          @ 2024-8-31 11:45:11

          直接python生成1-9全排列的组合,然后验证一下是否满足输出即可.

          Code

          from itertools import permutations
          
          # 生成所有数字的排列
          all = permutations('123456789')
          
          # 结果列表
          results = []
          
          # 遍历所有排列
          for perm in all:
              # 分别取出第一个三位数、第二个三位数和第三个三位数
              num1 = int(''.join(perm[:3]))
              num2 = int(''.join(perm[3:6]))
              num3 = int(''.join(perm[6:]))
              
              # 检查是否满足 1:2:3 的比例
              if num2 == 2 * num1 and num3 == 3 * num1:
                  results.append((num1, num2, num3))
          
          # 按照第一个数从小到大的顺序输出
          results.sort()
          
          # 打印结果
          for result in results:
              print(result[0], result[1], result[2])
          

          最后copy输出结果,提交即可

          192 384 576
          219 438 657
          273 546 819
          327 654 981
          
          • -1
            @ 2024-11-16 15:53:00

            B1.三连击

            思路

            没啥难的,手推即可

            Code

            192 384 576
            219 438 657
            273 546 819
            327 654 981

            • -1
              @ 2024-9-22 16:26:42

              B1.三连击

              思路

              这道题刚拿到就想到使用递归一个数位一个数位去推 。 搞一个函数,参数有h表示推到了第几个数位和a,b,c。 a,b,c按照题意理解就行了。
              但写代码的时候发现如果不优化递归,时间复杂度会变成999^{9},所以用了一个vis数组标记对应的1,2,3......这些数字在前面有没有被用过。
              这样就减少了时间复杂度,又满足了题目中1到9每个数字只能用一次的限制条件。

              Code

              #include<bits/stdc++.h>
              using namespace std;
              bool vis[105];
              void fun(int h,int a,int b,int c)
              {
              	if(h==10)
              	{
              		if(2*a==b && 3*a==c)
              			cout<<a<<" "<<b<<" "<<c<<endl;
              		return;
              	}
              	for(int i=1;i<=9;i++)
              	{
              		if(!vis[i])
              		{
              			vis[i]=1;
              			if(h<=3) 
              				fun(h+1,a*10+i,b,c);
              			else if(h<=6)
              				fun(h+1,a,b*10+i,c);
              			else
              				fun(h+1,a,b,c*10+i);
              			vis[i]=0;
              		}
              	}
              }
              int main()
              {
              	fun(1,0,0,0); 
              	return 0;
               } 
              
              • -1
                @ 2024-9-16 17:19:13

                B1. 三连击

                直接手算即可。

                192 384 576
                219 438 657
                273 546 819
                327 654 981
                
                • -1
                  @ 2024-9-16 14:41:55

                  其实并没有啥难度,手推即可

                  192 384 576
                  219 438 657
                  273 546 819
                  327 654 981
                  
                  • -1
                    @ 2024-9-8 20:14:46

                    B1 三连击题解:

                    思路:直接手算,最后把答案提交上去

                    192 384 576
                    219 438 657
                    273 546 819
                    327 654 981
                    
                    • -1
                      @ 2024-8-29 13:42:17

                      水题...

                      没啥好说的,直接手算就行。

                      192 384 576
                      219 438 657
                      273 546 819
                      327 654 981
                      
                      • -1
                        @ 2024-8-21 22:43:48

                        B1题解

                        思路很简单,直接用for枚举123到333之间的每一个数(因为333*3=999,已经是三位数的极限)

                        解题方法

                        直接判断,符合就输出

                        Code

                        
                        #include<iostream>
                        
                        using namespace std;
                        
                        int a,b,c;
                        
                        int main()
                        
                        {
                        	for(int i=123;i<=333;i++)
                            {
                        		a=i;
                        
                        		b=i*2;
                        
                        		c=i*3;
                        
                        		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==45)//判断各个数位是否满足条件
                        		
                                {
                        
                        			printf("%d %d %d",a,b,c);
                        
                                    cout<<endl;
                                }
                        
                            }
                            return 0;
                        }
                        

                        最后

                        拒绝复制粘贴!!!!

                      • -2
                        @ 2024-12-28 15:38:03

                        B1.三连击

                        思路

                        脑算

                        Code

                        192 384 576 219 438 657 273 546 819 327 654 981

                        • -2
                          @ 2024-12-23 21:33:20

                          手推题

                          答案:

                          192 384 576

                          219 438 657

                          273 546 819

                          327 654 981

                          • -2
                            @ 2024-11-30 15:54:28

                            三连击

                            解题思路

                            Em......Em......

                            直接手算直接手算

                            答案

                            192 384 576

                            219 438 657

                            273 546 819

                            327 654 981

                            • -2
                              @ 2024-10-23 22:27:25

                              A simple mathmatics problem

                              The simplest way to solve the problem: Calculate by yourself!

                              Answer: 192 384 576 219 438 657 273 546 819 327 654 891

                              Actually, there is a mistake above. What is it? I believe you can find it!

                              • @ 2025-1-24 23:05:31

                                Are you a foreigner? Why use English? This is a Chinese website. If you use English, some people will not understand it. I suggest you translate it into Chinese with a translation tool and then submit the solution.

                            • -3
                              @ 2024-8-25 19:13:34

                              #include<bits/stdc++.h> 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; reutrn 0; }

                              • @ 2024-8-25 22:27:19

                                你过了吗,过了的话是怎么上传答案的?

                            • -4
                              @ 2024-9-16 12:50:09

                              解题方法

                              手算答案即可

                              Key

                              192 384 576
                              219 438 657
                              273 546 819
                              327 654 981
                              
                              • -5
                                @ 2024-9-26 19:18:43

                                B1.三连击


                                直接手算即可

                                Code

                                192 384 576
                                219 438 657
                                273 546 819
                                327 654 981
                                
                                • -8
                                  @ 2024-8-16 19:14:26

                                  B1. 三连击

                                  本人用了一个很离谱的方法,我直接在纸上把答案算出来了(我也没想到就这么点)

                                  所以,直接输出

                                  Code

                                  #include<bits/stdc++.h>

                                  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;

                                  reutrn 0;

                                  }

                                  ##完美结束

                                  • 1

                                  信息

                                  ID
                                  32
                                  时间
                                  1000ms
                                  内存
                                  256MiB
                                  难度
                                  3
                                  标签
                                  递交数
                                  481
                                  已通过
                                  249
                                  上传者