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

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

1. 数组元素循环右移问题


题目:一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?


输入格式:

每个输入包含一个测试用例,第1行输入N(1≤N≤100)和M(≥0);第2行输入N个整数,之间用空格分隔。


输出格式:

在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。


输入样例:


1. 6 2
2. 1 2 3 4 5 6

输出样例:

5 6 1 2 3 4



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

```c++

#include <stdio.h>
int main()
{
    int n, m, a[1000];
    scanf("%d %d", &n, &m);
    m = m % n;
    int count = m;
    ______________;
    for (int i = 0; i < count; i++)
        scanf("%d", &a[i]);
    int first = 1;
    for (int i = 0; i < n; i++)
    {
        if (!first)
            printf(" ");
        printf("%d", a[i]);
        first = 0;
    }
}
```


出处:

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

代码:

c++

输出:

6 2

1 2 3 4 5 6

5 6 1 2 3 4



2. 输出字符图形


输入一个正整数n(代表图形的行数),输出如样例形式的图形。

输入:7

输出:

    D      D
    CD    DC
    BCD  DCB
    ABCDDCBA
    BCD  DCB
    CD    DC
    D      D


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

```c++

#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<string> a(n, ""), b(n, "");
    int m = (n + 1) / 2;
    int p = 0;
    for (int i = m - 1; i >= 0; i--)
    {
        for (int j = 0; j <= i; j++)
            a[i].push_back('A' + j + p);
        b[i] = a[i];
        reverse(b[i].begin(), b[i].end());
        ___________________;
        p++;
    }
    p = 0;
    for (int i = n - 1; i >= m; i--)
    {
        a[i] = a[p];
        b[i] = b[p++];
    }
    for (int i = 0; i < n; i++)
        cout << a[i] << b[i] << endl;
    return 0;
}
```



出处:

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

代码:

#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<string> a(n, ""), b(n, "");
    int m = (n + 1) / 2;
    int p = 0;
    for (int i = m - 1; i >= 0; i--)
    {
        for (int j = 0; j <= i; j++)
            a[i].push_back('A' + j + p);
        b[i] = a[i];
        reverse(b[i].begin(), b[i].end());
    for (int j = i + 1; j < m; j++)
    {
        a[i] += " ";
        b[i] = " " + b[i];
    }
        p++;
    }
    p = 0;
    for (int i = n - 1; i >= m; i--)
    {
        a[i] = a[p];
        b[i] = b[p++];
    }
    for (int i = 0; i < n; i++)
        cout << a[i] << b[i] << endl;
    return 0;
}



输入输出:

7↙

D               D

CD          DC

BCD     DCB

ABCDDCBA

BCD     DCB

CD          DC

D               D


2867f70a98904c9ca7607a6b9e44858f.png



3. 移除链表元素


给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点

示例 1:

77a1c3b8a1c4066a5773b6eb8738e857.jpeg


输入:head = [1,2,6,3,4,5,6], val = 6

输出:[1,2,3,4,5]


示例 2:

输入:head = [], val = 1

输出:[]


示例 3:

输入:head = [7,7,7,7], val = 7

输出:[]


提示:

   列表中的节点数目在范围 [0, 10^4] 内

   1 <= Node.val <= 50

   0 <= val <= 50

出处:

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

代码:


#define null INT_MIN
#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution
{
public:
    ListNode *removeElements(ListNode *head, int val)
    {
        ListNode *dumynode = new ListNode(0);
        dumynode->next = head;
        ListNode *fast = dumynode->next;
        ListNode *slow = dumynode;
        while (fast != NULL)
        {
            if (fast->val == val)
            {
                slow->next = slow->next->next;
            }
            else
            {
                slow = slow->next;
            }
            fast = fast->next;
        }
        ListNode *ret = dumynode->next;
        delete dumynode;
        return ret;
    }
};
ListNode* buildNodeList(vector<int> vec) {
    ListNode *head = new ListNode(0);
    ListNode *p = head;
    for (size_t i = 0; i < vec.size(); i++) {
        ListNode *node = new ListNode(vec[i]);
        p->next = node;
        p = p->next;
    }
    return head->next;
}
string NodeList2String(ListNode *head) {
  if (head==NULL) return "[]";
    ListNode *p = head;
  string res = "[";
    while (p != nullptr) {
        res.append(to_string(p->val));
        res.append(",");
        p = p->next;
    }
    res.pop_back();
    res.append("]");
    return res;
}
int main()
{
  Solution s;
  vector<int> nums = {1,2,3,6,4,5,6};
  ListNode* root = buildNodeList(nums);
  cout << NodeList2String(s.removeElements(root, 6)) << endl;
  nums = {7,7,7,7,7};
  root = buildNodeList(nums);
  cout << NodeList2String(s.removeElements(root, 7)) << endl;
  return 0;
}


输出:

[1,2,3,4,5]

[]

目录
相关文章
|
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