C/C++每日一练(20230423)

简介: C/C++每日一练(20230423)

1. 多组输入求和


给定 2 个正整数 a, b ,a 和 b 最多可能有 40 位,求出 a + b 的和。


输入描述


两个正整数 a, b,a 和 b 最多可能有 40 位。一行表示一个数。

输出描述

a + b 的和。

样例输入

1. 111111111111111111111111111111111111111
2. 222222222222222222222222222222222222222

样例输出

333333333333333333333333333333333333333



出处:

https://edu.csdn.net/practice/26319576

代码:

#include <iostream>
#include <cstring>
using namespace std;
int main(){
    while (1)
    {
        char s1[200],s2[200];
        int a[200]={0},b[200]={0},l1,l2,c,k,i;
        gets(s1);
        l1=strlen(s1);
        if (l1 == 0) break;
        gets(s2);
        l2=strlen(s2);
        if(l1<l2) k=l2;
        else k=l1;c=k;
        for(i=0;i<l1;k--,i++)
            a[k]=s1[l1-1-i]-'0';
        for(k=c,i=0;i<l2;k--,i++)
            b[k]=s2[l2-1-i]-'0';
        for(i=c;i>=0;i--){
            a[i]+=b[i];
            if(a[i]>=10){
                a[i]=10;
                a[i-1]++;
            }
        }
        if(a[0]!=0){
            for(i=0;i<=c;i++)
                cout<<a[i];
        }else{
            for(i=1;i<=c;i++)
                cout<<a[i];
        }
        cout << endl;
    }
}


输出:


2. 螺旋矩阵 II


给你一个正整数 n ,生成一个包含 1n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix


示例 1:

b2086bc4a6e47ebbf922fed2e817b298.jpeg


输入:n = 3

输出:[[1,2,3],[8,9,4],[7,6,5]]


示例 2:

输入:n = 1

输出:[[1]]


提示:

  • 1 <= n <= 20

以下程序实现了这一功能,请你填补空白处内容:

···c++

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<vector<int>> generateMatrix(int n)
    {
        vector<vector<int>> ans(n, vector<int>(n, 0));
        int i, j = 0, time = 0, cnt = 1; //time记录第几圈
        ans[0][0] = 1;
        while (cnt < n * n)
        {
            ___________________;
            time++;
        }
        return ans;
    }
};
```


出处:

https://edu.csdn.net/practice/26319577

代码:

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<vector<int>> generateMatrix(int n)
    {
        vector<vector<int>> ans(n, vector<int>(n, 0));
        int i, j = 0, time = 0, cnt = 1; //time记录第几圈
        ans[0][0] = 1;
        while (cnt < n * n)
        {
      for (i = time, j++; j < n - time && cnt < n * n; j++)
          ans[i][j] = ++cnt;
      for (j--, i++; i < n - time && cnt < n * n; i++)
          ans[i][j] = ++cnt;
      for (i--, j--; j >= time && cnt < n * n; j--)
          ans[i][j] = ++cnt;
      for (j++, i--; i > time && cnt < n * n; i--)
          ans[i][j] = ++cnt;
            time++;
        }
        return ans;
    }
};
string vectorToString(vector<int> arr){
  string res = "[";
  int size = arr.size();
  for (int i = 0; i < size; i++) {
    res += to_string(arr[i]);
    if (i != size-1) {
      res += ",";
    }
  }
  return res + "]";
}
string vector2DToString(vector<vector<int>> vect) {
    string res = "[";
    size_t len = vect.size();
    for (size_t i = 0; i < len; i++)
  {
        res += vectorToString(vect[i]);
        if (i+1 != len)
          res += ",";
    }
    res += "]";
    return res;
}
int main()
{
  Solution s;
    cout << vector2DToString(s.generateMatrix(3)) << endl;
  return 0;
}

输出:

[[1,2,3],[8,9,4],[7,6,5]]


3. 路径交叉


给你一个整数数组 distance 


从 X-Y 平面上的点 (0,0) 开始,先向北移动 distance[0] 米,然后向西移动 distance[1] 米,向南移动 distance[2] 米,向东移动 distance[3] 米,持续移动。也就是说,每次移动后你的方位会发生逆时针变化。

判断你所经过的路径是否相交。如果相交,返回 true ;否则,返回 false 。

示例 1:

8ff63b9228149b90de12438778363e72.jpeg


输入:distance = [2,1,1,2]

输出:true


示例 2:



输入:distance = [1,2,3,4]

输出:false


示例 3:

bdefbc868dc738985c3e5a16ec3225f5.jpeg


输入:distance = [1,1,1,1]

输出:true

提示:

   1 <= distance.length <= 10^5

   1 <= distance[i] <= 10^5

出处:

https://edu.csdn.net/practice/26319578

代码:


#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    bool isSelfCrossing(vector<int> &distance)
    {
        int all_step = distance.size(), current_step = 0;
        if (all_step < 4)
            return false;
        current_step = 2;
        while (current_step < all_step && distance[current_step] > distance[current_step - 2])
            current_step++;
        if (current_step == all_step)
            return false;
        if (distance[current_step] >= distance[current_step - 2] - (current_step > 3 ? distance[current_step - 4] : 0))
            distance[current_step - 1] -= current_step > 2 ? distance[current_step - 3] : 0;
        current_step++;
        while (current_step < all_step && distance[current_step] < distance[current_step - 2])
            current_step++;
        return current_step != all_step;
    }
};
int main()
{
  Solution s;
  vector<int> distance = {2,1,1,2};
    cout << (s.isSelfCrossing(distance) ? "true" : "false") << endl;
  distance = {1,2,3,4};
    cout << (s.isSelfCrossing(distance) ? "true" : "false") << endl;
  distance = {1,1,1,1};
    cout << (s.isSelfCrossing(distance) ? "true" : "false") << endl;
  return 0;
}

输出:

true

false

true

目录
相关文章
|
2月前
|
Linux 监控 Ubuntu
Linux 终端操作命令(1)
Linux 终端操作命令(1)
71 1
Linux 终端操作命令(1)
|
2月前
|
算法 Java Go
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
30 1
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
|
2月前
|
Linux 监控 Shell
Linux 终端命令之文件浏览(4) head, tail
Linux 终端命令之文件浏览(4) head, tail
35 0
Linux 终端命令之文件浏览(4) head, tail
|
2月前
|
Shell Linux 机器学习/深度学习
Linux 终端操作命令(3)内部命令用法
Linux 终端操作命令(3)内部命令用法
53 0
Linux 终端操作命令(3)内部命令用法
|
2月前
|
Python Linux Ubuntu
Linux系统部署Python语言开发运行环境
Linux系统部署Python语言开发运行环境
127 0
Linux系统部署Python语言开发运行环境
|
2月前
|
Go Unix 开发者
Go语言time库,时间和日期相关的操作方法
Go语言time库,时间和日期相关的操作方法
66 0
Go语言time库,时间和日期相关的操作方法
|
2月前
|
C++ 存储 Serverless
力扣C++|一题多解之数学题专场(2)
力扣C++|一题多解之数学题专场(2)
33 0
力扣C++|一题多解之数学题专场(2)
|
2月前
|
Go 机器学习/深度学习 Rust
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
53 0
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
|
2月前
|
Java Go C++
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
41 0
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
|
2月前
|
Java Go C++
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort
32 0
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort