5 条题解

  • 3
    @ 2023-7-7 22:35:11

    解题思路:

    根据题目来看,我们很容易写一堆if-else if-else去判断多种情况该输出什么,事实上,我们只需要依次判断n能否被3,5,7整除,如果能,就输出这个数和一个空格,因为我们还需要处理n不能被3,5,7整除的情况,所以我们可以设一个变量,并使其初始值为0,当它能被3,5,7任何一个数字整除时,将变量更改为1,如果判断完成后,变量仍然为0,输出n

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int n,state = 0;
        cin >> n;
        if (n % 3 == 0)
        {
            cout << 3 << ' ';
            state = 1;
        }
        if (n % 5 == 0)
        {
            cout << 5 << ' ';
            state = 1;
        }
        if (n % 7 == 0)
        {
            cout << 7 << ' ';
            state = 1;
        }
        if (state == 0)
            cout << 'n';
        return 0;
    }
    
    • 1
      @ 2024-5-5 11:18:49
      #include <iostream>
      using namespace std;
      int main()
      {
          int a;
          cin >> a;
          //能同时被3,5,7整除
          if(a%105==0)
          {
              cout << "3 5 7";
          }
          //只能被其中两个数乘除
          else if(a%35==0)
          {
              cout << "5 7";
          }
          else if(a%21==0)
          {
              cout << "3 7";
          }
          else if(a%15==0)
          {
              cout << "3 5";
          }
          //只能被其中一个数整除
          else if(a%7==0)
          {
              cout << "7";
          }
          else if(a%5==0)
          {
              cout << "5";
          }
          else if(a%3==0)
          {
              cout << "3";
          }
          //不能被任何数整除
          else
          {
              cout << "n";
          }
      }
      
      • 0
        @ 2024-5-1 18:21:07
        ```cpp
        #include <iostream>
        using namespace std;
        int main()
        {
        int x;
        cin >> x;
        if (x%3==0&&x%5==0&&x%7==0)//能否被3,5,7整除
        cout << "3 5 7";
        else if (x%3==0&&x%5==0&&x%7!=0)//只被3,5整除
        cout << "3 5";
        else if(x%3==0&&x%7==0&&x%5!=0)//只被3。7整除
        cout << "3 7";
        else if(x%5==0&&x%7==0&&x%3!=0)//只被5,7整除
        cout << "5 7";
        else if(x%3==0&&x%5!=0&&x%7!=0)只被3整除
        cout << "3";
        else if(x%3!=0&&x%5==0&&x%7!=0)只被5整除
        cout << "5";
        else if(x%3!=0&&x%5!=0&&x%7==0)只被7整除
        cout << "7";
        else
        cout <<"n";
        return 0;
        }
        
        
        
        
        
        
        
      • 0
        @ 2023-7-22 15:17:40

        yasuo

        #include <iostream>
        int main(){
            int q,flag=0;
            std::cin>>q;
            if(q%3==0){std::cout<<"3 ";flag=1;}
            if(q%5==0){std::cout<<"5 ";flag=1;}
            if(q%7==0){std::cout<<"7 ";flag=1;}
            if(flag==0)std::cout<<"n";
            return 0;}
        
        • 0
          @ 2023-6-21 0:31:21
          #include <iostream>
          using namespace std;
          
          int main() {
              int n;
              cin >> n;
          
              if (n % 3 == 0 && n % 5 == 0 && n % 7 == 0) {
                  cout << "3 5 7";
              } else if (n % 3 == 0 && n % 5 == 0) {
                  cout << "3 5";
              } else if (n % 3 == 0 && n % 7 == 0) {
                  cout << "3 7";
              } else if (n % 5 == 0 && n % 7 == 0) {
                  cout << "5 7";
              } else if (n % 3 == 0) {
                  cout << "3";
              } else if (n % 5 == 0) {
                  cout << "5";
              } else if (n % 7 == 0) {
                  cout << "7";
              } else {
                  cout << "n";
              }
          
              return 0;
          }
          
          
          • 1

          信息

          ID
          166
          时间
          1000ms
          内存
          128MiB
          难度
          3
          标签
          递交数
          220
          已通过
          117
          上传者