C/C++每日一练(20230423) 多组输入求和、螺旋矩阵II、路径交叉

简介: C/C++每日一练(20230423) 多组输入求和、螺旋矩阵II、路径交叉

1. 多组输入求和

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

输入描述

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

输出描述

a + b 的和。

样例输入

111111111111111111111111111111111111111
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:

输入:n = 3

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


示例 2:

输入:n = 1

输出:[[1]]


提示:

  • 1 <= n <= 20

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

···c++
#include 
using namespace std;
class Solution
{
public:
    vector> generateMatrix(int n)
    {
        vector> ans(n, vector(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:

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

输出:true


示例 2:

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

输出:false


示例 3:

输入: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


🌟 每日一练刷题专栏 🌟

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

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

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

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

主页:https://hannyang.blog.csdn.net/


目录
相关文章
|
3天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名
【4月更文挑战第22天】Pandas Python库提供数据排序和排名功能。使用`sort_values()`按列进行升序或降序排序,如`df.sort_values(by=&#39;A&#39;, ascending=False)`。`rank()`函数用于计算排名,如`df[&#39;A&#39;].rank(ascending=False)`。多列操作可传入列名列表,如`df.sort_values(by=[&#39;A&#39;, &#39;B&#39;], ascending=[True, False])`和分别对&#39;A&#39;、&#39;B&#39;列排名。
24 2
|
3天前
|
数据处理 Python
如何使用Python的Pandas库进行数据排序和排名?
Pandas在Python中提供数据排序和排名功能。使用`sort_values()`进行排序,如`df.sort_values(by=&#39;A&#39;, ascending=False)`进行降序排序;用`rank()`进行排名,如`df[&#39;A&#39;].rank(ascending=False)`进行降序排名。多列操作可传入列名列表,如`df.sort_values(by=[&#39;A&#39;, &#39;B&#39;], ascending=[True, False])`。
33 6
|
3天前
|
存储 编译器 C++
在C++语言中计算并打印出两个数的求和
在C++语言中计算并打印出两个数的求和
25 0
|
3天前
|
存储 Python
一文掌握python数组字典dict()的全部用法(零基础学python(三))
一文掌握python数组字典dict()的全部用法(零基础学python(三))
66 0
|
3天前
|
算法 索引 Python
Python3实现旋转数组的3种算法
Python3实现旋转数组的3种算法
25 0
|
3天前
|
数据可视化 数据处理 索引
Python如何对数据进行排序和排名操作?
Python如何对数据进行排序和排名操作?
44 0
|
3天前
|
Python
【Python进阶(六)】——随机数与数组
【Python进阶(六)】——随机数与数组
|
3天前
|
算法 Python
Python中不使用sort对列表排序的技术
Python中不使用sort对列表排序的技术
19 1
|
3天前
|
存储 程序员 Python
Python中自定义类实例化数组的艺术
Python中自定义类实例化数组的艺术
10 1
|
3天前
|
Python
使用Python pandas的sort_values()方法可按一个或多个列对DataFrame排序
【5月更文挑战第2天】使用Python pandas的sort_values()方法可按一个或多个列对DataFrame排序。示例代码展示了如何按'Name'和'Age'列排序 DataFrame。先按'Name'排序,再按'Age'排序。sort_values()的by参数接受列名列表,ascending参数控制排序顺序(默认升序),inplace参数决定是否直接修改原DataFrame。
27 1