18 条题解

  • 5
    @ 2023-8-8 21:57:34
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int a[105][105], b[105][105], n, m, sum;
    int main()
    {
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> a[i][j];
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> b[i][j];
            }
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (a[i][j] == b[i][j])
                {
                    sum ++;
                }
            }
        }
        cout << fixed << setprecision(2) << 1.0 * sum / (n * m) * 100;
        return 0;
    }
    
    • 4
      @ 2023-7-16 21:26:44
      #include <iostream>
      #include <iomanip>
      using namespace std;
      int a[105][105];
      int b[105][105];
      int main()
      {
          int n,m;
          int sum = 0;
          cin >> n >> m;
          for (int i = 1; i <= n; i++)
          {
              for (int j = 1; j <= m; j++)
              {
                  cin >> a[i][j];                    
              }
          }
      
          for (int i = 1; i <= n; i++)
          {
              for (int j = 1; j <= m; j++)
              {
                  cin >> b[i][j];                
              }
          }
          for (int i = 1; i <= n; i++)
          {
              for (int j = 1; j <= m; j++)
              {
                  if (a[i][j] == b[i][j])
                  {
                      sum++;                 
                  }
              }
          }
          cout << fixed << setprecision(2) << ((1.0 * sum) / (n * m) * 100);
          return 0;
      }
      

      解析:

      1.开俩数组,并定义储存图像长宽的变量。

      2.用循环嵌套获取数据并将其储存在对应的数组里。

      3.用循环嵌套遍历每一个元素,判断是否相同,是就将相似度( 统计变量 sum )自增。

      4.用相似度除以总像素数量可得同像素占比(一个 0.xx 的小数)乘以 100 得到百分比。

      5.这次一定👍 ,话说不知道为什么,虽然我还能提起写题解的兴趣,但感觉没有之前的那种幽默的感觉了,而是更追求效率,解析思路也越来越精炼,越来越少,我认为我得做出改变了。

      • 2
        @ 2023-7-13 12:09:42

        解析

        根据题意两幅图像的相似度定义为相同像素点数占总像素点数的百分比,可知 相似度 = 相同点数量 / 总数量。

        故读入两个矩阵之后,遍历矩阵中的每个元素,判断对应的点是否相同,相同就增加相同点的数量。

        最后相除求得相似度即可。

        易错点

        问题求的是百分比,所以最终结果需要乘以100。

        题解

        #include <iostream>
        #include <iomanip>
        using namespace std;
        int a[105][105], b[105][105], n, m, sum;
        int main()
        {
            cin >> n >> m;
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= m; j++)
                    cin >> a[i][j];
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= m; j++)
                    cin >> b[i][j];
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= m; j++)
                    if (a[i][j] == b[i][j])
                        sum++;
            cout << fixed << setprecision(2) << 1.0 * sum / (n * m) * 100;
            return 0;
        }
        
        • 1
          @ 2024-6-15 20:26:26

          我真服了压缩流,非得把亿点代码放在一行写!!😕 😕

          #include<bits/stdc++.h>
          using namespace std;int main(){int m,n,o=0;cin >> m >> n;int a[105][105],b[105][105];for (int i=1;i<=n;i++){for (int j=1;j<=m;j++){cin >> a[i][j];}}for (int i=1;i<=n;i++){for (int j=1;j<=m;j++){cin >> b[i][j];}}for (int i=1;i<=n;i++){for (int j=1;j<=m;j++){if (a[i][j]==b[i][j]){o++;}}}cout << fixed << setprecision(2) << ((1.0*o)/(n*m)*100);}
          

          好看吗!!!?????👎 👎

          #include<bits/stdc++.h>
          using namespace std;
          int main()
          {
              int m,n,o=0;
              cin >> m >> n;
              int a[105][105],b[105][105];
              for (int i=1;i<=n;i++){
                  for (int j=1;j<=m;j++){
                      cin >> a[i][j];
                  }
              }
              for (int i=1;i<=n;i++){
                  for (int j=1;j<=m;j++){
                      cin >> b[i][j];
                  }
              }
              for (int i=1;i<=n;i++){
                  for (int j=1;j<=m;j++){
                      if (a[i][j]==b[i][j])
                      {
                          o++;
                      }
                  }
              }
              cout << fixed << setprecision(2) << ((1.0*o)/(n*m)*100);
              }
          

          同意点赞!!

          • 1
            @ 2024-4-16 20:22:45
            #include<iostream>
            #include<iomanip>
            using namespace std;
            int main()
            {
                int m,n,sum=0;
                cin >> m >> n;
                int a[m][n],b[m][n];
                int zong=m*n;
                for(int i=0;i<m;i++)//循环输入两个矩阵
                {
                    for(int j=0;j<n;j++)
                    {
                        cin >> a[i][j];
                    }
                }
                for(int i=0;i<m;i++)
                {
                    for(int j=0;j<n;j++)
                    {
                        cin >> b[i][j];
                    }
                }
                for(int i=0;i<m;i++)//计算相同点数
                {
                    for(int j=0;j<n;j++)
                    {
                        if (a[i][j]==b[i][j])
                        {
                            sum++;
                        }
                    }
                }
                double ans=sum*100.0/zong;//计算相似度
                cout << fixed << setprecision(2) << ans;
                return 0;
            }
            
            • 1
              @ 2023-8-4 8:06:25

              P0030题解

              核心思路


              for循环输入a,b数组,有相同像素点时sum加一,使用格式化输出(sum/n*m) 核心代码如下

              cout << fixed << setprecision(2) << ((1.0 * sum) / (n * m) * 100);
              //相似度输出
              
              for (int i = 1; i <= n; i++)  //for循环遍历,匹配相似度
              {
                  for (int j = 1; j <= m; j++)
                  {
                      if (a[i][j] == b[i][j])
                      {
                          sum++;    //像素点相同时增加sum
                      }
                  }
              }
              

              完整题解


              #include <iostream>
              #include <iomanip>
              using namespace std;
              int a[105][105];
              int b[105][105];
              int main()
              {
                  int n,m;
                  int sum = 0;
                  cin >> n >> m;
                  for (int i = 1; i <= n; i++)
                  {
                      for (int j = 1; j <= m; j++)
                      {
                          cin >> a[i][j];            
                      }
                  }
              
                  for (int i = 1; i <= n; i++)
                  {
                      for (int j = 1; j <= m; j++)
                      {
                          cin >> b[i][j];        
                      }
                  }
                  for (int i = 1; i <= n; i++)  //for循环遍历,匹配相似度
                  {
                      for (int j = 1; j <= m; j++)
                      {
                          if (a[i][j] == b[i][j])
                          {
                              sum++;    //像素点相同时增加sum
                          }
                      }
                  }
                  cout << fixed << setprecision(2) << ((1.0 * sum) / (n * m) * 100);
                  //相似度输出
                  return 0;
              }
              

              已AC

              image

              • 1
                @ 2023-7-19 22:19:34

                压缩流7行解决👀️

                #include <bits/stdc++.h>
                int main(){
                    int a[101][101],b[101][101],x,y,sum;std::cin>>x>>y;
                    for(int i=1;i<=x;i++)for(int j=1;j<=y;j++)std::cin>>a[i][j];
                    for(int i=1;i<=x;i++)for(int j=1;j<=y;j++)std::cin>>b[i][j];
                    for(int i=1;i<=x;i++)for(int j=1;j<=y;j++)if(a[i][j]==b[i][j])sum++;
                    printf("%.2f",1.0*sum/(x*y)*100);return 0;}
                
                • @ 2023-7-19 22:20:47

                  复制起来都比别人方便👍

              • 1
                @ 2023-7-19 11:50:14
                #include <iostream>
                #include <cstdio>
                using namespace std;
                int main()
                {
                    int m, n, a[1005][1005], b[1005][1005];
                    double sum = 0.00;
                    cin >> m >> n;
                    for (int s = 1; s <= 3; s++)
                    {
                        for (int i = 1; i <= m; i++)
                        {
                            for (int j = 1; j <= n; j++)
                            {
                                if (s == 1)
                                {
                                    cin >> a[i][j];
                                }
                                else if (s == 2)
                                {
                                    cin >> b[i][j];
                                }
                                else if (s == 3)
                                {
                                    if (a[i][j] == b[i][j])
                                        sum++; 
                                }
                            }
                        }
                    }
                    printf("%.2lf", (sum / (n * m)*100));
                    return 0;
                }
                
                • 0
                  @ 2024-6-15 17:55:29
                  #include <bits/stdc++.h>
                  using namespace std;
                  int a[105][105],b[105][105],m,n,sum = 0;
                  int main()
                  {
                      cin >> m >> n;
                      for (int i = 1;i <= m;i++)
                      {
                          for (int j = 1;j <= n;j++)
                          {
                              cin >> a[i][j];
                          }
                      }
                      for (int i = 1;i <= m;i++)
                      {
                          for (int j = 1;j <= n;j++)
                          {
                              cin >> b[i][j];
                          }
                      }
                      for (int i = 1;i <= m;i++)
                      {
                          for (int j = 1;j <= n;j++)
                          {
                              if (a[i][j] == b[i][j])
                                  sum += 1;
                          }
                      }
                      cout << fixed << setprecision(2) << sum * 100.0 / (n * m);
                      return 0;
                  }
                  
                  • 0
                    @ 2024-6-14 22:48:26
                    #include <iostream>
                    #include <iomanip>
                    using namespace std;
                    int main()
                    {
                        int a[10005],b[10005],m,n,ac = 1;
                        double sum;
                        cin >> m >> n;
                        for (int i = 0; i <= n * m - 1; i++)
                        {
                            cin >> a[i];
                        }
                        for (int i = 0; i <= n * m - 1; i++)
                        {
                            cin >> b[i];
                        }
                        for (int i = 0; i <= n * m - 1; i++)
                        {
                            if (a[i] == b[i])
                            {
                                ac += 1;
                            }
                        }
                        ac -= 1;
                        sum = (double)ac / (n * m) * 100;
                        cout << fixed << setprecision(2) << sum;
                    }
                    
                    • 0
                      @ 2024-6-14 22:38:37
                      #include <iostream>
                      #include <iomanip>
                      using namespace std;
                      int main()
                      {
                          int aa[100][100];
                          int bb[100][100];
                          int  a ,b;
                          int sum = 0;
                          cin >> a >> b;
                          for (int i = 1; i <= a; i++)
                          {
                              for (int j = 1; j <= b; j++)
                              {
                                  cin >> aa[i][j];
                              }
                          }
                          for (int i = 1; i <= a; i++)
                          {
                              for (int j = 1; j <= b; j++)
                              {
                                  cin >> bb[i][j];
                              }
                          }
                          
                          for (int i = 1; i <= a; i++)
                          {
                              
                              for (int j = 1; j <= b; j++)
                              {
                                  if (aa[i][j] == bb[i][j] )
                                  {
                                      sum++;
                                  }
                                  
                                  
                              }
                              
                          }
                          cout << fixed << setprecision(2) << float(sum * 100) / a / b ;
                          return 0;
                      }
                      
                      
                      • 0
                        @ 2023-11-21 19:22:23
                        #include <iostream>
                        #include <cstdio>
                        using namespace std;
                        int main()
                        {
                            int m , n , a[100][100] , b[100][100] , c = 0;
                            double num;
                            cin >> m >> n;
                            for(int i = 0;i < m;i++)
                            {
                                for(int j = 0;j < n;j++)
                                {
                                    cin >> a[i][j];
                                }
                            }
                            for(int i = 0;i < m;i++)
                            {
                                for(int j = 0;j < n;j++)
                                {
                                    cin >> b[i][j];
                                }
                            }
                            for(int i = 0;i < m;i++)
                            {
                                for(int j = 0;j < n;j++)
                                {
                                    if(a[i][j] == b[i][j])
                                    {
                                        c++;
                                    }
                                }
                            }
                            num = c * 1.0 / (m * n) * 100;
                            printf("%.2f",num);
                            return 0;
                        }
                        
                        • 0
                          @ 2023-7-25 18:13:53

                          这个题,很简单: using namespace std; int main() { int m,n; cin>>m>>n; int a[105][105],b[105][105]; for (int i=1 ; i<=m ; i++){ for (int j=1 ; j<=n ; j++){ int num; cin>>num; a[i][j]=num;}} for (int i=1 ; i<=m ; i++){ for (int j=1 ; j<=n ; j++){ int num; cin>>num; b[i][j]=num;}} double sum=0; for (int i=1 ; i<=m ; i++){ for (int j=1 ; j<=n ; j++){ if (a[i][j] == b[i][j]) sum++;}} cout<<fixed<<setprecision(2)<<sum/(m*n)*100; return 0; } Where is 头文件?👀️

                          • @ 2023-8-4 8:07:29

                            你他喵的压缩流一行解决是吧

                        • 0
                          @ 2023-7-15 11:13:00
                          #include <bits/stdc++.h>
                          using namespace std;
                          int a[100][100],b[100][100];
                          int main(){
                          	double m,n,f,s=0.0;
                              cin>>m>>n;
                              for (int i=0;i<m;i++){
                                  for (int j=0;j<n;j++) cin>>a[i][j];
                              }
                              for (int i=0;i<m;i++){
                                  for (int j=0;j<n;j++) cin>>b[i][j];
                              }
                              for (int i=0;i<m;i++){
                                  for (int j=0;j<n;j++){
                                      s+=a[i][j]==b[i][j];
                                  }
                              }
                              f=s/(m*n);
                              printf("%.2lf",f*100);
                          	return 0;
                          }
                          
                          • 0
                            @ 2023-7-13 23:27:01

                            思路:

                            在获取到两张图片的数据以后,写一个for循环嵌套来遍历这两个二维数组,如果两个点相等,就将用于统计有多少个点是一样的的计数变量sum+1, 然后输入sum占总数的百分之几就好了

                            输出的时候要注意三个点:

                            1、输出百分数,但不需要带百分号,要将算出来的数*100再输出

                            2、记得把sum或总数其中一个强制转类型为double或者设定sum的时候就把sum设为double类型的

                            3、记得保留两位小数

                            //这次代码有一点长,所以用回车做了分段处理
                            #include <iostream>
                            #include <iomanip>
                            using namespace std;
                            
                            int main()
                            {
                                int m,n,photo1[105][105],photo2[105][105],total,sum = 0;
                                cin >> m >> n;
                                total = m * n;
                            
                                for(int i = 0;i < m;i++)
                                {
                                    for(int j = 0;j < n;j++)
                                        cin >> photo1[i][j];
                                }
                            
                                for(int i = 0;i < m;i++)
                                {
                                    for(int j = 0;j < n;j++)
                                        cin >> photo2[i][j];
                                }
                            
                                for(int i = 0;i < m;i++)
                                {
                                    for(int j = 0;j < n;j++)
                                    {
                                        if(photo1[i][j] == photo2[i][j])
                                            sum++;
                                    }
                                }
                            
                                cout << fixed << setprecision(2) << (double)sum / total * 100;
                                return 0;
                            }
                            
                            • 0
                              @ 2023-7-13 22:38:15

                              //把hetao120092的代码稍加修改~~~

                              #include <iostream>
                              #include <iomanip>
                              
                              using namespace std;
                              
                              int main()
                              {
                                  int m,n;//ans去掉
                                  cin>>m>>n;
                                  int a[105][105],b[105][105];
                                  for (int i=1 ; i<=m ; i++)
                                  {
                                      for (int j=1 ; j<=n ; j++)
                                      {
                                          int num;
                                          cin>>num;
                                          a[i][j]=num;
                                      }
                                  }
                                  for (int i=1 ; i<=m ; i++)
                                  {
                                      for (int j=1 ; j<=n ; j++)
                                      {
                                          int num;
                                          cin>>num;
                                          b[i][j]=num;
                                      }
                                  }
                                  double sum=0;
                                  for (int i=1 ; i<=m ; i++)
                                  {
                                      for (int j=1 ; j<=n ; j++)
                                      {
                                          if (a[i][j] == b[i][j])
                                          {
                                              sum++;
                                          }
                                      }
                                  }
                                  double ans=sum*1.0/(m*n)*100;
                                  cout<<fixed<<setprecision(2)<<ans;//ans就够了
                                  return 0;
                              }
                              
                              • 0
                                @ 2023-7-13 17:48:13
                                #include <iostream>
                                #include <iomanip>
                                using namespace std;
                                int main()
                                {
                                    int m,n;
                                    cin >> m >> n;
                                    int a[1000][1000];
                                    int b[1000][1000];
                                    int sum = 0;
                                    for(int i = 1;i<=m;i++)
                                    {
                                        for(int j = 1;j<=n;j++)
                                        {
                                            cin >> a[i][j];
                                        }
                                    }
                                    for(int i = 1;i<=m;i++)
                                    {
                                        for(int j = 1;j<=n;j++)
                                        {
                                            cin >> b[i][j];
                                        }
                                    }
                                    for(int i = 1;i<=m;i++)
                                    {
                                        for(int j = 1;j<=n;j++)
                                        {
                                            if(a[i][j] == b[i][j])
                                            {
                                                sum += 1;
                                            }
                                        }
                                    }
                                    float num = 0;
                                    num = 100 * (float(sum) / float(m * n));
                                    cout << fixed << setprecision(2) << num;
                                    return 0;
                                }
                                
                                • -3
                                  @ 2023-7-13 16:53:43
                                  #include <iostream>
                                  #include <iomanip>
                                  
                                  using namespace std;
                                  
                                  int main()
                                  {
                                      int m,n,an=1145145;
                                      cin>>m>>n;
                                      int a[105][105],b[105][105];
                                      for (int i=1 ; i<=m ; i++)
                                      {
                                          for (int j=1 ; j<=n ; j++)
                                          {
                                              int num;
                                              cin>>num;
                                              a[i][j]=num;
                                          }
                                      }
                                      for (int i=1 ; i<=m ; i++)
                                      {
                                          for (int j=1 ; j<=n ; j++)
                                          {
                                              int num;
                                              cin>>num;
                                              b[i][j]=num;
                                          }
                                      }
                                      double sum=0;
                                      for (int i=1 ; i<=m ; i++)
                                      {
                                          for (int j=1 ; j<=n ; j++)
                                          {
                                              if (a[i][j] == b[i][j])
                                              {
                                                  sum++;
                                              }
                                          }
                                      }
                                      double ans=sum/(m*n)*100;
                                      cout<<fixed<<setprecision(2)<<an;
                                      return 0;
                                  }
                                  

                                  警告: 不要复制(直接复制会出错)

                                • 1

                                信息

                                ID
                                289
                                时间
                                1000ms
                                内存
                                128MiB
                                难度
                                5
                                标签
                                递交数
                                1594
                                已通过
                                630
                                上传者