19 条题解

  • 2
    @ 2024-11-8 22:54:49

    J2A. Turtle and Equations题解

    思路

    依次枚举每种可能性,计算结果,无法得出就输出 No

    解题方法

    暴力枚举

    时间复杂度:O(1)O(1)

    Code

    #include<bits/stdc++.h>
    using namespace std;
    int a,b,c,d;
    int tool(int x,int y,int z){
        if(x==0)return y+z;
        if(x==1)return y-z;
        if(x==2)return y*z;
        if(x==3)return y/z;
    }
    int main(){
        int x;
        cin>>a>>b>>c>>d;
        for(int i=0;i<4;i++){
            x=tool(i,a,b);
            for(int j=0;j<4;j++){
                if(tool(j,x,c)==d){
                    cout<<"Yes";
                    return 0;
                }
            }
        }
        cout<<"No";
        return 0;
    }
    
    • 2
      @ 2024-10-6 22:29:22

      Turtle and Equations题解

      思路

      很简单,把所有情况枚举出来就行了

      解题方法

      如思路

      复杂度

      时间复杂度:

      空间复杂度:

      Code

      #include<bits/stdc++.h>
      using namespace std;
      int a,b,c,d;
      int main(){
      	ios::sync_with_stdio(0);
      	cin.tie(0),cout.tie(0);
      	cin>>a>>b>>c>>d;
      	for(int i=1;i<=4;i++){
      		int y=0,m=0;
      		if(i==1) y=a+b;
      		if(i==2) y=a-b;
      		if(i==3) y=a*b;
      		if(i==4) y=a/b;
      		m=y;
      		for(int j=1;j<=4;j++){
      			if(j==1){
      				y=y+c;	
      				if(y==d){
      					cout<<"Yes";
      					return 0;
      				}else{
      					y=m;
      				}
      			} 
      			if(j==2){
      				y=y-c;
      				if(y==d){
      					cout<<"Yes";
      					return 0;
      				}else{
      					y=m;	
      				}
      			} 
      			if(j==3){
      				y=y*c;
      				if(y==d){
      					cout<<"Yes";
      					return 0;
      				}else{
      					y=m;
      				}	
      			} 
      			if(j==4){
      				y=y/c;
      				if(y==d){
      					cout<<"Yes";
      					return 0;
      				}else{
      					y=m;
      				}
      			} 
      		}
      	}
      	cout<<"No";
      	return 0;
      }
      • 2
        @ 2024-10-6 20:34:37

        MX.[J2A]题解

        思路

        先来说说思路。

        很简单,判断即可。

        计算出以下情况(以下的=不指程序中的,而是数学中的):

        • (a+b)+c=d(a + b) + c = d
        • (a+b)c=d(a + b) - c = d
        • (a+b)×c=d(a + b) \times c = d
        • (ab)+c=d(a - b) + c = d
        • (ab)c=d(a - b) - c = d
        • (ab)×c=d(a - b) \times c = d
        • (a×b)+c=d(a \times b) + c = d
        • (a×b)c=d(a \times b) - c = d
        • (a×b)×c=d(a \times b) \times c = d

        代码

        #include<bits/stdc++.h>
        using namespace std;
        int a,b,c,d;
        int ans=0;
        
        int Calculate(int x,int y)
        {
        	int f,s;
        	if(x==1) f=a+b;	
        	if(x==2) f=a-b;	
        	if(x==3) f=a*b;	
        	if(y==1) s=f+c;	
        	if(y==2) s=f-c;	
        	if(y==3) s=f*c;	
        	return s;
        }
        
        int main()
        {
        	cin>>a>>b>>c>>d;
        	for(int i=1;i<=3;i++) 
        		for(int j=1;j<=3;j++) 
        			if(Calculate(i,j)==d) ans=1;
        	if(ans==1) cout<<"Yes";
        	else cout<<"No";
        } 
        

        避免打表无聊,于是写了个复杂的。

        • 1
          @ 2025-6-1 19:22:20

          hack:

          10 5 10 12
          

          ans:

          No
          

          以下代码输出:

          Yes
          
          #include<bits/stdc++.h>
          using namespace std;
          int main(){
              long long a,b,c,d;
              cin>>a>>b>>c>>d;
              if((a+b)+c==d)cout<<"Yes";
              else if((a-b)+c==d)cout<<"Yes";
              else if((a*b)+c==d)cout<<"Yes";
              else if((a/b)+c==d)cout<<"Yes";
              else if((a+b)-c==d)cout<<"Yes";
              else if((a-b)-c==d)cout<<"Yes";
              else if((a*b)-c==d)cout<<"Yes";
              else if((a/b)-c==d)cout<<"Yes";
              else if((a+b)*c==d)cout<<"Yes";
              else if((a-b)*c==d)cout<<"Yes";
              else if((a*b)*c==d)cout<<"Yes";
              else if((a/b)*c==d)cout<<"Yes";
              else if((a+b)/c==d)cout<<"Yes";
              else if((a-b)/c==d)cout<<"Yes";
              else if((a*b)/c==d)cout<<"Yes";
              else if((a/b)/c==d)cout<<"Yes";
              else cout<<"No";
          }
          
          • 1
            @ 2025-3-29 10:24:29

            J2A.题解

            思路

            枚举所有情况,如果有任意一种情况满足便输出Yes,反之输出No

            解题方法

            枚举

            复杂度

            时间复杂度:

            O(1)O(1)

            空间复杂度:

            Code

            #include <bits/stdc++.h>
            using namespace std;
            int main()
            {
            	double a, b, c, d;
            	cin>>a>>b>>c>>d;
            	if((a+b)+c==d || (a-b)+c==d || (a*b)+c==d || (a/b)+c==d || (a+b)-c==d || (a-b)-c==d || (a*b)-c==d || (a/b)-c==d || (a+b)*c==d || (a-b)*c==d || (a*b)*c==d || (a/b)*c==d || (a+b)/c==d || (a-b)/c==d || (a*b)/c==d || (a/b)/c==d)
            	{
            		cout<<"Yes";
            	}
            	else cout<<"No";
            	return 0;
            }
            
            • 1
              @ 2024-11-25 17:49:53

              思路

              模拟

              时间复杂度:

              O(A33)O(A^3_3)

              Code

              // c++ 20 (O2)
              
              #include <bits/stdc++.h>
              using namespace std;
              
              int main(){
                  // input
                  int a,b,c,d; cin >> a >> b >> c >> d;
                  int ans = false;
                  // check
                  if ((a+b)+c == d) ans ++;
                  if ((a+b)-c == d) ans ++;
                  if ((a+b)*c == d) ans ++;
                  if ((a-b)+c == d) ans ++;
                  if ((a-b)-c == d) ans ++;
                  if ((a-b)*c == d) ans ++;
                  if ((a*b)-c == d) ans ++;
                  if ((a*b)+c == d) ans ++;
                  if ((a*b)*c == d) ans ++;
                  //output
                  if (ans) cout << "Yes";
                  else cout << "No";
                  return 0;
              }
              
              • 1
                @ 2024-8-25 12:09:44

                Turtle and Equations题解

                思路

                爆枚两个预算符

                Code

                #include<bits/stdc++.h>
                using namespace std;
                int main()
                {
                	int a,b,c,d;
                	cin>>a>>b>>c>>d;
                	for(int i=1;i<=3;i++)
                	{
                		for(int j=1;j<=3;j++)
                		{
                			int num,sum,ans;
                			switch(i)
                			{
                				case 1:num=a+b;break;
                				case 2:num=a-b;break;
                				case 3:num=a*b;break;
                			}
                			switch(j)
                			{
                				case 1:sum=num+c;break;
                				case 2:sum=num-c;break;
                				case 3:sum=num*c;break;
                			}
                			if(sum==d)
                			{
                				cout<<"Yes";
                				return 0;
                			}
                		}
                	} 
                	cout<<"No"; 
                	return 0;
                }
                
                • 0
                  @ 2024-9-15 16:36:37

                  打表即可。很水。

                  #include<bits/stdc++.h>
                  #define int long long
                  #define INF 0x3f3f3f
                  using namespace std;
                  int a,b,c,d;
                  signed main()
                  {
                  	cin>>a>>b>>c>>d;
                  	if((a+b)+c==d||(a-b)+c==d||(a*b)+c==d){
                  		cout<<"Yes";
                  	}
                  	else if((a+b)-c==d||(a-b)-c==d||(a*b)-c==d){
                  		cout<<"Yes";
                  	}
                  	else if((a+b)*c==d||(a-b)*c==d||(a*b)*c==d){
                  		cout<<"Yes";
                  	}
                  	else cout<<"No";
                  	return 0;
                  }
                  
                  • 0
                    @ 2024-8-5 13:26:29

                    题解

                    思路:一个一个枚举可能的情况,如果枚举完了,都没有等于 dd ,那么就输出 No。

                    解题方法:同上。

                    时间复杂度: O(1)O(1)

                    Code

                    #include<iostream>
                    #include<cstdio>
                    #include<cmath>
                    using namespace std;
                    long long a,b,c,d;
                    int main() {
                    	cin>>a>>b>>c>>d;
                    	char k='*';
                    	if(k=='*') {
                    		long long tmp=a*b;
                    		if(tmp+c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    		else if(tmp-c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    		else if(tmp*c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    	}
                    	k='-';
                    	if(k=='-') {
                    		long long tmp=a-b;
                    		if(tmp+c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    		else if(tmp-c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    		else if(tmp*c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    	}
                    	k='+';
                    	if(k=='+') {
                    		long long tmp=a+b;
                    		if(tmp+c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    		else if(tmp-c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    		else if(tmp*c==d) {
                    			cout<<"Yes"<<endl;
                    			return 0;
                    		}
                    	}
                    	cout<<"No"<<endl;
                    	return 0;
                    }
                    
                    • -1
                      @ 2024-9-21 18:21:17

                      Turtle and Equations题解

                      思路

                      简单打表。

                      Code

                      #include<bits/stdc++.h>
                      using namespace std;
                      long long a,b,c,d;
                      int main(){
                      	cin>>a>>b>>c>>d;
                      	if(a+b+c==d||a+b-c==d||a+b*c==d||(a+b)*c==d||a-b+c==d||a-b-c==d||a-b*c==d||(a-b)*c==d||a*b+c==d||a*b-c==d||a*b*c==d||(a*b)*c==d||a/b+c==d||a/b-c==d||a/b*c==d||a/(b*c)==d){
                      		cout<<"Yes";
                      	}else{
                      		cout<<"No";
                      	}
                      	return 0;
                      }
                      
                      • -1
                        @ 2024-9-2 21:26:45

                        标题

                        思路

                        解题方法

                        复杂度

                        时间复杂度:

                        添加时间复杂度, 示例: O(n)O(n)

                        空间复杂度:

                        添加空间复杂度, 示例: O(n)O(n)

                        Code

                        • -1
                          @ 2024-8-26 21:52:29

                          标题

                          思路

                          解题方法

                          复杂度

                          时间复杂度:

                          添加时间复杂度, 示例: O(n)O(n)

                          空间复杂度:

                          添加空间复杂度, 示例: O(n)O(n)

                          Code

                          • -1
                            @ 2024-8-4 21:40:31

                            算法:枚举。

                            这题由于从左往右计算即可,所以无需使用栈。

                            因为给定了 a,b,c,da,b,c,d,而且只需在两个空格中分别填入加减乘三种运算符中的一个运算符即可,所以最多有 99 种情况,枚举所有情况(如果结果为 dd 直接结束)求解即可。

                            接下来是代码。

                            #include<bits/stdc++.h>
                            using namespace std;
                            int a,b,c,d; 
                            char c1[]={'+','-','*'}; 
                            char c2[]={'+','-','*'}; //打两张表方便枚举
                            int main(){
                            	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);//读入优化 
                            	cin>>a>>b>>c>>d; 
                            	for(int i = 0; i < 3; i++) {
                            		int sum = 0; 
                            		switch(c1[i]) {
                            			case '+':{
                            				sum+=a+b; 
                            				break;
                            			}
                            			case '-':{
                            				sum+=a-b; 
                            				break; 
                            			}
                            			case '*':{
                            				sum+=a*b; 
                            				break;
                            			}
                            		}
                            		for(int j = 0; j < 3; j++) {
                            			int sum2 = sum; 
                            			switch(c2[j]) {
                            				case '+':{
                            					sum+=c; 
                            					break;
                            				}
                            				case '-':{
                            					sum-=c; 
                            					break; 
                            				}
                            				case '*':{
                            					sum*=c; 
                            					break;
                            				}
                            			}
                            			if(sum==d) {
                            				cout<<"Yes"; 
                            				return 0; 
                            			}
                            			sum = sum2; 			
                            		}
                            	}
                                    cout<<"No"; 
                            	return 0; 
                            }
                            

                            因为 a,b,ca, b, c 上限都是 1010,可以求得结果最大为 10001000, 所以不会爆 int

                            这样,本题就解决了。

                            如有不足,还请指出,感谢观看。

                            • -1
                              @ 2024-8-4 19:10:23

                              Turtle and Equations题解

                              思路

                              看到两个空格就想到了两重循环。

                              解题方法

                              每个循环都是 33 次,这样可以遍历两个空格的符号,然后利用 tmptmp 计算结果,如果相等则输出 Yes,直接 return 0

                              复杂度

                              时间复杂度:O(32)O(3^{2})

                              代码

                              #include <bits/stdc++.h>
                              
                              using namespace std;
                              
                              int a, b, c, d, tmp;
                              
                              int main() {
                                  cin >> a >> b >> c >> d;
                                  for (int i = 1; i <= 3; ++i) {
                                      for (int j = 1; j <= 3; ++j) {
                                          if (i == 1) {
                                              tmp = a + b;
                                          } else if (i == 2) {
                                              tmp = a - b;
                                          } else if (i == 3) {
                                              tmp = a * b;
                                          }
                                          if (j == 1) {
                                              tmp += c;
                                          } else if (j == 2) {
                                              tmp -= c;
                                          } else if (j == 3) {
                                              tmp *= c;
                                          }
                                          if (tmp == d) {
                                              cout << "Yes" << endl;
                                              return 0;
                                          }
                                      }
                                  }
                                  cout << "No" << endl;
                              
                                  return 0;
                              }
                              
                              • -1
                                @ 2024-8-4 18:39:30

                                Description\textbf{Description}

                                给你四个正整数 a,b,c,da, b, c, d

                                现在你有一条算式 (a  b)  c(a\ \Box\ b)\ \Box\ c。你需要判断能否在两个方框内分别填入三种运算符 +,,×+, -, \times 之一(运算符可以重复使用),使得算式运算的结果等于 dd

                                Solution\textbf{Solution}

                                每个方框有 33​ 种可能性,所以整个方案数总共有 32=93^2=9​ 种,所以可以枚举每一种情况,然后判断即可。

                                时间复杂度 O(1)O(1)

                                Code\textbf{Code}

                                #include <bits/stdc++.h>
                                
                                int a, b, c, d;
                                
                                int main() {
                                  std::cin >> a >> b >> c >> d;
                                  if ((a + b) + c == d || 
                                      (a + b) - c == d ||
                                      (a + b) * c == d ||
                                      (a - b) + c == d ||
                                      (a - b) - c == d ||
                                      (a - b) * c == d ||
                                      (a * b) + c == d ||
                                      (a * b) - c == d ||
                                      (a * b) * c == d) puts("Yes"); // 枚举 9 种情况
                                  else puts("No");
                                  return 0;
                                }
                                
                                • -2
                                  @ 2024-8-7 19:58:34

                                  J2A 题解

                                  第六篇题解。

                                  这题只需要枚举,算出每一种可能的情况,判断是否有刚好等于 dd 的情况即可 AC。

                                  代码:

                                  #include<iostream>
                                  using namespace std;
                                  int main()
                                  {
                                      int a,b,c,d;
                                      cin>>a>>b>>c>>d;
                                      if(a+b+c==d||a+b-c==d||a-b+c==d||a-b-c==d||a*b+c==d||a*b-c==d||(a+b)*c==d||(a-b)*c==d||a*b*c==d)cout<<"Yes";
                                      else cout<<"No";
                                      return 0;
                                  }
                                  
                                  • -3
                                    @ 2024-8-12 14:35:35

                                    J2A题解

                                    思路

                                    模拟每一个算式,看等不等于d。

                                    解题方法

                                    if

                                    时间复杂度:

                                    时间复杂度O(9)O(9)

                                    空间复杂度:

                                    添加空间复杂度, 示例: O(5)O(5)

                                    Code

                                    #include<bits/stdc++.h>
                                    using namespace std;
                                    int a,b,c,d,s;
                                    int main(){
                                    	cin>>a>>b>>c>>d;
                                    	s=(a+b)+c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	s=(a-b)+c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	s=(a*b)+c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	s=(a+b)-c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	s=(a+b)*c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	s=(a-b)-c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	s=(a-b)*c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	s=(a*b)*c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	s=(a*b)-c;
                                    	if(s==d){
                                    		cout<<"Yes"<<endl;
                                    		return 0;
                                    	}
                                    	cout<<"No"<<endl;
                                    	return 0;
                                    }
                                    
                                    • -3
                                      @ 2024-8-5 16:33:03

                                      标Turtle and Equations题解

                                      思路

                                      枚举 把所有方法都枚举出来就好了。

                                      解题方法

                                      没啥可说的,把所有情况都写出来

                                      Code

                                      #include <bits/stdc++.h>
                                      using namespace std;
                                      
                                      int main(){
                                          int a, b, c, d;
                                          cin >> a >> b >> c >> d;
                                      
                                          if ((a + b) + c == d) cout << "Yes\n";
                                          else if((a + b) - c == d) cout << "Yes\n";
                                          else if((a + b) * c == d) cout << "Yes\n";
                                          else if((a - b) + c == d) cout << "Yes\n";
                                          else if((a - b) - c == d) cout << "Yes\n";
                                          else if((a - b) * c == d) cout << "Yes\n";
                                          else if((a * b) + c == d) cout << "Yes\n";
                                          else if((a * b) - c == d) cout << "Yes\n";
                                          else if((a * b) * c == d) cout << "Yes\n";
                                          else cout << "No\n";
                                      
                                          return 0;
                                      }
                                      
                                      • -5
                                        @ 2024-8-4 17:32:12

                                        Turtle and Equations题解

                                        赛时没时间,赛后看了一下。一开始看到题吓了一跳,还以为是数学题。仔细一看才发现,这是道妥妥大水题。

                                        思路

                                        说白了很简单。仔细看题面,就可以发现,表达式很简洁,所以只需要分类讨论就行了。

                                        解题方法

                                        我的写法可能麻烦了些,写了个简单的函数来判断,具体看代码吧。

                                        Code

                                        #include<bits/stdc++.h>
                                        using namespace std;
                                        typedef long long ll;
                                        bool chk(int a,int b,int c)
                                        {
                                        	if(a+b==c||a-b==c||a*b==c) return true;
                                        	else return false;
                                        }
                                        int main()
                                        {
                                        	ios_base::sync_with_stdio(false);
                                        	cin.tie(0);cout.tie(0);
                                        	int a,b,c,d;
                                        	cin>>a>>b>>c>>d;
                                        	int he=a+b,cha=a-b,ji=a*b;
                                        	if(chk(he,c,d)||chk(cha,c,d)||chk(ji,c,d)) cout<<"Yes";
                                        	else cout<<"No";
                                        	return 0;
                                        }
                                        
                                        • 1

                                        信息

                                        ID
                                        25
                                        时间
                                        1000ms
                                        内存
                                        512MiB
                                        难度
                                        1
                                        标签
                                        递交数
                                        1802
                                        已通过
                                        626
                                        上传者