#include<bits/stdc++.h>
using namespace std;
const int N=25;
int e[N],ne[N],head=-1,idx=1;
int n,m;
void add_to_head(int x){
e[idx]=x;
ne[idx]=head;
head=idx++;
}
void add(int k,int x){
e[idx]=x;
ne[idx]=ne[k];
ne[k]=idx++;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
if(i==1){
add_to_head(i);
}
else{
add(i-1,i);
}
}
ne[n]=head;
int cur=head;
while(n--){
for(int i=1;i<m-1;i++){
cur=ne[cur];
}
cout<<e[ne[cur]]<<" ";
ne[cur]=ne[ne[cur]];
cur=ne[cur];
}
return 0;
}
#include<iostream>
#include<queue>
using namespace std;
int n,m;
int main(){
cin>>n>>m;
queue<int>q;
for(int i=1;i<=n;i++){
q.push(i);
}
int i=1;
while(!q.empty()){
if(i==m){
cout<<q.front()<<" ";
q.pop();
i=1;
}
else{
q.push(q.front());
q.pop();
i=i+1;
}
}
return 0;
}
2、单链表的创建,销毁与遍历
复制代码
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
Node* head = NULL;
int num;
// 逆序创建链表
while (cin >> num && num != -1) {
Node* newNode = new Node;
newNode->data = num;
newNode->next = head;
head = newNode;
}
// 遍历链表输出
Node* cur = head;
while (cur != NULL) {
cout << cur->data;
if (cur->next != NULL) cout << " ";
cur = cur->next;
}
cout << endl;
// 销毁链表
while (head != NULL) {
Node* temp = head;
head = head->next;
delete temp;
}
return 0;
}
3、最大子段和
复制代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[105];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int dq_max = a[1];
int qj_max = a[1];
for (int i = 2; i <= n; i++) {
dq_max = max(a[i], dq_max + a[i]);
qj_max = max(qj_max, dq_max);
}
cout << qj_max << endl;
return 0;
}
4、求二叉树的高度
cpp复制代码
#include<bits/stdc++.h>
using namespace std;
const int N=105;
struct node{
int data;
int left;
int right;
}a[N];
int n;
int dfs(int root){
if(root==0)return 0;
int h1=dfs(a[root].left);
int h2=dfs(a[root].right);
return max(h1,h2)+1;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].data>>a[i].left>>a[i].right;
}
cout<<dfs(1);
return 0;
}
5、二叉树的遍历
cpp复制代码
#include<bits/stdc++.h>
using namespace std;
const int N=105;
struct node{
int left;
int right;
}a[N];
int n;
void inorder(int root){
if(root==0)return;
cout<<root<<" ";
inorder(a[root].left);
inorder(a[root].right);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].left>>a[i].right;
}
inorder(1);
return 0;
}
6、完全二叉树的权值
复制代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N];
int n,x;
int main(){
int h=0,nn=0;
cin>>n;
for(int i=1;i<=n;i++){
if(i>nn){
nn+=pow(2,h);
h++;
}
cin>>x;
a[h]+=x;
}
int max_a=0,maxh=0;
for(int i=1;i<=h;i++){
if(a[i]>max_a){
max_a=a[i];
maxh=i;
}
}
cout<<maxh<<endl;
return 0;
}
7、二叉树求值
复制代码
#include<bits/stdc++.h>
using namespace std;
const int N = 105; // 题目限制n≤100
struct Node {
int value; // 节点权值
int left, right; // 左右子节点编号
int count; // 子树节点个数
int sum; // 子树权值和
};
Node nodes[N];
// 递归计算以u为根的子树的节点个数和权值和
void dfs(int u) {
if (u == 0) return; // 空节点
// 递归处理左右子树
dfs(nodes[u].left);
dfs(nodes[u].right);
// 计算当前子树的节点个数和权值和
nodes[u].count = 1; // 自身
nodes[u].sum = nodes[u].value;
if (nodes[u].left != 0) {
nodes[u].count += nodes[nodes[u].left].count;
nodes[u].sum += nodes[nodes[u].left].sum;
}
if (nodes[u].right != 0) {
nodes[u].count += nodes[nodes[u].right].count;
nodes[u].sum += nodes[nodes[u].right].sum;
}
}
int main() {
int n;
cin >> n;
// 读取每个节点的信息
for (int i = 1; i <= n; i++) {
cin >> nodes[i].value >> nodes[i].left >> nodes[i].right;
nodes[i].count = 0;
nodes[i].sum = 0;
}
// 从根节点开始递归计算(假设根节点是1)
dfs(1);
// 输出每个节点的子树信息
for (int i = 1; i <= n; i++) {
cout << nodes[i].count << " " << nodes[i].sum << endl;
}
return 0;
}
8、最大连续子序列
复制代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int a[N];
int b[N];
int n;
int main(){
cin>>n;
for(int i=1;i<+n;i++){
cin>>a[i];
}
int t=0;
for(int i=1;i<=n;i++){
if(a[i]>=0){
t=1;
break;
}
}
if(t==0){
cout<<a[1]<<" "<<a[n];
}
b[1]=a[1];
int max=b[1],begin=0,end=0;
for(int i=1;i<n;i++){
if(b[i-1]<0){
b[i]=a[i];
}
else{
b[i]=b[i-1]+a[i];
}
if(b[i]>max){
max=b[i];
end=i;
}
}
for(begin=end;begin>=0;begin--){
if(b[begin<0]){
break;
}
}
begin++;
cout<<max<<endl;
return 0;
}