Program Design Ladder Tournament Test Question Set L1-L4

7-1 Seeking corn yield per mu

As shown in the picture, there is a cornfield located on the north side of an east-west highway and on the east side of the north-south highway. The first vertex is 750 meters from the North-South Road and 550 meters from the East-West Road; the second vertex is 850 meters from the North-South Road and 50 meters from the East-West Road; the third vertex is 50 meters from the North-South Road and 250 meters from the East-West Road; the fourth vertex is from the north-south The road is 150 meters away, and the east-west road is 450 meters away. Corn is planted on this plot, and the total harvested yield is 120 tons. What is the yield of corn per mu? It is required to write a program to solve the problem.Insert picture description here
Insert picture description here

Programming requirements :
There must be at least two functions in the program. The functions of the functions are as follows:
(1) Write a function to calculate the distance between two points, so that the length of the four sides of the quadrilateral in the arithmetic graph and the length of a diagonal line;
(2) Write a function to calculate the triangulation area according to Helen's formula;

Description
1 square meter = 0.0015 acres

Input format:
Enter 8 real numbers in one line, separated by spaces between each number. Two groups from left to right, each group represents the abscissa and ordinate of a point.

Output format:
output a real number, representing the yield of corn per mu. The output retains 2 decimal places.

Input sample:
750 550 850 50 50 250 150 450
Output sample:
326.53

#include <stdio.h>
#include <math.h>
double d(double x1,double y1,double x2,double y2)
{
    
    
    double d;
    d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    return d;
}
double area(double L1,double L2,double L3)
{
    
    
    double p,area;
    p=0.5*(L1+L2+L3);
    area=sqrt(p*(p-L1)*(p-L2)*(p-L3));
    return area;
}
int main()
{
    
    
  double xA,yA,xB,yB,xC,yC,xD,yD;
  double AB,AD,BD,CD,BC;
  double area1,area2,sum=0,Ssum;
  scanf("%lf %lf %lf %lf %lf %lf %lf %lf",&xA,&yA,&xB,&yB,&xC,&yC,&xD,&yD);
  AB=d(xA,yA,xB,yB);
  AD=d(xA,yA,xD,yD);
  BD=d(xB,yB,xD,yD);
  CD=d(xC,yC,xD,yD);
  BC=d(xB,yB,xC,yC);
  //printf("%f %f %f %f %f \n",AB,AD,BD,CD,BC);
  area1=area(AB,AD,BD);
  area2=area(BD,CD,BC);
  //printf("%f %f\n",area1,area2);
  sum=area1+area2;
  Ssum=sum*0.0015;
  printf("%.2f",120000/Ssum);
    return 0;
}

Insert picture description here

7-2 Determine whether a given integer is a prime number [programming questions]

A prime number refers to a number that is not divisible by other natural numbers except 1 and itself among the natural numbers greater than 1. This question requires writing a program to determine whether the number entered from the keyboard is a prime number. If it is a prime number, output the information XX is prime number. and output the information XX is not prime number.
Programming requirements:
Write a function IsPrime() to determine prime numbers. If it is a prime number, the return value of the function is that number, otherwise the return value of the function is 0.
Insert picture description here

Input format :
Enter a non-negative integer in one line.

Output format:
output the information XX is prime number. or XX is not prime number. in one line.

Input example 1:
5
Output example 1:
5 is prime number
Input example 2:
121
Output example 2:
121 is not prime number.

#include <stdio.h>
#include <math.h>
int isPrime(int number)
{
    
    
    int i;
    if(number==1) return 0;
    else
        for(i=2;i<sqrt(number);i++)
            if(!(number%i)) return 0;
    return number;
}
int main()
{
    
    
    int n;
    scanf("%d",&n);
    if(isPrime(n)) printf("%d is prime number.",n);
    else printf("%d is not prime number.",n);
    return 0;
}

Insert picture description here

7-3 Sum of N numbers

The requirement of this question is very simple, that is, find the sum of N numbers. The trouble is that these numbers are given in the form of the numerator/denominator of a rational number, and the sum you output must also be in the form of a rational number.

Input format :
Input the first line to give a positive integer N (≤100). The following line gives N rational numbers in the format a1/b1 a2/b2... Make sure that all numerators and denominators are within the long integer range. In addition, the sign of a negative number must appear in front of the numerator.

Output format:
output the simplest form of the above-mentioned number sum-that is, the result is written as an integer part of the fraction, where the fraction part is written as a sub/denominator. The numerator is required to be smaller than the denominator, and they have no common factor. If the integer part of the result is 0, only the fractional part is output.
Insert picture description here

Input example 1:
5
2/5 4/15 1/30 -2/60 8/3
Output example 1:
3 1/3
Input example 2:
2
4/3 2/3
Output example 2:
2
input Sample 3:
3
1/3 -1/6 1/8
Output Sample 3:
7/24

#include <stdio.h>
#include <string.h>
#include <math.h>
int gcd(int q,int b)
{
    
    
    int r;
    r=q%b;
    while(r)
    {
    
    
        q=b;
        b=r;
        r=q%b;
    }
    return b;
}
int main()
{
    
    
    int N,i,m;
    long long int sum1=0,sum=1;
    scanf("%d\n",&N);
    int a,b;
    for(i=0;i<N;i++)
    {
    
    
        scanf("%d/%d",&a,&b);
        sum1*=b;
        sum1+=a*sum;
        sum*=b;
        m=gcd(sum1,sum);
        sum1/=m;
        sum/=m;
    }
    //printf("%d %d\n",sum1,sum);
    if(sum1%sum==0) printf("%lld",sum1/sum);
    else if(sum1&&(sum1<sum)) printf("%lld/%lld",sum1,sum);
    else printf("%lld %lld/%lld",sum1/sum,sum1%sum,sum);
    return 0;
}

Insert picture description here

7-4 Find the quotient and remainder of dividing A by B

Calculate the quotient and remainder of A/B, where the dividend A is a non-negative integer with no more than 1000 digits, and the divisor B is any non-negative integer that does not exceed the range of the int type. You are required to output the quotient Q and the remainder R.

Input format :
input A and B in one line, separated by a space.

Output format:
output Q and R in sequence on one line, separated by spaces.
Insert picture description here

Input sample:
1234567891234567893 23
Output sample:
53676864836285560 13

#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
    
    
    char a[1001];//被除数
    int b[1001];//存商
    int B,i=0,R=0,n,k=0;
    scanf("%s",a);
    scanf("%d",&B);
    n=strlen(a);
    while(a[i]=='0'&&i<n)//被除数前边可能以零开头,计零个数
        i++;
    for(;i<n;i++)
    {
    
    
        R=R*10+(a[i]-'0');
        b[k++]=R/B;
        R%=B;
    }
    if(k==1) printf("%d %d",b[0],R);//如果是商是一位数,直接输出
    else
    {
    
    
        i=0;
        while(b[i]==0&&i<k)
            i++;
        for(;i<k;i++)
             printf("%d",b[i]);
        printf(" %d",R);
    }
    return 0;
}

Insert picture description here

The reference answer provided by Mr. Zheng, 嘤嘤嘤

#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
    
    
    char a[1001]={
    
    '\0'};//被除数
    int B,i=0,r=0,k=0,sign=0,flag=0;
    scanf("%s %d",a,&B);
    while(a[i]=='0'&&i<strlen(a))
        i++;
    for(;i<strlen(a);i++)
    {
    
    
        r=r*10+(a[i]-'0');
        if(r<B&&sign==1)
        {
    
    
            printf("0");
            flag=1;
        }
        if(r>=B)
        {
    
    
            printf("%d",r/B);
            r%=B;
            sign=1;
            flag=1;
        }

    }
    if(flag) printf(" %d",r);
    else printf("0 %d",r);
    return 0;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_52765554/article/details/115188382