① Use of ternary operator ② Use if to output the maximum value ③ Switch to determine the number of days per month ④ Number of hundreds of daffodils ⑤ Random guessing numbers [Dark horse programmer video]

[Ternary Operator]

Ternary operator: (expression①)? (Expression ②): (Expression ③)

① correct, output ②; ① wrong, output ③.

Ternary operator: What is returned is "variable" or can "continue to assign"

#include <iostream>
using namespace std;

int main()
{
    
    
	//三目运算符:(表达式①)?(表达式②):(表达式③)
	//①正确,输出② ;①错误,输出③ 。
	//三目运算符:返回的是 “变量” 或者 可以 “继续赋值”

	int a = 10, b = 30;
	int c;
	c = (a > b ? a : b); //输出c = 30; 
	cout << (a > b ? a : b) + 100 << endl;
	return 0;
}

[Use if to output the maximum value]
Sequence structure: one-by-one output
Selected structure: Selective output
Cyclic structure: Output some sequences multiple times

if-if the first if is satisfied, the program will jump out

#include <iostream>
using namespace std;

int main()
{
    
    
	//顺序结构:一条条的输出
	//选择结构:有选择的输出
	//循环结构:多次的输出一些序列

	//if -- 满足第一个if就会跳出程序

    //输入三个数字,输出最大的
	int a, b, c;
	cout << "输入三个数字: " << endl;
	cin >> a >> b >> c;
	if (a > b)
	{
    
    
		if (a > c)
			cout << "max = " << a << endl;
		else
			cout << "max = " << c << endl;
	}
	else
	{
    
    
		if (b > c)
			cout << "max = " << b << endl;
		else
			cout << "max = " << c << endl;
	}
	return 0;
}

[Switch judges the number of days per month]

In the parentheses after switch, write "conditions based on"

There is a break after each case

There must be a default at the end ;

The execution rate of switch is higher than that of if

#include <iostream>
using namespace std;

int main()
{
    
    
	//switch后面的括号里面写需要“依据的条件”
	//每一个case后面有个break
	//最后必须有一个default;
	//switch比if的执行率偏高一些

	//题目 :输入月份,输出月份有多少天
	int month;
	int a;
	cout << "输入月份:" << endl;
	cin >> month;
	if (month == 2)
		a = 2; //day = 29;
	else
	{
    
    
		if (month % 2 == 1)
			a = 1;//day = 31;
		else
			a = 0;//day = 30;
	}
	switch (a)
	{
    
    
    	case 0 :
			cout << "day = 30" << endl;
			break;
		case 1 :
			cout << "day = 31" << endl;
			break;
		case 2 :
			cout << "day = 29" << endl;
			break;
		default :
			break;
	}


	return 0;
}

[Number of hundreds of daffodils]

#include <iostream>
using namespace std;

int main()
{
    
    
	int a, b, c;
	for (int m = 100; m <= 999; m++)
	{
    
    
	a = m / 100;
	b = m % 100 / 10; 
	c = m % 10;
	if (a * a * a + b * b * b + c * c * c == m)
		cout << m << endl;
	}
	return 0;
}

[Guess the number randomly]
rand( )%100 is to select a number from 0-99

Add random number seed to use system time to generate random number to prevent the random number from being the same every time

The random number seed must be included in the header file ctime and used with the following rules:

srand((unsigned int)time(NULL));  

a = rand() % 100; 
在表达式后面加b,就是b -- 99+b
//题目 :随机输入数字,系统判断是否大或者小,如果超过五次,就错误了

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    
    
	//rand( )%100 是在 0 — 99 中  选择一个数字
	int a, b, sum = 1;
	//添加随机数种子利用系统时间生成随机数,防止每一次的随机数相同
	//随机数种子必须由头文件ctime,并且用如下规则使用
    srand((unsigned int)time(NULL));
	a = rand() % 100;//在表达式后面加b,就是b -- 99+b
	cin >> b;
	while (b != a)
	{
    
    
		sum++;
		if (b > a)
			cout << "大了" << endl;
		else if (b < a)
			cout << "小了" << endl;
		cin >> b;//输入的数字
		if (sum == 5)//输入的次数
		{
    
    
			cout << "不好意思,次数为" << sum <<"次。" << endl << "已经超过规定数字" << endl;
			break;
		}
	}
	if (a == b)
        cout << "正确!" << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_42198265/article/details/113408616