逻辑运算符

bool flag = true;
cout << !flag << endl;
cout << !!flag << endl;
bool flag1 = false;
bool flag2 = true;
bool flag3;
flag3 = flag && flag1;
cout << flag3 << endl;
flag3 = flag || flag2;
cout << flag3 << endl;
flag3 = flag && flag2;
cout << flag3 << endl;
flag3 = flag || flag1;
cout << flag3 << endl;

条件语句的运用

int a, b, c;
a = b = c = 0;
cout << "请分别输入三只小猪的体重:";
cin >> a >> b >> c;
if (a > b)
{
	if (a < c)
	{
		cout << "c 最重\n";
	}
	else if (a > c)
	{
		cout << "a 最重\n";
	}
	else
	{
		cout << "a 与 c 都最重\n";
	}
}
else if (a == b)
{
	if (a < c)
	{
		cout << "c 最重\n";
	}
	else if (a > c)
	{
		cout << "a 与 b 最重\n";
	}
	else
	{
		cout << "a 、b 与 c 一样重\n";
	}
}
else
{
	if (b < c)
	{
		cout << "c 最重\n";
	}
	else if (b > c)
	{
		cout << "b 最重\n";
	}
	else
	{
		cout << "a 与 c 都最重\n";
	}
}

三目运算符

	int a, b, c;
	a = 10, b = 15, c = 0;
	c = a > b ? a : b;
	cout << c << endl;
	//比较a和b后赋值
	(a > b ? a : b) = 100;
	cout << a << " " << b << endl;

switch语句

int score = 0;
	cin >> score;
	switch (score)
	{
	case 10:
		cout << "这部电影是经典电影";
		break;
	case 9:
		cout << "这部电影是经典电影";
		break;
	case 8:
		cout << "不错的电影";
		break;
	case 7:
		cout << "不错的电影";
		break;
	case 6:
		cout << "电影一般般";
		break;
	default:
		cout << "电影是烂片";
		break;
	}
	cout << endl;

猜数字代码while方式

// 添加随机数种子,利用当前系统时间生成随机数
	// 防止每次随机数都一样
	srand((unsigned int)time(NULL));
	int t = rand()%100 + 1;
	cout << t << endl;
	int num = -1;
	while (1)
	{
		cin >> num;
		if (num == t)
		{
			cout << "恭喜你猜对了!\n";
			break;
		}
		else if (num < t)
		{
			cout << "猜小了\n";
		}
		else
		{
			cout << "猜大了\n";
		}
	}

猜数字do while()方式

	// 添加随机数种子,利用当前系统时间生成随机数
	// 防止每次随机数都一样
	srand((unsigned int)time(NULL));
	int t = rand()%100 + 1;
	cout << t << endl;
	int num = -1;
	int flag = 1;
	do
	{
		
		cin >> num;
		if (t == num)
		{
			cout << "恭喜你猜对了!\n";
			flag = 0;
		}
		else if (num < t)
		{
			cout << "猜小了\n";
		}
		else
		{
			cout << "猜大了\n";
		}
	} while (flag); // 条件为假时退出循环

do while解决水仙花数-1

//水仙花数:3位数的数字个十百上的数字3次幂之和等于本身
	int num = 0;
	cin >> num;
	int tmp = 0, num2 = 0;
	num2 = num;
	int i = 3;
	do 
	{
		tmp += pow(num % 10,3);
		num /= 10;
	} while (--i);
	cout << endl;
	if (num2 == tmp)
	{
		cout << "是水仙花数";
	}
	else
	{
		cout << "不是水仙花数";
	}
	cout << endl;

do while解决水仙花数-2

	//水仙花数:3位数的数字个十百上的数字3次幂之和等于本身
	int num = 100;
	do 
	{
		int a, b, c;
		a = num % 10;
		b = num / 10 % 10;
		c = num / 100;
		int num2 = a * a * a + b * b * b + c * c * c;
		if (num2 == num)
		{
			cout << num << "\t";
		}
		num++;
	} while (num<1000);
	cout << endl;

100以内翘7

int i;
for (i = 1; i <= 100; i++)
{
	cout << i;
	if (i / 10 == 7 || i % 7 == 0 || i % 10 == 7) // 十位有7、7的倍数、个位有7
	{
		cout << ", 敲桌子\n";
	}
}

数组地址

int arr[] = { 1,45,63,64,52 };
char ch[] = "fhlgalf";
cout << sizeof(ch) << endl;
cout << sizeof(arr) << endl;
cout << sizeof(arr) / 4 << endl;
cout << (int)arr << " " << (int)&arr[1] << endl;

逆置数组元素

	int arr[] = { 5,2,3,9,10,7 };
	int endArr = sizeof(arr) / sizeof(arr[0]) - 1;
	int start = 0;
	while (start<endArr)
	{
		int temp = arr[start];
		arr[start++] = arr[endArr];
		arr[endArr--] = temp;
	}
	for (int i = 0; i < start + endArr + 1; i++)
	{
		cout << i << "  " << arr[i] << endl;
	}
	/*for (; start < endArr; start++, endArr--)
	{
		int temp = arr[start];
		arr[start] = arr[endArr];
		arr[endArr] = temp;
		cout << start << "  " << endArr << endl;
	}
	cout << endl;
	for (int i = 0; i < start + endArr + 1; i++)
	{
		cout << i << "  " << arr[i] << endl;
	}
	cout << endl;*/

冒泡排序

	int arr[6] = { 5,2,3,9,10,7 };
	int endArr = sizeof(arr) / sizeof(arr[0]) - 1;
	int start = 0;
	/*for (int i = 0; i < 6; i++)
	{
		for (int j = i; j < 5; j++) // j<5,j+1不超过数组长度
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}*/
	for (int i = 0; i < 6 - 1; i++)  //6-1表示需要排序的次数
	{
		for (int j = 0; j < 6 - 1 - i; j++) //6-1-i表示需要每轮比较后需要比较的个数
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
	for (int i = 0; i < 6; i++)
	{
		cout << arr[i] << '\t';
	}
	cout << endl;