分段函数
计算分段函数,测试数据分别是-2、3、7。

#include <stdio.h>
#include <math.h>
int main( )
{ float x,f;
int i;
for (i=0; i<3; i++) {
scanf("%f",&x);
if(x<0)
f=fabs(x+1);
else if(
x>=0 && x<=5
) f=2*x+1;
else f=sin(x)+5;
printf("x=%.2f,y=%.2f\n",
x,f
);
}
return 0;
}
写出与switch语句等价的else-if语句
switch (ch){
case '0' : case '1' : case '2' : case '3' : case '4' :
case '-':
minus++; break;
case '5' : case '6' : case '7' : case '8' : case '9' :
digit ++;break;
default:
other ++; break;
}
if
(ch=='0'||ch=='1'||ch=='2'||ch=='3'||ch=='4'||ch=='-')
{
minus++;
}else if
(ch=='5'||ch=='6'||ch=='7'||ch=='8'||ch=='9' )
{
digit ++;
}else {
other ++;
}
找出3个数中最大的数
#include <stdio.h>
int main()
{
int a, b, c, max;
scanf("%d %d %d", &a, &b, &c);
max = 0;
if ( a > b ) {
if ( a > c ) {
max=a;
} else {
max=c;
}
} else {
if
(c<b)
{
max = b;
} else {
max=c;
}
}
printf("%d\n", max);
return 0;
}
输出每个月的天数
输入年,输出该年每个月的天数。其中1、3、5、7、8、10、12月有31天,4、6、9、11月有30天,2月平年有28天,闰年有29天。判断闰年的条件是:能被 4 整除但不能被 100 整除,或者能被 400 整除。
#include <stdio.h>
int main()
{
int day, month, year;
scanf("%d", &year);
for(month = 1; month <= 12; month++){
switch
(month)
{
case 2:
if
(year%4==0&&year%100!=0||year%400==0)
{
day = 29;
}else{
day = 28;
}
break;
default:
day = 31; break;
case 4:case 6:case 9:case 11:
day = 30; break;
}
printf("%d ", day) ;
}
return 0;
}
输出偶数
输入一个正整数n,再输入n个整数,输出其中的偶数。要求相邻数字中间用一个空格分开,行末不得有多余空格。
# include <stdio.h>
int main()
{
char ch;
int first, k, n, x;
scanf("%d", &n);
first=1;
for(k = 1; k <= n; k++){
scanf("%d", &x);
if
(x%2==0)
{
if ( first == 1 ){
printf("%d", x);
first = 0;
}else{
printf(" %d", x);
}
}
}
return 0;
}
显示扑克牌
在玩牌程序中,每一组牌用数字1〜13代表。输入数字,显示相应的牌。其中2〜10直接显示数字,而数字1、11、12、13则分别用Ace、Jack、Queen、King来表示。
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
if(n >= 1 && n <= 13){
switch
(n)
{
case 1:
printf("Ace\n");break;
case 11: printf("Jack\n"); break;
case 12:
printf("Queen\n");break;
case 13: printf("King\n"); break;
default:
printf("%d\n",n);break;
}
}else{
printf("Error\n");
}
return 0;
}
计算分段函数

输入格式:
输入在一行中给出实数x。
输出格式:
在一行中按“f(x) = result”的格式输出,其中x与result都保留两位小数。
注:可在头文件中包含math.h,并调用sqrt函数求平方根,调用pow函数求幂。
#include<stdio.h>
#include<math.h>
int main()
{
float x,y;
scanf("%f",&x);
if(x>=0)
y=pow(x,0.5);
else
y=(x+1)*(x+1)+2*x+1/x;
printf("f(%.2f) = %.2f",x,y);
return 0;
}
输出三角形面积和周长
本题要求编写程序,根据输入的三角形的三条边a、b、c,计算并输出面积和周长。注意:在一个三角形中, 任意两边之和大于第三边。三角形面积计算公式:area=
s(s−a)(s−b)(s−c)开根号 ,其中s=(a+b+c)/2。
#include<stdio.h>
#include<math.h>
int main()
{
float a,b,c,s,area,perimeter;
scanf("%f %f %f",&a,&b,&c);
s=(a+b+c)/2;
perimeter=a+b+c;
area=sqrt(s*(s-a)*(s-b)*(s-c));
if((a+b>c&&b+c>a)&&a+c>b)
printf("area = %.2f; perimeter = %.2f",area,perimeter);
else
printf("These sides do not correspond to a valid triangle");
return 0;
}
判断闰年
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
if ((x%4==0&&x%100!=0)||(x%400==0))
printf("yes");
else
printf("no");
return 0;
}
三天打鱼两天晒网
#include<stdio.h>
int main()
{
int a,b;
scanf("%d",&a);
b=a%5;
if(b==1||b==2||b==3)
printf("Fishing in day %d",a);
else
printf("Drying in day %d",a);
return 0;
}
分段计算居民水费
#include<stdio.h>
int main()
{
float x,y;
scanf("%f",&x);
if (x<0)
y=0;
else if (x>15)
y=2.5*x-10.5;
else
y=4*x/3;
printf("f(%.2f) = %.2f",x,y);
return 0;
}
1259

被折叠的 条评论
为什么被折叠?



