Example 2 a quadratic equation
Description [title]
Input coefficients a, b and c, of Equation AX 2 + BX + C = 0 of the root.
[Input Format]
Multiple sets of input data. Each set of data comprising the three coefficients a, b, c. When a = 0, the input data is completed.
[Output format]
Output equation roots, see sample output format reserved to two decimal places.
[Sample input]
1 2 1
1.0 -4.0 3.0
1.5 2.0 1.5
0 0 0
[Sample Output]
x1=x2=-1.00
x1=3.00,x2=1.00
x1 = -0.67 + 0.75i, x2 = -0.67-0.75i
(1) programming ideas.
The input coefficients a, can be divided into is not equal to a 0 and a 0 in both cases equal.
When a == 0, the end of input is calculated.
If a program can be written in a nested structure.
(2) source.
#include <stdio.h>
#include <math.h>
int main ()
{
double a,b,c,dlt,real,imag,x1,x2;
while (1)
{
scanf("%lf%lf%lf",&a,&b,&c);
if (a==0) break;
dlt=b*b-4*a*c;
if (dlt>0)
{
x1=(-b+sqrt(dlt))/(2*a);
x2=(-b-sqrt(dlt))/(2*a);
printf("x1=%.2f,x2=%.2f\n",x1,x2);
}
else if (dlt==0)
{
x1=x2=(-b)/(2*a);
printf("x1=x2=%.2f\n",x1);
}
else
{
real=(-b)/(2*a);
imag=sqrt(-dlt)/(2*a);
printf("x1=%.2f+%.2fi,x2=%.2f-%.2fi\n",real,imag,real,imag);
}
}
return 0;
}
Problem 2
2-1 triangle area
Description [title]
Any input three sides (a, b, c real), if a triangle configuration, the area is calculated and output, or output flag "No Triangle!".
Triangle area formula:
[Input Format]
Multiple sets of input data. Each set of data consists of three numbers a, b, c, representative of three sides of a triangle. When a = 0, the input data is completed.
[Output format]
Output triangle area reserved to two decimal places. Or No Triangle! Information, if the three sides of a given value can not constitute a triangle.
[Sample input]
3 4 5
6.0 6.0 6.0
1 1 3
0 0 0
[Sample Output]
6.00
15.59
No Triangle!
(1) programming ideas.
a, b, c of the three input number, only when a + b> c, a + c> b and b + c> a meet to form a triangle. Thus, by selecting the structure
if (a+b>c && a+c>b && b+c>a)
{Triangle area is calculated and output;}
else
{Output can not form a triangle prompt information;}
(2) source.
#include <stdio.h>
#include <math.h>
int main ()
{
float a,b,c,t,s;
while (1)
{
scanf("%f%f%f",&a,&b,&c);
if (a==0) break;
if (a+b>c && a+c>b && b+c>a)
{
t=(a+b+c)/2.0;
s=sqrt(t*(t-a)*(t-b)*(t-c));
printf("%.2f\n",s);
}
else
printf("No Triangle!\n");
}
return 0;
}
Xiaoyu 2-2 home electricity
This question is selected from the Los Valley exam (https://www.luogu.org/problem/P1422).
Description [title]
Summer, and each household's electricity consumption have increased a lot, the corresponding electricity also post more. Xiaoyu home today received a notice electricity. Xiaoyu see written above: According to Min valence requirements [2006] No. 27, and monthly electricity consumption 150 kwh per kWh 0.4463 yuan the following sections performed monthly electricity consumption in some 151 to 400 kilowatt-hours per kWh 0.4663 yuan execution, the monthly electricity consumption 401 kwh and more parts per kWh 0.5663 yuan execution; Xiaoyu want to test themselves, the number of tariff on electricity bills payable notice in the end it is correct. Write a program, known total electricity, according to price regulations, payable calculated electricity should be.
[Input Format]
An integer that indicates the total power (in units of kilowatt hours), not more than 10,000.
[Output format]
(1 unit in the dollars, reserved to the decimal point) after the output of a number, a decimal retained.
[Sample input]
267
[Sample Output]
121.5
(1) programming ideas.
The case of monthly consumption of x, which is divided into three gradient charging rule.
In a first gradient (x <= 150), a first electricity y = * gradient unit consumption of electricity = 0.4463 * x;
In the second gradient (150 <x <= 400), y = power * electricity second gradient portion of the second portion of the gradient in the first unit of electricity + * a first gradient gradient power unit of electricity = (x-150) * 0.4663 + 150 * 0.4463.
In the third gradient (x> 400), the third portion of the gradient y = electricity power unit portion of a third gradient of electricity * + * a second portion of the gradient of the second electric part unit of electricity + gradient in a first gradient of the first electric * gradient unit of electricity = (x-400) * 0.5663 + 250 * 0.4663 + 150 * 0.4463.
A simple multi-branch structure can be solved.
(2) source.
#include <stdio.h>
int main ()
{
int x;
double y;
scanf("%d",&x);
if (x<=150) y=0.4463*x;
else if (x<=400) y=150*0.4463+(x-150)*0.4663;
else y = 150 * 0.4463 + 250 * 0.4663 + (x-400) * 0.5663;
printf("%.1lf\n",y);
return 0;
}
2-3 P1909 buy pencils
This question is selected from the Los Valley exam (https://www.luogu.org/problem/P1909).
Description [title]
P n Teachers need to go to the store to buy pencils as children participate NOIP gift. She found the store a total of three kinds of packaging pencil, pencil in a number of different packaging may be different, prices may vary. To be fair, P teacher decided to only buy the same package of pencils.
Shops are not allowed to open pencil packaging, so P teacher may need to buy more than n pencils enough for the children to send gifts.
Now P teacher wanted to know, when the number of stores in each package are sufficient, at least enough to buy a minimum of n pencil how much it costs.
[Input Format]
The first line contains a positive integer n, the number of pencil required.
The next three rows with two positive integers a packaged pencil described: wherein the first integer represents the number 1 pencil within such a package, the second integer represent the price of such a package.
Ensure that all positive integer number 7 is not more than 10,000.
[Output format]
An integer representing the money it takes at least P teacher.
[Sample input]
57
2 2
50 30
30 27
[Sample Output]
54
(1) programming ideas.
Let P number of teacher pencil purchase is n, the number of pencils in a certain package is a, such a package price b. Package Number P teacher needs to buy a package of pencil c.
Obviously, if n is divisible by a, then c = n / a; otherwise c = n / a + 1.
Seeking three kinds of packaging to a minimum value c * b.
(2) source.
#include <stdio.h>
int main ()
{
int n,a,b,c,i,min;
scanf("%d",&n);
scanf("%d%d",&a,&b);
if (n%a==0) c=n/a;
else c=n/a+1;
min = b * c;
for (i=1;i<=2;i++)
{
scanf("%d%d",&a,&b);
if (n%a==0) c=n/a;
else c=n/a+1;
if (min>c*b) min=c*b;
}
printf("%d\n",min);
return 0;
}