第07节 分支结构程序体验

一.课堂示例
1.给两个数,求两数中的大者(双分支结构)

#include <stdio.h>
int main()
{
    
    
	int a, b, c;
	scanf_s("%d %d", &a, &b);
	if (a > b)
	{
    
    
		c = a;
	}
	else
	{
    
    
		c = b;
	}
	printf("max=%d", c);
	return 0;
}
输出结果:
10 20
max = 20

2.给两个数,求两数中的大者(单分支结构)

#include <stdio.h>
int main()
{
    
    
	int a, b, t;
	scanf_s("%d %d", &a, &b);
	if (a < b)
	{
    
    
		t = a;
		a = b;
		b = t;
	}
	printf("max=%d", a);
	return 0;
}
输出结果:
10 20
max = 20

二.实践项目
1.给定两个正整数,求出两数的正差值

#include <stdio.h>
int main()
{
    
    
	int a, b, c;
	scanf_s("%d %d", &a, &b);
	c = a - b;
	if (c < 0)
	{
    
    
		c = -c;
	}
	printf("正差值=%d", c);
}
输出结果:
10 20
正差值=10

2.输入三个数,输出其中的最大值

#include <stdio.h>
int main()
{
    
    
	int a, b, c,max;
	scanf_s("%d %d %d", &a, &b, &c);
	if (a>b)
	{
    
    
		max = a;
	}
	else
	{
    
    
		max = b;
	}

	if (max < c)
	{
    
    
		max = c;
	}
	printf("三数最大值%d\n", max);
}
输出结果:
10 34 45
三数最大值45

3.输入员工一周工作时间,输出周工资

#include <stdio.h>
int main()
{
    
    
	int 工作时间, 本周工资;
	printf("工作时间: ");
	scanf_s("%d", &工作时间);
	if (工作时间 < 40)
	{
    
    
		本周工资 = 20 * 工作时间;
	}
	else
	{
    
    
		周工资 = 20 * 40 + 30 * (工作时间 - 40);
	}
	printf("本周工资: %d元\n", 本周工资);
}
输出结果:
工作时间: 50
本周工资: 1100

猜你喜欢

转载自blog.csdn.net/m0_51439429/article/details/114295798