C++数据结构 第一次作业

C++数据结构 第一次作业问题 A 一排里的位置交换 体育课上 老师把一排里的两个身高不同的同学的位置交换了一下以方便安排分组训练 你能编程模拟这个过程吗 输入 第一行是自然数 n n 小于 100 表示有 n 个数 第二行是 n 个表示身高的数据 第三行是要交换的两个同学的序号 按左起从 1 开始依次排序 输出

大家好,我是讯享网,很高兴认识大家。

问题 A: 一排里的位置交换

体育课上,老师把一排里的两个身高不同的同学的位置交换了一下以方便安排分组训练。你能编程模拟这个过程吗?

输入

第一行是自然数n(n小于100),表示有n个数,第二行是n个表示身高的数据,第三行是要交换的两个同学的序号(按左起从1开始依次排序)。

输出

交换位置后的一排身高值。中间用空格间隔。

样例输入 Copy

5 152 155 120 145 160 2 5 

讯享网

样例输出 Copy

讯享网152 160 120 145 155
#include<iostream> using namespace std; int main() { int n=0; cin>>n; int height[n]; for(int i=0;i<n;i++) { cin>>height[i]; } int a,b,tem; cin>>a>>b; tem=height[a-1]; height[a-1]=height[b-1]; height[b-1]=tem; for(int i=0;i<n;i++) { cout<<height[i]<<" "; } return 0; } 

问题 B: 围成圈

假如有一次班里组织户外活动,同学们随机围坐成一圈做游戏,每个同学都记住了左右同学的编号,活动结束后,老师想让你帮忙复原当时大家坐的位置,你能通过每个同学记录的左右同学的编号,把当时大家坐的一圈情况复原吗?

输入

第一行是人数n(n<100)。从第二行开始n行,分别表示1-n编号的同学左右两个同学的编号。最后一行某个同学的编号K。

输出

围坐的这个圈里,从第K个同学开始顺时针的序列。

样例输入 Copy

讯享网5 4 5 5 3 2 4 3 1 1 2 3 

样例输出 Copy

3 2 5 1 4
讯享网#include<iostream> using namespace std; struct Node{ int data; Node* ahead,*next; }; Node* Creatlist(int n) { int i,j,a,b; Node*p[100]; for(i=0;i<n;i++) { p[i]=new Node; p[i]->data=i+1; } for(j=0;j<n;j++) { cin>>a>>b; for(i=0;i<n;i++) { if(a==i+1) { p[j]->next=p[i]; break; } } for(i=0;i<n;i++) { if(b==i+1) { p[j]->ahead=p[i]; break; } } } return p[0]; } int main() { int k,i,n; cin>>n; Node*first=NULL,*temp=NULL; first=Creatlist(n); temp=first; cin>>k; for(i=0;i<n;i++) { if(temp->data==k) break; temp=temp->next; } for(i=0;i<n;i++) { cout<<temp->data<<" "; temp=temp->next; } return 0; } 

问题 C: 十进制整数转二进制

二进制是计算机运算的基础,给你一个十进制整数,你能编程实现转二进制吗?

输入

第一行n,表示接着下边有n个十进制整数,每个占一行。

输出

对应每个十进制整数输出相应二进制数占一行。

样例输入 Copy

2 5 16 

样例输出 Copy

讯享网101 10000 
#include<iostream> using namespace std; void change(int k); int main() { int n=0; cin>>n; int shi[n]; for(int i=0;i<n;i++) { cin>>shi[i]; } for(int i=0;i<n;i++) { int temp=shi[i]; change(temp); } return 0; } void change(int k) { if(k!=0) { int l,j=0; l=k; int er[1000]={}; while(l) { er[j]=l%2; l/=2; j++; } while(j--&&j>=0) { cout<<er[j]; } cout<<endl; } else { cout<<"0"<<endl; } } 

问题 D: 进出栈

设栈S的初始状态为空,元素a, b, c, d, e, f, g 依次入栈,给你一个出栈序列,请编程判断出栈序列是否正确。

输入

占一行,为出栈序列。

输出

如果出栈学列是可能的,输出True,否则输出False。

样例输入 Copy

讯享网a b c d e f g 

样例输出 Copy

True
讯享网#include<iostream> #include<assert.h> #include<string.h> using namespace std; const int stackIncreament = 20; template<class T> class Seqstack { private: T* elements; int top; int maxSize; void overflowProcess() { T* arr = new T[maxSize + stackIncreament]; for (int i = 0; i <= top; i++) arr[i] = elements[i]; delete[]elements; elements = arr; maxSize += stackIncreament; } public: Seqstack(int sz = 50) :top(-1), maxSize(sz)//构造函数建立一个空栈 { elements = new T[maxSize]; assert(elements != NULL); } ~Seqstack() { delete[]elements; } bool IsFull()const { return (top == maxSize - 1) ? true : false; } bool IsEmpty() const { return (top == -1) ? true : false; } void Push(const T& x) { if (IsFull()) overflowProcess(); elements[++top] = x; } bool Pop() { if (IsEmpty()) return false; else { top--; return true; } } T getTop() { if (IsEmpty())return 0; return elements[top]; } int getSize()const { return top + 1; } void MakeEmpty()//清空栈的内容 { top = -1; } }; int main() { char ch = 'a',str[8]; for (int i = 0; i < 7; i++) cin >> str[i]; Seqstack<char>s; int num = 0,flag=1; while (num < 7) { if (ch == str[num]) { ch++; num++; } else if (!s.IsEmpty() && s.getTop() == str[num]) { s.Pop(); num++; } else if (ch <= 'g') { s.Push(ch); ch++; } else { flag = 0; break; } } if (flag) cout << "True"; else cout << "False"; return 0; }//出栈顺序 

问题 E: 栈容量

设栈S的初始状态为空,元素a,b,c,d,e,f,g依次入栈,出栈顺序为b,d,c,f,e,a,g那么栈容量至少应该是3。如果任意给你一个出栈序列,你能编程判断相应的栈容量至少是多少吗?


讯享网

输入

元素a,b,c,d,e,f,g依次入栈情况下的一种出栈序列。

输出

对应出栈序列的栈容量至少是多少。

样例输入 Copy

b d c f e a g 

样例输出 Copy

讯享网3
#include<iostream> #include<assert.h> #include<string.h> using namespace std; const int stackIncreament = 1; template<class T> class Seqstack { private: T* elements; int top; int maxSize; void overflowProcess() { T* arr = new T[maxSize + stackIncreament]; for (int i = 0; i <= top; i++) arr[i] = elements[i]; delete[]elements; elements = arr; maxSize += stackIncreament; } public: Seqstack(int sz = 1) :top(-1), maxSize(sz)//构造函数建立一个空栈 { elements = new T[maxSize]; assert(elements != NULL); } ~Seqstack() { delete[]elements; } bool IsFull()const { return (top == maxSize - 1) ? true : false; } bool IsEmpty() const { return (top == -1) ? true : false; } void Push(const T& x) { if (IsFull()) overflowProcess(); elements[++top] = x; } bool Pop() { if (IsEmpty()) return false; else { top--; return true; } } T getTop() { if (IsEmpty())return 0; return elements[top]; } int getmaxSize()const { return maxSize; } int getSize()const { return top + 1; } void MakeEmpty()//清空栈的内容 { top = -1; } }; int main() { char ch = 'a',str[8]; for (int i = 0; i < 7; i++) cin >> str[i]; Seqstack<char>s; int num = 0,flag=1; while (num < 7) { if (ch == str[num]) { s.Push(ch); s.Pop(); ch++; num++; } else if (!s.IsEmpty() && s.getTop() == str[num]) { s.Pop(); num++; } else if (ch <= 'g') { s.Push(ch); ch++; } else { flag = 0; break; } } cout << s.getmaxSize(); return 0; }//栈容量问题 

问题 F: 自创语言

学了一段英语课之后,小名同学发现英语单词就是26个字母中若干个组合在一起的,于是他想自己也创立一种语言,为了简化单词,他计划只选26个小写字母的前n个符号构造长度也为n个符号的单词,构造好单词后,先要编写个词典,给每个单词有个解释以便人们学习他自创的语言。你能编程帮助按字典序输出所有长度为n的单词吗?

输入

占一行,为整数n(n<26)。

输出

所有由前n个符号构造长度也为n个符号的单词,按字典序每行输出一个单词。

样例输入 Copy

讯享网2 

样例输出 Copy

aa ab ba bb
讯享网#include<iostream> using namespace std; char ch[27] = "aaaaaaaaaaaaaaaaaaaaaaaaaa"; void printword(int n, int m) { int i,j; if (m == 0) { for (i = 0; i < n; i++) cout << ch[i]; cout << endl; } else { for (ch[n-m]='a',j = 0; j < n; j++, ch[n - m]++) printword(n, m - 1); } } int main() { int n, m; cin >> n; m = n; printword(n, m); return 0; }//自创语言题(递归法) 

问题 G: 离队

体育课上,班上的同学排成了一排,结果有个同学突然感觉不适,需要去医院,就离队了,你能编程模拟离队后的状态吗?

输入

第一行是整数n(n<100),第二行有n个整数,第三行是k,表示从左开始第k个离队。

输出

输出离队后的数字序列。

样例输入 Copy

5 120 125 135 126 145 3 

样例输出 Copy

讯享网120 125 126 145
#include<iostream> using namespace std; struct Node{ int data; Node*next; }; Node *deletenode(int x,Node *head); Node *outputlist(Node *head); Node *creatlist(int a) { Node*temp,*tail=NULL,*head=NULL; int num; cin>>num; head=new Node; head->data=num; head->next=NULL; tail=head; for(int i=1;i<a;i++) { cin>>num; temp=new Node; temp->data=num; temp->next=NULL; tail->next=temp; tail=temp; } return head; } int main() { int n; Node *listhead=NULL; cin>>n; if(n>0) { listhead=creatlist(n); } //已经创建完链表 int x=0; cin>>x; listhead=deletenode(x,listhead);//删除 outputlist(listhead);//输出 return 0; } Node *deletenode(int x,Node *head) { Node *prep=NULL; Node *px=head; for(int i=1;i<x;i++) { prep=px; px=px->next; } prep->next=px->next; delete px; return head; } Node*outputlist(Node *head) { Node *ps=head; while(ps!=NULL) { cout<<ps->data<<" "; ps=ps->next; } } 

问题 H: 入队

体育课上,上课铃响后,大家排成了一排,结果有一个同学迟到了,老师让他插在这一排的某个位置,你能编程模拟这个过程吗?

输入

第一行是整数n(n<100),第二行n个整数,第三行是整数m和要插入的位置k(位置从左往右依次从1排序)。

输出

入队后的n+1个数据序列。

样例输入 Copy

讯享网5 123 125 128 121 145 136 2 

样例输出 Copy

123 136 125 128 121 145
讯享网#include<iostream> using namespace std; struct Node{ int data; Node*next; }; Node *addnode(int x,int y,Node *head); Node *outputlist(Node *head); Node *creatlist(int a) { Node*temp,*tail=NULL,*head=NULL; int num; cin>>num; head=new Node; head->data=num; head->next=NULL; tail=head; for(int i=1;i<a;i++) { cin>>num; temp=new Node; temp->data=num; temp->next=NULL; tail->next=temp; tail=temp; } return head; } int main() { int n; Node *listhead=NULL; cin>>n; if(n>0) { listhead=creatlist(n); } //已经创建完链表 int x=0,y=0;//x是插入的数,y是插入的位置 cin>>x>>y; listhead=addnode(x,y,listhead);//插入 outputlist(listhead);//输出 return 0; } Node *addnode(int x,int y,Node *head) { Node *future=NULL; Node *past=NULL; Node *now=head; for(int i=1;i<y;i++) { past=now; now=now->next; } future=new Node; future->data=x; if(past==NULL) { future->next=now; return future; } else { past->next=future; future->next=now; return head; } } Node*outputlist(Node *head) { Node *ps=head; while(ps!=NULL) { cout<<ps->data<<" "; ps=ps->next; } } 

 

 

小讯
上一篇 2025-01-07 08:30
下一篇 2025-03-09 15:11

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/50221.html