"Introduction to the algorithm contest" on the computer practice--The first chapter _ Algorithm competition

Source: Internet
Author: User
Tags mathematical functions square root

Exercise 1-1 average (average)

Enter 3 integers, output their average value, and keep 3 decimal places.

#include <stdio.h>

int main ()
{
    int a,b,c;
    Double average;
    
    scanf ("%d%d%d", &a,&b,&c);
    Average = (a+b+c)/3.0;
    printf ("%.3lf\n", average);
    
    return 0;
}

Exercise 1-2 temperature (temperature)

Enter Fahrenheit temperature F, output corresponding to Celsius temperature C, retain 3 decimal places. Tip: C=5 (f-32)/9.

#include <stdio.h>

int main ()
{
    double f,c;
    
    scanf ("%lf", &f);
    c = 5* (f-32)/9;
    printf ("%.3lf\n", c);
    
    return 0;
}

Exercise 1-3 continuous and (sum)

Input positive integer n, output 1+2+ The value of the +n. Tip: The goal is to solve the problem, not to practice programming.

Analysis: 1+2+ +n = (1+n) *N/2

#include <stdio.h>

int main ()
{
    int n,sum;
    
    scanf ("%d", &n);
    sum = (1+n) *n/2;
    printf ("%d\n", sum);
    
    return 0;
}

Exercise 1-4 sine and cosine (sincos)

Input positive integer n (n<360), output the sine, cosine function value of n degrees. Tip: Use mathematical functions.

Analysis: The angle must be converted to radians. Angle 180 degree corresponds to Radian 1pi

#include <stdio.h>
#include <math.h>

int main ()
{
    double n/* angle * * *,
	       x/* radians.
	Const double PI = 4.0*atan (1.0);
    
    scanf ("%lf", &n);
    x = N/180*pi;
    printf ("%lf\n", Sin (x));
    printf ("%lf\n", cos (x));
    
    return 0;
}

Exercise 1-5 distance (distance)

Enter 4 floating-point number x1,y1,x2,y2 and output the distance of the midpoint (x1,y1) point (x2,y2) of the planar coordinate system.

Analysis: Plane distance between two points: D=sqrt ((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1))

sqrt () Calculate the square root of arithmetic

#include <stdio.h>
#include <math.h>

int main ()
{
    double x1,y1,x2,y2,d;
    
    scanf ("%lf%lf%lf%lf", &x1,&y1,&x2,&y2);
    D = sqrt ((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
    printf ("%lf\n", d);
    
    return 0;
}

Exercise 1-6 even (odd)

Enter an integer to determine if it is an even number. If it is, the output is "yes", otherwise "no" is output. Hint: can be judged by a variety of methods.

Analysis: In addition to% 2, today we learned a new method: if (n&1), with 1 bitwise AND, the result is 1 is odd, 0 is even

#include <stdio.h>

int main ()
{
    int n;
    
    scanf ("%d", &n);
    if (n&1) printf ("No"),/* is odd/
    else printf ("yes"),/* is even/return 
    
    0;
}


Exercise 1-7 Discount (discount)

A clothes 95 yuan, if the consumption of 300 yuan, can play 85 percent. Enter the number of pieces of clothing purchased, output the amount to be paid (in units: yuan), retain two decimal places.

#include <stdio.h>

int main ()
{
    int n;
    Double money;
    
	scanf ("%d", &n);
	Money = 95*n;
	if (money>=300)
	    money*=0.85;
	printf ("%.2lf\n", money);
	
    return 0;
}

Exercise 1-8 Absolute Value (ABS)

Enter a floating-point number, output its absolute value, and retain two decimal places.

#include <stdio.h>

int main ()
{
    double n;
    
	scanf ("%lf", &n);
	if (n>0) 
	    printf ("%.2lf\n", n);
	else 
	    printf ("%.2lf\n",-N);
	
    return 0;
}

Exercise 1-9 triangle (triangle)

Enter a triangular three-side length value (all positive integers) to determine whether it can be three sides long for a right triangle. If so, the output is "yes" and if not, the "no" is output. If the triangle cannot be formed at all, the "not a triangle" is output.

Analysis: First to determine whether the triangle can be formed, and further determine whether to constitute a right-angled triangle.

#include <stdio.h>

int main ()
{
    int a,b,c,t;
    
    scanf ("%d%d%d", &a,&b,&c);
    if (a>c)
        {t=a;a=c;c=t;}
    if (b>c)
        {t=b;b=c;c=t;} /* These 2 if statements ensure that C is the longest edge */ 
        
    if (a+b<=c)
        printf ("Not a triangle\n");
    else
        if (a*a+b*b = = c*c)
            printf ("yes\n");
        else
            printf ("no\n");
    
    return 0;
}

Exercise 1-10 years (year)

Enter the year to determine if it is a leap years. If it is, the output is "yes", otherwise "no" is output. Hint: It is not enough to simply judge the remainder divided by 4.

#include <stdio.h>

int main ()
{
    int y;
    
    scanf ("%d", &y);
    if (y%4==0 && y%100!=0 | | | y%400==0)
        printf ("yes\n");
    else
        printf ("no\n");
    
    return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.