Introduction to the classic algorithm contest (second edition) the first chapter exercises

Problem 1-1 Mean
input three integers, their average output, three decimal places.

#include <stdio.h>
int main ()
{
Double A, B, C; // must be well-defined data type to be entered
Scanf ( "LF%%% LF LF", & A, & B, & C);
the printf ( "% .3lf", (A + B + C) /3.0);
}


Problem 1-2 Temperature
input Fahrenheit f, corresponding to the output temperature in Celsius c, three decimal places. Tip c = 5 (f-32) / 9

#include <stdio.h>
int main()
{
float f,c;
scanf("%f",&f);
c=5*(f-32)/9;
printf("%.3f",c);
return 0;
}

 

Exercises for 1-3 and
enter a positive integer n, the output value of 1 + 2 + 3 ,,, + n is.

#include <stdio.h>
int main ()
{int n-;
Scanf ( "% D", & n-);
the printf ( "% D", (n-* (n-+. 1) / 2));
return 0;
}


Problem 1-4 the sine and cosine
inputs a positive integer n (n <360>), the sinusoidal output of degree n, cosine function values.

#include <stdio.h>
#include <math.h>
#define PI 4.0 * atan (1.0) // do not equate
int main ()
{int n-;
Scanf ( "% D", & n-);
IF (n-< 360)
{
the printf ( "% LF \ n-", SiN ((PI * n-) / 180 [));
the printf ( "% LF \ n-", COS ((PI * n-) / 180 [));
}
the else
the printf ( " error the iNPUT ");
return 0;
}


exercise 1-5 discount
clothes 95 yuan, if spending $ 300 to play 15%, input the number of clothes to buy, the amount of output required to pay (unit: RMB), two reserved decimal places.

#include <stdio.h>
int main()
{ int n;
float sum;//先定义好sum
scanf("%d",&n);
if(n*95>=300)
{
sum=n*95*0.85;
printf("%.2f\n",sum);

}
The else
SUM n-* = 95;
the printf ( "%. 2F.", SUM);
return 0;
}


Problem 1-6 triangular
input triangle side length three values (both positive integers), it is determined whether the right triangle three side lengths, if so, the output yes, if not, the output no, if can not form a triangle, then the output not a triangle.

#include <stdio.h>
int main()
{
float a,b,c;
scanf("%f%f%f",&a,&b,&c);
if(((a+b)<c||(a+c)<b||(b+c)<a||a<0||b<0||c<0))
printf("not a triangle");
else if ((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a))
{
printf("yes");
}
else
{
printf("no");
}
return 0;
}


Problem 1--7 year
enter the year, it is determined whether or not a leap year. If so, then the output yes, otherwise the output no; (four years a leap, leap hundred years, four hundred years and then leap)

#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
if((n%4==0&&n%100!=0)||(n%400==0))
printf("yes");
else
{
printf("no");
}
return 0;
}

 

Guess you like

Origin www.cnblogs.com/lytuser/p/11695623.html