有一個函數如下:
編寫程序,輸入x的值,計算相應的y值輸出(保留3位小數)。
源程序:
#include<stdio.h>
#include<math.h>
int main()
{
float x,y;
scanf("%f", &x);
if(x<5) y=x*x-2;
else if(x<50) y=3*x +5;
else y=x-sqrt(4*x-1);
printf("y=%.3f\n",y);
return 0;
}
運行結果:
(1)
4
y=14.000
(2)
10.5
y=36.500
(3)
55.6
y=40.720
編寫一個程序,根據輸入的三角形的三條邊判斷是否能組成三角形,如果可以則輸出它的面積和三角形類型(等邊、等腰、直角、等腰直角、一般三角形等)。
源程序:
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, s, area;
printf("Input the three edge length:");
scanf("%f,%f,%f",&a,&b , &c);
if(a+b>c && b+c>a && a+c>b)
{
s=(a+b+c)/2;
area=sqrt(s*(s*(s-a)*(s-b)*(s-c)));
printf("area=%.2f\n",area);
if(fabs(a-b)<=0.0001&&fabs(b-c)<=0.0001&&fabs(c-a)<=0.0001)
{
printf("等邊三角形");
}
else if(fabs(a-b)<=0.0001||fabs(b-c)<=0.0001||fabs(c-a)<=0.0001)
{
if(fabs(a*a+b*b-c*c)<=0.0001||fabs(a*a+c*c-b*b)<=0.0001||fabs(b*b+c*c-a*a)<=0.0001)
{
printf("等腰直角三角形");
}
else
printf("等腰三角形");
}
else if(fabs(a*a+b*b-c*c)<=0.0001||fabs(a*a+c*c-b*b)<=0.0001||fabs(b*b+c*c-a*a)<=0.0001)
{
printf("直角三角形");
}
else printf("一般三角形\n");
}
else printf("不是三角形\n");
return 0;
}
運行結果:
(1)
Input the three edge length:3,4,5
area=14.70
直角三角形
(2)
Input the three edge length:4,4,5
area=19.90
等腰三角形
(3)
Input the three edge length:5,5,5
area=29.65
等邊三角形
(4)
Input the three edge length:5,5,7.071065
area=36.52
等腰直角三角形
(5)
Input the three edge length:5,6,7
area=44.09
一般三角形
(6)
Input the three edge length:4,5,10
不是三角形
--
設獎金稅率r有如下的要求 (n代表獎金) :
由鍵盤輸入獎金值,計算並輸出相應的稅率和實際應得獎金值。分別用if-else的嵌套語句和switch多分支選擇語句編寫程序。
源程序一:
#include<stdio.h>
int main()
{
float n, r, s;
int m;
printf("請輸入獎金值:");
scanf("%f", &n);
if(n<1000) r=0;
else if(n<3000) r=0.03;
else if(n<5000) r=0.05;
else if(n<10000) r=0.07;
else r=0.1;
s=n-r*n; //計算實際所得獎金值
printf("稅率r=%.0f%%, 獎金值n=%.2f, 實際所得獎金s=%.2f\n",r*100, n, s);
return 0;
}
源程序二:
#include<stdio.h>
int main( )
{
float n, r, s;
int m;
printf("請輸入獎金值:");
scanf("%f", &n);
if(n>=10000) m=10;
else m=n/1000;
switch(m)
{ case 0: r=0; break;
case 1:
case 2: r=0.03; break;
case 3:
case 4: r=0.05; break;
case 5: case 6:
case 7: case 8:
case 9:r=0.07; break;
case 10:r=0.1;
}
s=n-r*n; //計算實際所得獎金值
printf("稅率r=%.0f%%, 獎金值n=%.2f, 實際所得獎金s=%.2f\n",r*100, n, s);
return 0;
}
運行結果:
(1)
請輸入獎金值:3000
稅率r=5%, 獎金值n=3000.00, 實際所得獎金s=2850.00
(2)
請輸入獎金值:6000
稅率r=7%, 獎金值n=6000.00, 實際所得獎金s=5580.00
(3)
請輸入獎金值:11000
稅率r=10%, 獎金值n=11000.00, 實際所得獎金s=9900.00
--
從鍵盤輸入任意4個數a、b、c、d,按照從大到小的順序排列後重新輸出。源程序:
#include<stdio.h>
int main( )
{ int a, b, c, d,t;
scanf("%d,%d,%d,%d", &a,&b,&c,&d);
if(a<b) { t=a; a=b; b=t; }
if(a<c) { t=a; a=c; c=t; }
if(a<d) { t=a; a=d; d=t; }
if(b<c) { t=b; b=c; c=t; }
if(b<d) { t=b; b=d; d=t; }
if(c<d) { t=c; c=d; d=t; }
printf("%d %d %d %d\n", a,b,c,d);
return 0;
}
運行結果:
3,4,6,1
6 4 3 1
---
給出一個不超過4位數的正整數,判斷它是幾位數,並按逆向輸出各位數字。例1234,輸出為4321。源程序:
#include<stdio.h>
int main(void)
{
int num,i,j,k,m;
printf("輸入一個少於4位的正整數:");
scanf("%d",&num);
if(num>=0 && num<=9999)
{
if(num>1000 && num<=9999)
{
printf("是一個4位數\n");
m=num%10; /*求個位上的數字*/
k=num/10%10; /*求十位上的數字*/
j=num/100%10; /*求百位上的數字*/
i=num/1000; /*求千位上的數字*/
printf("逆序數為:%d%d%d%d\n",m,k,j,i);
}
else if(num>=100)
{ printf("是一個3位數\n");
m=num%10; /*求個位上的數字*/
k=num/10%10; /*求十位上的數字*/
j=num/100; /*求百位上的數字*/
printf("逆序數為:%d%d%d\n",m,k,j);
}
else if(num>=10)
{ printf("是一個2位數\n");
m=num%10; /*求個位上的數字*/
k=num/10; /*求十位上的數字*/
printf("逆序數為:%d%d\n",m,k);
}
else
{ printf("是一個1位數\n");
printf("逆序數為:%d\n",num);
}
}
else printf("是一個無效的數\n");
return 0;
}
運行結果:
(1)
輸入一個少於4位的正整數:63
是一個2位數
逆序數為:36
(2)
輸入一個少於4位的正整數:135
是一個3位數
逆序數為:531
(3)
輸入一個少於4位的正整數:1234
是一個4位數
逆序數為:4321
--
從鍵盤輸入一個正整數,判斷其是否能同時被5和7整除,若是則輸出「YES」,否則輸出「NO」。
源程序:
#include<stdio.h>
int main()
{
int x;
scanf("%d", &x);
if(x>0)
{ if(x%5==0&&x%7==0)
printf("YES\n");
else printf("NO\n");
}
else printf("data error!");
return 0;
}
運行結果:
(1)
14
NO
(2)
35
YES
(3)
-9
data error!
郵局郵寄包裹有如下規定:如果包裹的長、寬、高任意尺寸超過1米或重量超過40千克,不予郵寄;可以郵寄的包裹每件收取手續費5元,再根據不同的重量weight計算郵資。
weight<10kg 2.5元
10kg≤weight<30kg 2.0元
30kg≤weight<40kg 1.5元
編寫程序輸入包裹的長、寬、高和重量,計算並輸出郵資。
源程序:
#include<stdio.h>
int main()
{
float length, width, height, weight, money, r;
scanf("%f%f%f%f",&length, &width, &height, &weight);
printf("Please Input length, width, height, weight:\n");
if (length>1 || width>1 || height>1 || weight>40) r=-1;
else if (weight<10) r=2.5;
else if (weight<30) r=2.0;
else r=1.5;
if (r==-1) printf ("error\n");
else
{
money=weight*r+5;
printf ("%.2f\n", money);
}
return 0;
}
運行結果:
(1)
Please Input length, width, height, weight:
0.8 0.7 0.6 5
17.50
(2)
Please Input length, width, height, weight:
0.9 0.8 0.9 20
45.00
(3)
Please Input length, width, height, weight:
1.2 0.8 0.6 5
error
--
源程序一:
#include<stdio.h>
int main()
{
int a, b, c, d, min;
printf("Please input a, b, c, d:\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
min=a;
if(b<min) min=b;
if(c<min) min=c;
if(d<min) min=d;
printf("min=%d\n", min);
return 0;
}
源程序二:(採用條件運算符)
#include<stdio.h>
int main()
{
int a, b, c, d,min;
printf("Please input a, b, c, d:\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
min=a;
min=b<min? b:min;
min=c<min? c:min;
min=d<min? d:min;
printf("min=%d\n", min);
return 0;
}
運行結果:
Please input a, b, c, d:
6 4 5 2
min=2
----
某超市進行啤酒促銷活動,每瓶啤酒的單價為20元,買5瓶以上(包括5瓶)10瓶以下打9折;10瓶以上(包括10瓶)20瓶以下打85折;20瓶以上(包括20瓶)30瓶以下打8折;30瓶以上打75折。編寫程序,根據顧客的購買數量計算所要付款的數額(保留2位小數)。源程序:
#include<stdio.h>
int main()
{
float discount, price=20, total;
int x;
printf ("輸入購買的瓶數:");
scanf("%d", &x);
switch (x/5)
{ case 0: discount=1; break;
case 1: discount=0.9;break;
case 2:
case 3: discount=0.85;break;
case 4:
case 5: discount=0.8;break;
default: discount=0.75;
}
total=price*x*discount;
printf("所付金額為%.2f\n",total);
return 0;
}
運行結果:
輸入購買的瓶數:12
所付金額為204.00
---
從鍵盤輸入一個5位數,判斷它是不是迴文數。迴文數是指個位和萬位數相同,十位數和千位數相同的數,如12321是迴文數。源程序:
#include<stdio.h>
int main()
{
int ge,shi,qian,wan,x;
printf("Please input a number: ");
scanf("%d",&x);
wan=x/10000;
qian=x%10000/1000;
shi=x%100/10;
ge=x%10;
if (ge==wan && shi==qian)
printf("This number %d is huiwen\n",x);
else
printf("This number %d is not huiwen\n", x);
return 0;
}
運行結果:
(1)
Please input a number: 12321
This number 12321 is huiwen
(2)
Please input a number: 12325
This number 12325 is not huiwen
--