码题集周训(一题多解)

1.求圆面积和周长

请编写一个简单程序,输入半径,输出圆面积和周长。(PI是3.1415926)

输入格式:
double型

输出格式:
分2行输出圆面积和周长,保留6位小数

参考代码

def main():
    PI = 3.1415926
    r = float(input())
    print("Area={:.6f}\nCircumference={:.6f}".format(PI * r ** 2, 2 * PI * r))


if __name__ == '__main__':
    main();
import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
      double r = input.nextDouble();
      double PI = 3.1415926;
      System.out.printf("Area=%.6f\n",(PI*r*r));
      System.out.printf("Circumference=%.6f",(2*PI*r));
      input.close();
   }
}
#include<bits/stdc++.h> 

using namespace std;

int main( )
{
    double x, area, perimeter;
    double PI = 3.1415926;
    scanf("%lf", &x);
    area = PI * x * x;
    perimeter = 2 * PI * x;
    printf("Area=%.6lf\nCircumference=%.6lf", area, perimeter);
    return 0;
}

2.求矩形的面积和周长

请编写一个简单程序,输入矩形的长度和宽度,输出矩形的面积和周长。

输入格式:
实型,空格分隔

输出格式:
分2行输出矩形的面积和周长,保留6位小数

参考代码

import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
        double a = input.nextDouble();
        double b = input.nextDouble();
        System.out.printf("Area=%.6f\n",a*b);
        System.out.printf("Perimeter=%.6f",2*(a+b));

      input.close();
   }
}

def main():
    a,b = map(float,input().split(' '))
    Area = a*b
    Perimeter = 2*(a+b)
    print('Area={0:.6f}'.format(Area))
    print('Perimeter={0:.6f}'.format(Perimeter))


if __name__ == '__main__':
    main();

3.求矩形的面积和周长

请编写一个简单程序,输入矩形的长度和宽度,输出矩形的面积和周长。

输入格式:
实型,空格分隔

输出格式:
分2行输出矩形的面积和周长,保留6位小数

参考代码

def main():
    #code here
    a,b = map(float,input().split(' '))
    PI = 3.1415926
    Area = a*b*PI
    print('Area = {0:.6f}'.format(Area))


if __name__ == '__main__':
    main();
import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
        double a = input.nextDouble();
        double b = input.nextDouble();
        final double PI = 3.1415926;
        System.out.print("Area = ");
        System.out.printf("%.6f",a*b*PI);
        System.out.println();
      input.close();
   }
}

4.椭圆计算

请编写一个简单程序,输入长半轴和短半轴长度,计算输出椭圆的面积。(PI是3.1415926)

输入格式:
double型,空格分隔

输出格式:
输出椭圆的面积,保留6位小数

参考代码

import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
        float d = input.nextFloat();
        float h = input.nextFloat();
        float s = d*h/2;
        System.out.printf("Area=%.2f",s);

      input.close();
   }
}
def main():
    #code here
    d, h = map(float, input().split())
    print("Area={:.2f}".format(d*h/2))


if __name__ == '__main__':
    main();

5.三角形面积

请编写一个简单程序,计算给定底面和高度的三角形面积。

输入格式:
输入float型,空格分隔

输出格式:
输出三角形面积,保留2位小数

参考代码

import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
        double d = input.nextDouble();
        double h = input.nextDouble();
        double s = d*h;
        System.out.printf("%.6f",s);
      input.close();
   }
}
def main():
    #code here
    a,h=map(float,input().split())
    print(f'{a*h:.6f}')


if __name__ == '__main__':
    main();

6.平行四边形

请编写一个简单程序,输入平行四边形底和高,输出平行四边形面积。不考虑非法输入。

输入格式:
输入实型,空格分隔。

输出格式:
输出实型

参考代码

def main():
    #code here
    m,n=map(float,input().split())
    print("%.2f"%(m*n/2))


if __name__ == '__main__':
    main();
import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
    double a = input.nextDouble();
    double b = input.nextDouble();
    System.out.printf("%.2f",(a*b)/2);
      input.close();
   }
}

7.菱形

输入菱形的两个对角线的长度,输出菱形面积。

输入格式:
输入实型,空格分隔。

输出格式:
输出实型,保留2位小数。

参考代码


def main():
    #code here
    t,b,h = map(float,input().split())
    print('{:.2f}'.format((t+b)*h/2))


if __name__ == '__main__':
    main();
import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
        double h1 = input.nextDouble();
        double h2 = input.nextDouble();
        double d = input.nextDouble();
        double s = (d*(h1+h2))/2;
        System.out.printf("%.2f",s);
      input.close();
   }
}

8.梯形

输入梯形的两个底的长度和高,输出梯形面积。

输入格式:
输入实型,空格分隔。

输出格式:
输出实型,保留2位小数。

参考代码

#include<stdio.h>
#define PI 3.1415926
int main() 
{
    double r, n;
    scanf("%lf %lf", &r, &n);
    printf("%lf\n", n/360*PI*r*r);
    return 0; 
}

9.扇形面积

输入扇形的半径r和圆心角度数n°,输出扇形面积。不考虑非法输入。(Pl=3.1415926)

输入格式: 
输入实型,空格分隔

输出格式: 
输出实型

参考代码

import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      // code here
        int x1 = input.nextInt();
        int y1 = input.nextInt();
        int x2 = input.nextInt();
        int y2 = input.nextInt();
        int x3 = input.nextInt();
        int y3 = input.nextInt();
        System.out.printf("%.2f",(1.0/2)*Math.abs(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2));
      input.close();
   }
}
def main():
    #code here
    x1,y1,x2,y2,x3,y3 = map(float,input().split(' '))
    s = 1/2 * abs(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2)
    print('{0:.2f}'.format(s))


if __name__ == '__main__':
    main();

10.三角形坐标

输入三角形三个顶点A,B,C的坐标(x,y),根据公式计算并输出三角形面积。
S=1/2 * |x1y2+x2y3+x3y1-x1y3-x2y1-x3y2|

输入格式:
依次输入三个顶点A,B,C的坐标(x,y),整型,空格分隔。

输出格式:
输出实型,保留2位小数。

参考代码

import java.util.Scanner;
import java.util.*;

class Main {

   public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int a[] = new int[9];
        for(int i=0;i<a.length;i++) {
            a[i] = scanner.nextInt();
        }
        double ab = Math.sqrt(Math.pow(a[0]-a[3], 2)+Math.pow(a[1]-a[4], 2)+Math.pow(a[2]-a[5], 2));
        double ac = Math.sqrt(Math.pow(a[0]-a[6], 2)+Math.pow(a[1]-a[7], 2)+Math.pow(a[2]-a[8], 2));
        double bc = Math.sqrt(Math.pow(a[6]-a[3], 2)+Math.pow(a[7]-a[4], 2)+Math.pow(a[8]-a[5], 2));
        double p = (ab+ac+bc)/2;
        double s = Math.sqrt(p*(p-ab)*(p-ac)*(p-bc));
        System.out.println(String.format("%.2f", s));
        scanner.close();
   }
}
def main():
    #code here
    x1,y1,z1,x2,y2,z2,x3,y3,z3=map(float,input().split())
    a=((x1-x2)**2+(y1-y2)**2+(z1-z2)**2)**0.5
    b=((x1-x3)**2+(y1-y3)**2+(z1-z3)**2)**0.5
    c=((x3-x2)**2+(y3-y2)**2+(z3-z2)**2)**0.5
    S=(a+b+c)/2
    print('%.2f'%((S*(S-a)*(S-b)*(S-c))**0.5))


if __name__ == '__main__':
    main();
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值