4.1 选择结构和条件判断
#include<stdio.h>
#include<math.h>
int main(){
double a,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
double temp=b*b-4*a*c;
if(temp>=0){
temp=sqrt(temp);
double x1=(-b-temp)/(2*a);
double x2=(-b+temp)/(2*a);
printf("x1=%.2lf x2=%.2lf\n",x1,x2);
}else{
printf("No\n");
}
return 0;
}
课后习题
#include<stdio.h>
int main(){
int a,b,c,max;
scanf("%d%d%d",&a,&b,&c);
max=a;
if(b>max)
max=b;
if(c>max)
max=c;
printf("%d\n",max);
return 0;
}
#include<stdio.h>
#include<math.h>
int main(){
int n=1001;
while(n>1000||n<0){
printf("请输入小于1000的正数\n");
scanf("%d",&n);
}
int k=sqrt(n);
printf("%d\n",k);
return 0;
}
#include<stdio.h>
int main(){
int x;
scanf("%d",&x);
int y;
if(x<1){
y=x;
}else if(x>=1&&x<10){
y=2*x-1;
}else{
y=3*x-11;
}
printf("y=%d\n",y);
return 0;
}
#include<stdio.h>
int main(){
int score;
scanf("%d",&score);
if(score>=90)
printf("A");
else if(score>=80)
printf("B");
else if(score>=70)
printf("C");
else if(score>=60)
printf("D");
else
printf("E");
return 0;
}
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int num=0;//几位数
int arr[1000];
while(n>0){
arr[num]=n%10;
n/=10;
num++;
}
printf("n一共有%d位数\n",num);
int i=0;
for(i=num-1;i>=0;i--){
printf("%d ",arr[i]);
}
printf("\n");
for(i=0;i<num;i++){
printf("%d",arr[i]);
}
printf("\n");
return 0;
}
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
double sum=0;
if(n<=100000){
sum=0.1*n;
}else if(n<=200000){
sum+=(0.1*100000);
sum+=0.075*(n-100000);
}else if(n<=400000){
sum+=(0.1*100000);
sum+=(0.075*100000);
sum+=0.05*(n-200000);
}else if(n<=600000){
sum+=(0.1*100000);
sum+=(0.075*100000);
sum+=(0.05*200000);
sum+=0.03*(n-400000);
}else if(n<=1000000){
sum+=(0.1*100000);
sum+=(0.075*100000);
sum+=(0.05*200000);
sum+=(0.03*200000);
sum+=0.015*(n-600000);
}else{
sum+=(0.1*100000);
sum+=(0.075*100000);
sum+=(0.05*200000);
sum+=(0.03*200000);
sum+=0.015*600000;
sum+=0.01*(n-1000000);
}
printf("%.2lf\n",sum);
return 0;
}
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
double qian1=0.1*100000;
double qian2=qian1+0.075*100000;
double qian3=qian2+0.05*200000;
double qian4=qian3+0.03*200000;
double qian5=qian4+0.015*600000;
double sum=0;
int but=n/100000;
if(but>10){
but=10;
}
switch (but){
case 0:
sum=0.1*n;
break;
case 1:
sum+=qian1;
sum+=0.075*(n-100000);
break;
case 2:
case 3:
sum+=qian2;
sum+=0.05*(n-200000);
break;
case 4:
case 5:
sum+=qian3;
sum+=0.03*(n-400000);
break;
case 6:
case 7:
case 8:
case 9:
sum+=qian4;
sum+=0.015*(n-600000);
break;
case 10:
sum+=qian5;
sum+=0.01*(n-1000000);
break;
}
printf("%.2lf\n",sum);
return 0;
}
#include<stdio.h>
int main(){
int h=10;
double x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4;
printf("请输入一个坐标(x,y):");
scanf("%lf%lf",&x,&y);
d1=(x-x1)*(x-x1)+(y-y1)*(y-y1);
d2=(x-x2)*(x-x2)+(y-y2)*(y-y2);
d3=(x-x3)*(x-x3)+(y-y3)*(y-y3);
d4=(x-x4)*(x-x4)+(y-y4)*(y-y4);
if(d1>1&&d2>1&&d3>1&&d4>1){
h=0;
}
printf("该点的高度为%d\n",h);
return 0;
}