C/C++每日一练(20230321)
创始人
2025-05-31 18:38:38
0

目录

1. 用递归求第n项的值  🌟

2. 最小路径和  🌟

3. 二进制求和  🌟🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 用递归求第n项的值

1,2,4,2,3,6,12,6,3,....求第n项值

代码:

#include 
int fun(int n, int *x, int *y)
{int sum = 0, i;int size = 0;int dd = 1;for (i = 1; i <= (*x); i++){sum += (2 * i - 1);}if (sum == n){*y = 2 * (*x) - 1;return (*x);}else if (sum > n){(*y) = n - (sum - (2 * (*x) - 1));size = 2 * (*x) - 1;dd = (*x);for (i = 2; i <= (*y); i++){if (i <= (*x))dd *= 2;elsedd /= 2;}return dd;}else{(*x)++;return fun(n, x, y);}
}int main()
{int n;int row = 1, col = 0;int val;row = 1;col = 0;printf("请输入n:");scanf("%d", &n);val = fun(n, &row, &col);printf("第%d项是:%d\n", n, val);return 0;
}

输入输出:

请输入n:7
第7项是:12


2. 最小路径和

给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。

说明:每次只能向下或者向右移动一步。

示例 1:

输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
输出:7
解释:因为路径 1→3→1→1→1 的总和最小。

示例 2:

输入:grid = [[1,2,3],[4,5,6]]
输出:12

提示:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 200
  • 0 <= grid[i][j] <= 100

代码:

#include
#include
using namespace std;class Solution
{
public:int minPathSum(vector> grid){int i, j;int gridRowSize = grid.size();int gridColSize = grid[0].size();vector> dp(gridRowSize, vector(gridColSize));dp[0][0] = grid[0][0];int sum = dp[0][0];for (i = 1; i < gridRowSize; i++){sum += grid[i][0];dp[i][0] = sum;}sum = dp[0][0];for (i = 1; i < gridColSize; i++){sum += grid[0][i];dp[0][i] = sum;}for (i = 1; i < gridRowSize; i++){for (j = 1; j < gridColSize; j++){dp[i][j] = grid[i][j] + min(dp[i - 1][j], dp[i][j - 1]);}}return dp[gridRowSize - 1][gridColSize - 1];}
};int main()
{Solution s;vector> grid = {{1,3,1},{1,5,1},{4,2,1}};cout << s.minPathSum(grid) << endl;grid = {{1,2,3},{4,5,6}};cout << s.minPathSum(grid) << endl;return 0;
}

输出:

7
12


3. 二进制求和

给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0

示例 1:

输入: a = "11", b = "1"
输出: "100"

示例 2:

输入: a = "1010", b = "1011"
输出: "10101"

提示:

  • 每个字符串仅由字符 '0' 或 '1' 组成。
  • 1 <= a.length, b.length <= 10^4
  • 字符串如果不是 "0" ,就都不含前导零。

代码: 

#include 
using namespace std;class Solution
{
public:string addBinary(string a, string b){string res;int carry = 0;int i = a.length() - 1;int j = b.length() - 1;for (; i >= 0 && j >= 0; i--, j--){if (a[i] == '1' && b[j] == '1'){if (carry > 0){res.push_back('1');}else{res.push_back('0');}carry = 1;}else if (a[i] == '0' && b[j] == '0'){if (carry > 0){res.push_back('1');}else{res.push_back('0');}carry = 0;}else{if (carry > 0){res.push_back('0');carry = 1;}else{res.push_back('1');carry = 0;}}}while (i >= 0){if (a[i--] == '1'){if (carry > 0){res.push_back('0');carry = 1;}else{res.push_back('1');carry = 0;}}else{res.push_back(carry + '0');carry = 0;}}while (j >= 0){if (b[j--] == '1'){if (carry > 0){res.push_back('0');carry = 1;}else{res.push_back('1');carry = 0;}}else{res.push_back(carry + '0');carry = 0;}}if (carry > 0){res.push_back('1');}reverse(res.begin(), res.end());return res;}
};int main()
{Solution s;cout << s.addBinary("11", "1") << endl;cout << s.addBinary("1010", "1011") << endl;return 0;
}

输出:

100
10101


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...