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
3. 移除链表元素
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回 新的头节点 。
示例 1:
输入: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]
[]