One-dimensional quadratic equation solution
There are several methods for solving quadratic equations in one unknown, which are most commonly used in problem solving; including:
1. Direct leveling method;
2. Matching method;
3. Formula method;
4. Factorization method.
If you let the computer solve it, I think the formula method should be intuitive and effective.
Using the formula requires △ = b^2-4* a *c to distinguish, which involves the problem of square roots and powers, as well as the problem of imaginary numbers.
I learned from the Internet that the library functions sqrt and pow are related to square roots and powers, respectively. The problem of imaginary numbers has not been solved. I manually change the opposite number +i, and the technology is helpless (=.=).
As follows: On a
whim, passing 0.5 to the variable y of the pow function is not equivalent to prescribing a square!
The experiment proved that it is indeed feasible!
The topic code is as follows
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,x1,x2,c,e;
while(scanf("%f%f%f",&a,&b,&c)!=EOF)
{
if(a==0)
{
printf("Not quadratic equation\n");
}
else
{
e=pow(b,2)-4*a*c;
if(e==0)
{
printf("x1=x2=%.2f\n",-b/(2*a));
}
else if(e>0)
{
x2=(-b+sqrt(e))/(2*a);
x1=(-b-sqrt(e))/(2*a);
printf("x1=%.2f;x2=%.2f\n",x1,x2);
}
else if(e<0)
{
float i;
x2=sqrt(-e)/(2*a);
x1=sqrt(-e)/(2*a);
i=-b/(2*a);
printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",i,x1,i,x2);
}
}
}
return 0;
}
Several questions to investigate the cycle
#include <stdio.h>
int main()
{
int a,b,c;
while(scanf("%d",&a)!=EOF)
{
b=a;
c=a;
while(a>0)
{
b=c;
while(b>0)
{
printf("* ");
b-=1;
}
printf("\n");
a-=1;
}
}
return 0;
}
#include <stdio.h>
int main()
{
int a,i,j;
while(scanf("%d",&a)!=EOF)
{
for(i=1;i<=a;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
#include <stdio.h>
int main()
{
int a,i,j;
while(scanf("%d",&a)!=EOF)
{
for(i=1;i<=a;i++)
{
for(j=0;j<=a-i;j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
#include <stdio.h>
int main()
{
int a,i,j,k;
while(scanf("%d",&a)!=EOF)
{
for(i=1;i<=a;i++)
{
for(k=0;k<a-i;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
#include <stdio.h>
int main()
{
int a,i,j,k;
while(scanf("%d",&a)!=EOF)
{
for(i=1;i<=a;i++)
{
for(k=0;k<a-i;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
#include <stdio.h>
int main()
{
int a,i,j,k;
while(scanf("%d",&a)!=EOF)
{
for(i=1;i<=a;i++)
{
for(k=1;k<i;k++)
{
printf(" ");
}
for(j=0;j<=a-i;j++)
{
printf("* ");
}
printf("\n");
}
}
return 0;
}
Except for the while loop used in the first one, the for loops are used afterwards, so the for loop is better to control!