14 条题解

  • 4
    @ 2023-6-3 23:33:44

    题目:

    根据题目,我们可以得知,需要的库要有:iostream(或cstdio,根据自己喜好选择,这里以iostream为例)、cmath、iomanip PS:本题需用勾股定理,勾股定理讲的是在一个直角三角形内,两条直边长的平方和(a²+b²)等于斜边长的平方(c²),用公式表达为:a²+b²=c²,求斜边长c,则表达为:c=√a²+b²

    思路:

    定义双精度浮点型变量

    输入变量的值

    用减法得到两条直边长(把它们画成三角形)

    使用勾股定理,得到斜边长(即AB长)

    输出斜边长(或为AB长)

    偷懒简化:

    把计算写在输出里

    解析:

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    int main(){
    	double Xa, Ya, Xb, Yb, a, b;
    	cin >> Xa >> Ya;
    	cin >> Xb >> Yb;
    	a = Yb - Ya;
    	b = Xb - Xa;
    	cout << fixed << setprecision(3) << sqrt(pow(a, 2) + pow(b, 2)) << endl;
    	return 0;
    }
    
    • 2
      @ 2023-6-4 10:53:40

      首先,我们定义变量: double xa,xb,ya,yb; 然后,我们读入:std::scanf("%lf%lf%lf%lf",&xa,&ya,&xb,&yb); 再运算: sqrt(pow(yb-ya,2)+pow(xb-xa,2)) 最后带到读出里:std::printf("%.3lf",sqrt(pow(yb-ya,2)+pow(xb-xa,2))) 完整代码:

      #include <bits/stdc++.h>
      int main(){double xa,xb,ya,yb;std::scanf("%lf%lf%lf%lf",&xa,&ya,&xb,&yb);std::printf("%.3lf",sqrt(pow(yb-ya,2)+pow(xb-xa,2)));return 0;}
      

      格式化:

      #include <bits/stdc++.h>
      int main()
      {
          double xa,xb,ya,yb;
          std::scanf ("%lf %lf %lf %lf",&xa,&ya,&xb,&yb);
          std::printf ("%.3lf",sqrt (pow (yb - ya,2) + pow (xb - xa,2)));
          return 0;
      }
      
      • 2
        @ 2023-6-2 9:55:30

        解析

        求两点之间的长度,其实就是勾股定理求斜边

        image image

        题解

        #include <iostream>
        #include <cstdio>
        #include <cmath>
        using namespace std;
        int main()
        {
            double Xa, Ya;               // A的坐标
            double Xb, Yb;               // B的坐标
            double AB;                   // 线段AB的长度
            cin >> Xa >> Ya >> Xb >> Yb; // 输入所有坐标点
            double Xab = Xb - Xa;        
            double Yab = Yb - Ya;
            AB = sqrt(Xab * Xab + Yab * Yab);
            printf("%.3lf", AB);
            return 0;
        }
        
        • 1
          @ 2024-2-17 21:09:57

          #简略代码

          #include <bits/stdc++.h>
          int main(){double aa, ab, ba, bb;scanf("%lf%lf%lf%lf", &aa, &ab, &ba, &bb);double x = sqrt((aa - ba) * (aa - ba) + (ab - bb) * (ab - bb));printf("%.3f", x);}
          
          • 1
            @ 2023-6-4 19:27:55
            #include <iostream>
            #include <iomanip>
            #include <cmath>
            usinlg namespace std;
            int main(){
            	double Xa, Ya, Xb, Yb, a, b;
            	cin >> Xa >> Ya;
            	cin >> Xb >> Yb;
            	a = Yb - Ya;
            	b = Xb - Xa;
            	cout << fixed << setprecision(3) << sqrt(pow(a, 2) + pow(b, 2)) << endl;
            	return 0;
            }
            
            
            • 1
              @ 2023-6-3 17:33:51
              #include <iostream>
              #include <iomanip>
              #include <cmath>
              using namespace std;
              int main()
              {
                  double xa, ya, xb, yb;
                  cin >> xa >> ya;
                  cin >> xb >> yb;
                  double line = sqrt(pow(abs(xa-xb), 2) + pow(abs(ya-yb), 2));
                  cout << fixed << setprecision(3) << line;
                  return 0;
              }
              
              • 0
                @ 2024-6-1 20:11:50

                题解 c++ #include <bits/stdc++.h> //hetao4040809 using namespace std; int main() { double Xa, Ya; double Xb, Yb; double AB; cin >> Xa >> Ya >> Xb >> Yb; double Xab = Xb - Xa; double Yab = Yb - Ya; AB = sqrt(Xab * Xab + Yab * Yab); printf("%.3lf", AB); return 0; }

                • 0
                  @ 2024-5-24 17:08:29
                  #include<iostream>
                  #include<cstdio>
                  #include<cmath>
                  #include<iomanip>
                  using namespace std;
                  int main()
                  {
                      double a,b,c,d,e;
                      cin >> a >> b;
                      cin >> c >> d;
                      double f = c - a;
                      double g = b - d;
                      e = sqrt(f * f + g * g);
                      printf("%.3lf", e);
                      return 0;
                  }
                  
                  • 0
                    @ 2024-4-21 13:44:50

                    #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { double xa, ya, xb, yb; cin >> xa >> ya; cin >> xb >> yb; double line = sqrt(pow(abs(xa-xb), 2) + pow(abs(ya-yb), 2)); cout << fixed << setprecision(3) << line; return 0; }

                    • 0
                      @ 2023-8-10 23:53:22

                      本题需要用勾股定理(在Rt△中,a²+b²=c²)解决,引申为代码可表示为sqrt(开方)和pow(乘方)

                      思路解决,上代码

                      #include <bits/stdc++.h>
                      using namespace std;
                      int main()
                      {
                          double xa,ya,xb,yb;
                          cin>>xa>>ya>>xb>>yb;
                          double a=yb-ya,b=xb-xa;
                          cout<<fixed<<setprecision(3)<<sqrt(pow(a,2)+pow(b,2));
                          return 0;
                      }
                      

                      搞定

                      • 0
                        @ 2023-8-3 8:05:13

                        题目描述

                        已知线段的两个端点的坐标 A(Xa,Ya)B(Xb ,Yb) ,求线段 AB 的长度。

                        输入格式

                        输入。

                        共两行。

                        第一行是两个实数 Xa,YaXa**,Ya,即 AA** 的坐标。

                        第二行是两个实数 Xb,YbXb**,Yb,即 BB** 的坐标。

                        输入中所有实数的绝对值均不超过 10000​10000​。

                        输出格式

                        输出。

                        一个实数,即线段 ABAB 的长度,保留到小数点后 33 位。

                        完整代码(基于iostream以及cmath):

                        #include <iostream>
                        #include <cmath>
                        using namespace std;
                        int main()
                        {
                        	double  xa,ya,xb,yb;
                            double c;
                        	cin >>xa>>ya;
                            cin >>xb>>yb;
                            c = sqrt(pow((xb-xa),2)+pow((yb-ya),2));
                            cout.setf(ios::fixed);
                            cout.setf(ios::showpoint);
                            cout.precision(3);
                            cout << c;
                        	return 0;
                        }
                        
                        • 0
                          @ 2023-7-19 19:11:16

                          计算线段长度

                          思路:

                          求出线段分别长多少→通过勾股定理求斜线→保留三位小数并输出

                          上代码:

                          #include <iostream>
                          #include <iomanip>
                          #include <cmath>
                          using namespace std;
                          int main()
                          {
                              double xa,ya,xb,yb;
                              cin >> xa >> ya >> xb >> yb;
                              double a = abs(xa - xb);
                              double b = abs(ya - yb);
                              double n = sqrt(pow(a,2) + pow(b,2));
                              cout << fixed << setprecision(3) << n;
                              return 0;
                          }
                          
                          • 0
                            @ 2023-7-5 22:30:39

                            题目描述

                            已知线段的两个端点的坐标 A(Xa,Ya)B(Xb ,Yb) ,求线段 AB 的长度。

                            输入格式

                            输入。

                            共两行。

                            第一行是两个实数 Xa,YaXa**,Ya,即 AA** 的坐标。

                            第二行是两个实数 Xb,YbXb**,Yb,即 BB** 的坐标。

                            输入中所有实数的绝对值均不超过 1000010000

                            输出格式

                            输出。

                            一个实数,即线段 ABAB 的长度,保留到小数点后 33 位。

                            #include <iostream>
                            #include <cstdio>
                            #include <cmath>
                            using namespace std;
                            int main()
                            {
                            	double  xa,ya,xb,yb;
                                double c;
                            	cin >>xa>>ya;
                                cin >>xb>>yb;
                                c = sqrt(pow((xb-xa),2)+pow((yb-ya),2));
                                printf("%.3lf",c);
                            	return 0;
                            }
                            
                            • 0
                              @ 2023-6-3 10:13:31

                              思路:

                              获取数据后先求出两边边长,用abs()以防出现负数,最后用勾股定理a² + b² = c²求出c²的值,再用sqrt()求出c的长度,c的长度即为线段长度

                              #include <cstdio>
                              #include <cmath>
                              using namespace std;
                              
                              int main()
                              {
                                  double xa,ya,xb,yb,a,b;
                                  scanf("%lf %lf",&xa,&ya);
                                  scanf("%lf %lf",&xb,&yb);
                                  a = abs(xa - xb);
                                  b = abs(ya - yb);
                                  printf("%.3lf",sqrt(pow(a,2) + pow(b,2)));
                                  return 0;
                              }
                              
                              • @ 2023-6-3 21:33:31

                                平方不是非负的吗,绝对值可以去掉

                            • 1

                            信息

                            ID
                            125
                            时间
                            1000ms
                            内存
                            128MiB
                            难度
                            7
                            标签
                            递交数
                            5325
                            已通过
                            1069
                            上传者