20250302-Rain

1. 字符串

/*
1. 大小写转换
设计一个程序, 输入一行字符串, 将其中大写转为小写, 小写转为大写. 其余字符不变
(字符串长度<100)
样例输入: hELLO wORLD!
样例输出: Hello World!
*/

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

int main(void)
{
	string S;
	cout << "Please input 'S': ";
	// 输入字符串,可以包含空格
	getline(cin, S);

	// // 遍历 S 的每一个字符,单独判断
	// // 手撸
	// for(auto &c : S)
	// {
	// 	if(c >= 'a' && c <= 'z')
	// 	{
	// 		c = c - 'a' + 'A';
	// 	}
	// 	else if(c >= 'A' && c <= 'Z')
	// 	{
	// 		c = c - 'A' + 'a';
	// 	}
	// }

	// 内置方法
	for(auto &c : S)
	{
		if(islower(c))
		{
			c = toupper(c);
		}
		else if(isupper(c))
		{
			c = tolower(c);
		}
	}

	cout << S << endl;

	return 0;
}

2. 嵌套循环

/*
3. 杨辉三角
输入n(1<n<10), 打印高度为n的杨辉三角
样例输入: 5
样例输出:
 1
 1 1
 1 2 1
 1 3 3 1
 1 4 6 4 1
*/

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

int main(void)
{
    int n;
    cout << "Please input 'n': ";
    cin >> n;

    // 根据输入的 n 来动态分配 二维数组的大小
    vector<vector<int>> m_matrix(n, vector<int>(n, 0));

    // 填数
    for(int i = 0; i < n; i++)
    {
        // 内循环条件:根据样例输出发现 第 i 行会输出 i 个数 得到 j <= i
        for(int j = 0; j <= i; j++)
        {
            // 如果是第一行 || 第一列 || 其上方没有数
            // 则设为 1
            if(i == 0 || j == 0 || m_matrix[i - 1][j] == 0)
            {
                m_matrix[i][j] = 1;
                continue;
            }
            m_matrix[i][j] = m_matrix[i - 1][j - 1] + m_matrix[i - 1][j];
        }
    }

    // 打印矩阵
    for(auto &row : m_matrix)
    {
        for(auto element : row)
        {
            if(element == 0)
            {
                continue;
            }
            cout << element << ' ';
        }
        cout << endl;
    }
    return 0;
}

3. 螺旋矩阵

/*
*4. 包围圈
东东哥在一场军事演练中, 需要对一片正方形森林进行排查, 
为了防止被包围, 东东哥需要先排查外围才能进一步深入
输入: 正整数n(1<n<10),  表示森林的边长
输出: 东东哥排查森林的顺序
样例输入:
 4
样例输出:
 1   2   3   4
 12  13  14  5
 11  16  15  6
 10  9   8   7
*/

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

int main(void)
{
	int n;
	cout << "Please input 'n': ";
	cin >> n;

	vector<vector<int>> m_matrix(n, vector<int>(n, 0));
	// 初始化计数器
	int cnt = 1;

	// 初始化matrix的四个边界left right top bottom
	int left = 0;
	int right = n;
	int top = 0;
	int bottom = n;

	while(left < right && top < bottom)
	{
		// 从左到右遍历最上面一行
		for(int i = left; i < right; i++)
		{
			m_matrix[top][i] = cnt;
			cnt++;
		}
		// 最上面一行遍历完修改matrix的top边界
		top++;
		if(top >= bottom)
		{
			break;
		}

		// 从上到下遍历最右边一列
		for(int j = top; j < bottom; j++)
		{
			m_matrix[j][right - 1] = cnt;
			cnt++;
		}
		right--;
		// 如果已经遍历完matrx就结束,防止matrix只有一行一列,走到下面逻辑重复遍历
		if(left >= right)
		{
			break;
		}

		// 从右到左遍历最下面一行
		for(int k = right - 1; k >= left; k--)
		{
			m_matrix[bottom - 1][k] = cnt;
			cnt++;
		}
		// 及时修改边界 和 进行边界判断
		bottom--;
		if(top >= bottom)
		{
			break;
		}

		// 从下到上遍历最左边一列
		for(int l = bottom - 1; l >= top; l--)
		{
			m_matrix[l][left] = cnt;
			cnt++;
		}
		left++;
		if(left >= right)
		{
			break;
		}
	}

	// 打印矩阵
	for(auto &row : m_matrix)
    {
        for(auto element : row)
        {
            cout << setiosflags(ios::left) << setw(3) << element;
        }
        cout << endl;
    }
}

Last updated