9 条题解

  • 7
    @ 2024-2-19 16:37:02
    #include<bits/stdc++.h>
    #define int long long
    #define endl '\n' 
    using namespace std;
    int a[10000];
    string s1,s2,s3,s4;
    signed main(){
    	int x,y;
    	char f;
    	cin>>x>>y>>f;
    	switch(f){
    		case'+':
    			cout<<x+y;
    			break;
    		case'-':
    			cout<<x-y;
    			break;
    		case'*':
    			cout<<x*y;
    			break;
    		case'/':
    			if(y==0){
    				cout<<"Divided by zero!";
    			}
    			else{
    				cout<<x/y;
    			}
    			break;
    		default :
    			cout<<"Invalid operator!";
    	}
    	return 0;
    } 
    
    • 1
      @ 2025-3-23 9:39:06

      #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'': cout<<xy; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }

      • 1
        @ 2024-3-4 19:38:22
        • #include<bits/stdc++.h>
          #define int long long
          #define endl '\n' 
          using namespace std;
          int a[10000];
          string s1,s2,s3,s4;
          signed main(){
          	int x,y;
          	char f;
          	cin>>x>>y>>f;
          	switch(f){
          		case'+':
          			cout<<x+y;
          			break;
          		case'-':
          			cout<<x-y;
          			break;
          		case'*':
          			cout<<x*y;
          			break;
          		case'/':
          			if(y==0){
          				cout<<"Divided by zero!";
          			}
          			else{
          				cout<<x/y;
          			}
          			break;
          		default :
          			cout<<"Invalid operator!";
          	}
          	return 0;
          }
          

          Copy

        • 0

          rc245zhangjiayi1 LV 6 @ 1 天前

          #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'': cout<<xy; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }

        • 0

          rc247chenbosheng1 LV 8 @ 3 天前

          #include<bits/stdc++.h>
          #define int long long
          #define endl '\n' 
          using namespace std;
          int a[10000];
          string s1,s2,s3,s4;
          signed main(){
          	int x,y;
          	char f;
          	cin>>x>>y>>f;
          	switch(f){
          		case'+':
          			cout<<x+y;
          			break;
          		case'-':
          			cout<<x-y;
          			break;
          		case'*':
          			cout<<x*y;
          			break;
          		case'/':
          			if(y==0){
          				cout<<"Divided by zero!";
          			}
          			else{
          				cout<<x/y;
          			}
          			break;
          		default :
          			cout<<"Invalid operator!";
          	}
          	return 0;
          } 
          
        • 1
          @ 2024-3-1 14:04:59
          #include<bits/stdc++.h>
          #define int long long
          #define endl '\n' 
          using namespace std;
          int a[10000];
          string s1,s2,s3,s4;
          signed main(){
          	int x,y;
          	char f;
          	cin>>x>>y>>f;
          	switch(f){
          		case'+':
          			cout<<x+y;
          			break;
          		case'-':
          			cout<<x-y;
          			break;
          		case'*':
          			cout<<x*y;
          			break;
          		case'/':
          			if(y==0){
          				cout<<"Divided by zero!";
          			}
          			else{
          				cout<<x/y;
          			}
          			break;
          		default :
          			cout<<"Invalid operator!";
          	}
          	return 0;
          } 
          
          • 0
            @ 2025-3-23 9:38:51

            #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'': cout<<xy; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }

            • 0
              @ 2025-3-23 9:38:34

              #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'': cout<<xy; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }

              • 0
                @ 2024-3-9 21:10:46

                #include <bits/stdc++.h> using namespace std; int main() { int a,b,c; char d; cin>>a>>b>>d; if(d!='+'&&d!='-'&&d!=''&&d!='/') { cout<<"Invalid operator!";} else { if(d=='+') { c=a+b;} else if(d=='-') { c=a-b; } else if(d=='') { c=a*b; } else if(d=='/') { if(b==0) { cout<<"Divided by zero!"; exit(0); } else { c=a/b; } } else {

                }
                }
                cout<<c;
                return 0;
                

                }

                • 0
                  @ 2024-3-3 10:40:55

                  #include<bits/stdc++.h> #define int long long #define endl '\n' using namespace std; int a[10000]; string s1,s2,s3,s4; signed main(){ int x,y; char f; cin>>x>>y>>f; switch(f){ case'+': cout<<x+y; break; case'-': cout<<x-y; break; case'': cout<<xy; break; case'/': if(y==0){ cout<<"Divided by zero!"; } else{ cout<<x/y; } break; default : cout<<"Invalid operator!"; } return 0; }

                  • 0
                    @ 2024-2-23 16:28:55

                    叠甲:c风格处理速度比c++风格处理速度快,但两者皆可通过c++测试,请谅解!!!

                    首先,我们先排除计算器的常见问题:

                    #include<bits/stdc++.h>
                    using namespace std;
                    int main(){
                    int a,b;
                    char c;
                    cin>>a>>b>>c;
                    if(c!='+'&&c!='-'&&c!='*'&&c!='/')
                    {
                    cout<<"Invalid operator!";
                    return 0;//return 0指系统返回值,不再执行下个语句
                    }
                    if(c=='/'&&b==0)
                    {
                    cout<<"Divided by zero!";
                    return 0;
                    }
                    

                    这样,我们就可以排除符号写错、除数为0的问题。

                    符号写错是+、-、*、/以外的符号写入,从而导致的错误;

                    除数为0是一个DDDD(懂的都懂)的问题,不再多提。

                    其次,再加入算法,运行

                    if(c=='+')
                    cout<<a+b;
                    if(c=='-')
                    cout<<a-b;
                    if(c=='*')
                    cout<<a*b;
                    if(c=='/')
                    cout<<a/b;
                    }
                    

                    总结:此题难点在于一些错误输出的应对方法,但对算法没有过多要求,总体来说还是比较简单的。

                    • @ 2024-2-29 20:35:04

                      你的代码的确有问题,或者说是可以改进的地方: 1.头文件? 2.为什么不用scanf和printf而用cin和cout?scanf和printf的编译时间比cin和cout短,很好的规避了很多编译超时 3.同样是时间的问题,if else结构复杂,行数多,繁琐,我们可以用swich语句来解决:

                      swich(){
                          cace' ':cout<<;break;
                          cace' ':cout<<;break;
                          cace' ':cout<<;break;
                          cace' ':cout<<;break;
                          default:cout<<;break;
                      }
                      

                      只需要在判断除法的时候判断是否为0就行了:

                      if(y==0) cout<<"Divided by zero!"; 
                      else cout<</;
                      

                      编译时间长,着我没骗你,我用DEV做了个实验: 你的代码:image

                      我的代码:image 只不过这道题比较简单,还不太可能出现编译超时,不过做到复杂的算法题就得小心了。

                      下面分享一下我的代码:

                      #include<stdio.h>
                      int main()
                      {
                          int a,b;
                          char c;
                          scanf("%d %d %c",&a,&b,&c);
                          if (b==0)
                          printf("Divided by zero!");
                          else
                          switch(c)
                          {
                              case '+':printf("%d",a+b);break;
                              case '-':printf("%d",a-b);break;
                              case '*':printf("%d",a*b);break; 
                              case '/':
                                      if (b==0) printf("Divided by zero!");
                                      else printf("%d",a/b);
                                      break;
                              default:printf("Invalid operator!");
                          }
                          return 0;
                      }
                      
                    • @ 2024-2-29 20:37:35

                      我们的代码时差就2秒,现实中是很短的,不过在计算机里可是“谬以千秒”

                    • @ 2024-3-5 20:54:20

                      @ 你咋拿c风格来比呢?c风格肯定比c++风格处理速度快啊!我只使用c++风格演示。并且,首次编译时间较长,需多次编译

                    • @ 2024-3-5 21:10:15

                      谅解一下,头文件因未压在代码块中导致显示不出来,现已改正

                  • 1

                  信息

                  ID
                  1222
                  时间
                  1000ms
                  内存
                  128MiB
                  难度
                  1
                  标签
                  (无)
                  递交数
                  241
                  已通过
                  158
                  上传者