一: 汉诺塔(纯c语言)
输出每一步移动盘子的记录。一次移动一行。 每次移动的记录为例如3:a->b 的形式,即把编号为3的盘子从a杆移至b杆。
我们约定圆盘从小到大编号为1, 2, ...n。即最上面那个最小的圆盘编号为1,最下面最大的圆盘编号为n。
总所周知,汉诺塔问题递归是比较方便的
#include <stdio.h>
void h(int n,char a,char b,char c)
{
if(n==1)
printf("1:%c->%c\n",a,c);
else
{
h(n-1,a,c,b);
printf("%d:%c->%c\n",n,a,c);
h(n-1,b,a,c);
}
}
void main( )
{ int n,i=2;
scanf("%d",&n);
h(n,'a','b','c');
}
二:链表(c++)
链表的话c++创建简单一点,因为开辟空间只要new就行
顺序表(删除)
已知一组数据,采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素
输入格式:
输入包含三行数据,第一行是表中元素个数,第二行是顺序表的各个元素,第三行是区间x和y。
输出格式:
删除元素值在[x,y]之间的所有元素后,输出新的顺序表。(最后无空格)
输入样例:
在这里给出一组输入。例如:
10
55 11 9 15 67 12 18 33 6 22
10 20
结尾无空行
输出样例:
在这里给出相应的输出。例如:
55 9 67 33 6 22
#include <iostream>
using namespace std;
typedef struct shuju
{
int a;
struct shuju *next;
}s;
s *shuru (int n) //链表创建
{
int i;
s*head=new s;
s*pre=head;
for(i=1;i<=n;i++)
{
s*node=new s;
cin>>node->a;
pre->next=node;
node->next=NULL;
pre=node;
}
return head;
}
int main()
{
int n,x,y,con=0;
cin>>n; //节点数
s*head1=shuru(n); //调用链表
s*node=head1->next;
cin>>x>>y; //输入范围
while(node!=NULL)
{
if(con!=0&&(node->a>y||node->a<x)) cout<<" ";
con++;
if(node->a>y||node->a<x)
{cout<<node->a;}
node=node->next;
}
}