1. 螺旋矩阵
给你一个 m
行 n
列的矩阵 matrix
,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。
示例 1:
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
示例 2:
输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
输出:[1,2,3,4,8,12,11,10,9,5,6,7]
提示:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 10
-100 <= matrix[i][j] <= 100
以下程序实现了这一功能,请你填补空白处内容:
```c++ #include using namespace std; class Solution { public: vector spiralOrder(vector> &matrix) { vector ans; if (matrix.size() == 0) return ans; int cir = 0; int row = matrix.size(); int col = matrix[0].size(); int max_cir = int(min(matrix.size(), matrix[0].size()) + 1) / 2; for (; cir < max_cir; cir++) { for (int i = cir; i < col - cir; i++) { ans.push_back(matrix[cir][i]); } for (int i = cir + 1; i < row - cir; i++) { ans.push_back(matrix[i][col - 1 - cir]); } __________________; for (int i = row - 2 - cir; i > cir && (col - 1 - cir != cir); i--) { ans.push_back(matrix[i][cir]); } } return ans; } }; ```
出处:
https://edu.csdn.net/practice/26819119
代码:
#include <bits/stdc++.h> using namespace std; class Solution { public: vector<int> spiralOrder(vector<vector<int>> &matrix) { vector<int> ans; if (matrix.size() == 0) return ans; int cir = 0; int row = matrix.size(); int col = matrix[0].size(); int max_cir = int(min(matrix.size(), matrix[0].size()) + 1) / 2; for (; cir < max_cir; cir++) { for (int i = cir; i < col - cir; i++) { ans.push_back(matrix[cir][i]); } for (int i = cir + 1; i < row - cir; i++) { ans.push_back(matrix[i][col - 1 - cir]); } for (int i = col - 2 - cir; i >= cir && (row - 1 - cir != cir); i--) { ans.push_back(matrix[row - cir - 1][i]); } for (int i = row - 2 - cir; i > cir && (col - 1 - cir != cir); i--) { ans.push_back(matrix[i][cir]); } } return ans; } }; string vectorToString(vector<int> vect) { stringstream ss; ss << "["; for (int i = 0; i < (int)vect.size(); i++) { ss << vect[i]; if (i < (int)vect.size() - 1) ss << ","; } ss << "]"; return ss.str(); } int main() { Solution s; vector<vector<int>> matrix = {{1,2,3},{4,5,6},{7,8,9}}; cout << vectorToString(s.spiralOrder(matrix)) << endl; matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; cout << vectorToString(s.spiralOrder(matrix)) << endl; return 0; }
输出:
[1,2,3,6,9,8,7,4,5]
[1,2,3,4,8,12,11,10,9,5,6,7]
2. 戳气球
有 n
个气球,编号为0
到 n - 1
,每个气球上都标有一个数字,这些数字存在数组 nums
中。
现在要求你戳破所有的气球。戳破第 i
个气球,你可以获得 nums[i - 1] * nums[i] * nums[i + 1]
枚硬币。 这里的 i - 1
和 i + 1
代表和 i
相邻的两个气球的序号。如果 i - 1
或 i + 1
超出了数组的边界,那么就当它是一个数字为 1
的气球。
求所能获得硬币的最大数量。
示例 1:
输入:nums = [3,1,5,8]
输出:167
解释:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
示例 2:
输入:nums = [1,5]
输出:10
提示:
n == nums.length
1 <= n <= 500
0 <= nums[i] <= 100
出处:
https://edu.csdn.net/practice/26819120
代码:
#include <bits/stdc++.h> using namespace std; class Solution { public: vector<int> spiralOrder(vector<vector<int>> &matrix) { vector<int> ans; if (matrix.size() == 0) return ans; int cir = 0; int row = matrix.size(); int col = matrix[0].size(); int max_cir = int(min(matrix.size(), matrix[0].size()) + 1) / 2; for (; cir < max_cir; cir++) { for (int i = cir; i < col - cir; i++) { ans.push_back(matrix[cir][i]); } for (int i = cir + 1; i < row - cir; i++) { ans.push_back(matrix[i][col - 1 - cir]); } for (int i = col - 2 - cir; i >= cir && (row - 1 - cir != cir); i--) { ans.push_back(matrix[row - cir - 1][i]); } for (int i = row - 2 - cir; i > cir && (col - 1 - cir != cir); i--) { ans.push_back(matrix[i][cir]); } } return ans; } }; string vectorToString(vector<int> vect) { stringstream ss; ss << "["; for (int i = 0; i < (int)vect.size(); i++) { ss << vect[i]; if (i < (int)vect.size() - 1) ss << ","; } ss << "]"; return ss.str(); } int main() { Solution s; vector<vector<int>> matrix = {{1,2,3},{4,5,6},{7,8,9}}; cout << vectorToString(s.spiralOrder(matrix)) << endl; matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; cout << vectorToString(s.spiralOrder(matrix)) << endl; return 0; }
输出:
[1,2,3,6,9,8,7,4,5]
[1,2,3,4,8,12,11,10,9,5,6,7]
3. 实现五则运算
设计一个可以完成任意五则运算(加法/减法/乘法/除法/取余)的程序。除法按照计算机中整型相除来计算。 输入格式 多行输入,每输入一行数据对应输出一行。 每行输入格式为 a # b,其中 #∈{+,−,∗,/,%} a,b均为自然数 输出格式 每行输出对应的计算结果; 当运算为除法/取余的时候,如果除数为 0 ,输出 WA
。
输入样例
2+2 4*5 6/7 4%3 4%0
输出样例
4 20 0 1 WA
以下程序实现了这一功能,请你补全空白处内容:
```c++ #include int main() { int a; int b; char operation; int num; while (scanf("%d", &a) != EOF) { scanf("%c", &operation); scanf("%d", &b); if (operation == '+') { num = a + b; printf("%d\n", num); } else if (operation == '-') { num = a - b; printf("%d\n", num); } else if (operation == '*') { num = a * b; printf("%d\n", num); } _________________________; else { printf("%s\n", "WA"); } } return 0; } ```
出处:
https://edu.csdn.net/practice/26819121
代码:
#include <stdio.h> int main() { int a; int b; char operation; int num; while (scanf("%d", &a) != EOF) { scanf("%c", &operation); scanf("%d", &b); if (operation == '+') { num = a + b; printf("%d\n", num); } else if (operation == '-') { num = a - b; printf("%d\n", num); } else if (operation == '*') { num = a * b; printf("%d\n", num); } else if (operation == '/' && b != 0) { num = a / b; printf("%d\n", num); } else if (operation == '%' && b != 0) { num = a % b; printf("%d\n", num); } else { printf("%s\n", "WA"); } } return 0; }
输出:
略
🌟 每日一练刷题专栏 🌟
✨持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/