8 条题解

  • 13
    @ 2023-7-20 12:59:50
    #include <iostream>
    using namespace std;
    bool prime(int x)
    {
        if (x < 2)
        {
            return false;
        }
        for (int i = 2; i * i <= x; i++)
        {
            if (x % i == 0)
            {
                return false;   
            }
        }
        return true;
    }
    int main()
    {
        for (int i = 6; i <= 100; i += 2)
        {
            for (int j = 2; j <= i; j++)
            {
                if (prime(j) && prime(i - j))
                {
                    cout << i << "=" << j << "+" << i - j << endl;
                    break;
                }            
            }
        }
        return 0;
    }
    

    斩草不除根,春风吹又生。

    解析:

    1.定义一个判断实参是否为质数的函数。(真的不想再讲了,用了很多遍了!)懒得再讲一遍

    2.用 for 循环遍历的到每一个需要拆解的偶数。

    3.再用 for 循环从 2 (因为 1 不伦不类)开始遍历到 i(需拆偶数)

    4.用函数判断 j 和 i - j 是否为质数。是则输出算式。

    5.用 break 跳出当前一层循环(遍历得 j ),因为我们得到的第一个算式的 j 肯定是最小的,跳出可以避免输出别的拆解组合。

    记得用 endl 或 /n ,切到另一行。

    6.点个赞吧👍 点赞你就是我爹妈(bushi

    • @ 2023-9-3 15:09:13

      hetao1296998,我yyds(永远的神or永远都是?)你爹 👀️ 👍

  • 4
    @ 2023-7-19 23:16:34

    yaosou帮你压缩改进一下👀️ 就是std::打的手累😄

    #include <iostream>
    int main(){std::cout << "6=3+3" << std::endl << "8=3+5" << std::endl << "10=3+7" << std::endl << "12=5+7" << std::endl << "14=3+11" << std::endl << "16=3+13" << std::endl << "18=5+13" << std::endl << "20=3+17" << std::endl << "22=3+19" << std::endl << "24=5+19" << std::endl << "26=3+23" << std::endl << "28=5+23" << std::endl << "30=7+23" << std::endl << "32=3+29" << std::endl << "34=3+31" << std::endl << "36=5+31" << std::endl << "38=7+31" << std::endl << "40=3+37" << std::endl << "42=5+37" << std::endl << "44=3+41" << std::endl << "46=3+43" << std::endl << "48=5+43" << std::endl << "50=3+47" << std::endl << "52=5+47" << std::endl << "54=7+47" << std::endl << "56=3+53" << std::endl << "58=5+53" << std::endl << "60=7+53" << std::endl << "62=3+59" << std::endl << "64=3+61" << std::endl << "66=5+61" << std::endl << "68=7+61" << std::endl << "70=3+67" << std::endl << "72=5+67" << std::endl << "74=3+71" << std::endl << "76=3+73" << std::endl << "78=5+73" << std::endl << "80=7+73" << std::endl << "82=3+79" << std::endl << "84=5+79" << std::endl << "86=3+83" << std::endl << "88=5+83" << std::endl << "90=7+83" << std::endl << "92=3+89" << std::endl << "94=5+89" << std::endl << "96=7+89" << std::endl << "98=19+79" << std::endl << "100=3+97";}
    
    • @ 2023-7-20 18:49:53

      《论某些大佬是如何把明明要写十几行的代码压缩的只剩两行》

    • @ 2023-7-20 18:50:28

      全是狠活是吧

    • @ 2023-7-21 19:25:52

      6

      一个一个输出是吧

    • @ 2023-8-13 18:03:00

      逆天 什么开卷考试

  • 1
    @ 2023-7-19 19:42:23

    en......有亿点点长

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        cout << "6=3+3" << endl << "8=3+5" << endl << "10=3+7" << endl << "12=5+7" << endl << "14=3+11" << endl << "16=3+13" << endl << "18=5+13" << endl << "20=3+17" << endl << "22=3+19" << endl << "24=5+19" << endl << "26=3+23" << endl << "28=5+23" << endl << "30=7+23" << endl << "32=3+29" << endl << "34=3+31" << endl << "36=5+31" << endl << "38=7+31" << endl << "40=3+37" << endl << "42=5+37" << endl << "44=3+41" << endl << "46=3+43" << endl << "48=5+43" << endl << "50=3+47" << endl << "52=5+47" << endl << "54=7+47" << endl << "56=3+53" << endl << "58=5+53" << endl << "60=7+53" << endl << "62=3+59" << endl << "64=3+61" << endl << "66=5+61" << endl << "68=7+61" << endl << "70=3+67" << endl << "72=5+67" << endl << "74=3+71" << endl << "76=3+73" << endl << "78=5+73" << endl << "80=7+73" << endl << "82=3+79" << endl << "84=5+79" << endl << "86=3+83" << endl << "88=5+83" << endl << "90=7+83" << endl << "92=3+89" << endl << "94=5+89" << endl << "96=7+89" << endl << "98=19+79" << endl << "100=3+97";
    }
    
  • -1
    @ 2024-4-19 21:13:32
    #include<iostream>
    using namespace std;
    bool check(int n)
    {
        if(n==0 || n==1)
        {
            return false;
        }
        for(int i=2;i<n;i++)
        {
            if (n%i==0)
            {
                return false;
            }
        }
        return true;
    }
    int main()
    {
        int sum,ans=0;
        cin >> sum;
        for(int i=sum;i>=0;i--)
        {
            if (check(i) && check(sum-i))
            {
                if (i*(sum-i)>=ans)
                {
                    ans=i*(sum-i);
                }
            }
        }
        cout << ans;
        return 0;
    }
    
    • -1
      @ 2023-7-19 11:38:35

      解析

      将6~100中的每个数表示成两个素数相加

      枚举其中的一个加数,判断两个加数是否都为素数

      题解

      #include <iostream>
      using namespace std;
      bool isP(int x)
      {
          if (x < 2)
              return false;
          for (int i = 2; i * i <= x; i++)
              if (x % i == 0)
                  return false;
          return true;
      }
      int main()
      {
          for (int i = 6; i <= 100; i += 2)
          {
              //找到 i 拆分的方案
              for (int j = 2; j <= i / 2; j++)//现在的方案是 j+(i-j) == i
                  if (isP(j) && isP(i - j))
                  {
                      cout << i << "=" << j << "+" << i - j << "\n";
                      break;
                  }
          }
          return 0;
      }
      
      
      
      • @ 2023-7-30 22:18:33

        老师,为什么我在最后输出的if嵌套中没使用“{}”框住执行语句,它便没有输出值?

      • @ 2023-7-30 22:18:52

        老师,为什么我在最后输出的if嵌套中没使用“{}”框住执行语句,它便没有输出值?

      • @ 2023-8-3 16:31:53

        多行要用{}@

    • -2
      @ 2023-7-20 11:09:39
      #include<bits/stdc++.h>
      using namespace std;
      bool jntm(int n)
      {
          if (n<2) return false; 
          for (int i=2;i*i<=n;i++)
          {
              if (n%i==0) return false;
          }
          return true;
      }
      int main()
      {
          for (int j=6;j<=100;j+=2)
          {
              for (int k=2;k<=j;k++)
              {
                  int f=j-k;
                  if (jntm(k) && jntm(f)) 
                  {
                      cout<<j<<"="<<k<<"+"<<f<<endl; 
                      break;
                  }
              }
          }
          return 0;
      }
      
      • -2
        @ 2023-7-19 21:52:04

        思路:

        既然是要把6到100之间的所有偶数拆开,那可以先写个for循环把6到100之间的所有偶数全枚举出来(循环变量为i)

        然后,再写个for循环枚举2到i之间所有的数(循环变量为j),通过i-j算出另一个数,如果两个数都是质数,那么输出,然后用个break跳出第二层循环(因为我们只需要把第一种结果输出出来就行了)

        #include <iostream>
        using namespace std;
        
        bool prime(int n)
        {
            for(int i = 2;i < n;i++)
            {
                if(n % i == 0)
                    return false;
            }
            return true;
        }
        
        int main()
        {
            for(int i = 6;i <= 100;i+=2)
            {
                for(int j = 2;j <= i - 2;j++)
                {
                    if(prime(j) && prime(i - j))
                    {
                        cout << i << '=' << j << '+' << i - j << endl;
                        break;
                    }
                }
            }
            return 0;
        }
        
        • -3
          @ 2023-8-3 16:16:34
          #include<iostream>
          using namespace std;
          int main()
          {
              int sushu[] = {0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
              bool flag = false;
              for(int i = 6; i <= 100; i += 2)
              {
                  for(int j = 1; j <= 25; j++)
                  {
                      for(int k = j; k <= 25; k++)
                      {
                          if(sushu[j] + sushu[k] == i)
                          {
                              cout << i << "=" << sushu[j] << "+" << sushu[k] << endl;
                              flag = true;
                              break;
                          }
                      }
                      if(flag)
                      {
                          flag = false;
                          break;
                      }
                  }
              }
              return 0;
          }
          
          • 1

          信息

          ID
          306
          时间
          1000ms
          内存
          128MiB
          难度
          4
          标签
          递交数
          1293
          已通过
          555
          上传者