目录
单链表:(新增了一个清屏操作)
#include<bits/stdc++.h>
using namespace std;
typedef struct nood
{
int a;
nood *next;
}nood;
nood *bg;
nood *nt;
void chushi(nood *&ans)
{
ans=(nood*)malloc(sizeof(nood));
ans->a=0;
ans->next=NULL;
}
void init()
{
cout<<"请输入要插入的元素的个数"<<endl;
int geshu;
cin>>geshu;
cout<<"请输入你要的元素"<<endl;
nt=bg;
while(geshu--)
{
int x;
cin>>x;
nood *pp;
chushi(pp);
pp->a=x;
nt->next=pp;
nt=nt->next;
}
cout<<"初始化成功"<<endl;
}
void add()
{
cout<<"请输入你要插入的下标位置"<<endl;
int x;
cin>>x;
nt=bg;
int flag=0;
while(x--)
{
if(nt->next==NULL)
{
flag=1;
break;
}
nt=nt->next;
}
if(flag)
{
cout<<"对不起,你的输入有误"<<endl;
}
else
{
cout<<"请输入你要插入的值"<<endl;
int ans;
cin>>ans;
nood *pp;
chushi(pp);
pp->a=ans;
if(nt->next==NULL)
{
nt->next=pp;
pp->next=NULL;
}
else
{
pp->next=nt->next;
nt->next=pp;
}
cout<<"插入成功"<<endl;
}
}
void print()
{
nt=bg;
if(nt->next==NULL)cout<<"没有元素"<<endl;
cout<<"链表的所有元素如下:"<<endl;
while(nt->next!=NULL)
{
nt=nt->next;
cout<<nt->a<<" ";
}
cout<<endl;
}
void del()
{
cout<<"请输入你要删除的位置"<<endl;
int x;
cin>>x;
x--;
nt=bg;
int flag=0;
while(x--)
{
if(nt->next==NULL)
{
flag=1;
break;
}
nt=nt->next;
}
if(flag)
{
cout<<"对不起,您的输入有误请重新输入"<<endl;
}
else
{
if(nt->next==NULL)cout<<"对不起,您的输入有误请重新输入"<<endl;
else
{
if(nt->next->next!=NULL)nt->next=nt->next->next;
else nt->next=NULL;
cout<<"删除成功"<<endl;
}
}
}
void finds()
{
cout<<"请输入你要查找的元素"<<endl;
int x;
cin>>x;
nt=bg;
int cnt=0;
vector<int>ans;
while(nt->next!=NULL)
{
cnt++;
nt=nt->next;
if(nt->a==x)ans.push_back(cnt);
}
if(ans.size()==0)cout<<"查无此元素"<<endl;
else
{
cout<<"该元素的下标为:"<<endl;
for(int i=0;i<ans.size();i++)cout<<ans[i]<<" ";
cout<<endl;
}
}
void findx()
{
cout<<"请输入你要查询的元素位置"<<endl;
int x;
cin>>x;
nt=bg;
int flag=0;
while(x--)
{
if(nt->next==NULL)
{
flag=1;
break;
}
nt=nt->next;
}
if(flag)cout<<"输入不正确,请您重新输入"<<endl;
else
{
cout<<"该位置的元素值为:"<<nt->a<<endl;
}
}
void caidan()
{
cout<<1<<":初始化元素"<<" "<<2<<":插入元素"<<" "<<3<<"输出所有元素";
cout<<" "<<4<<":删除元素"<<" "<<5<<":查找元素"<<" "<<6<<"查找位置"<<endl;
}
int main()
{
chushi(bg);
chushi(nt);
int s;
caidan();
while(cin>>s)
{
if(s==1)
{
init();
system("pause");
system("cls");
caidan();
}
else if(s==2)
{
add();
system("pause");
system("cls");
caidan();
}
else if(s==3)
{
print();
system("pause");
system("cls");
caidan();
}
else if(s==4)
{
del();
system("pause");
system("cls");
caidan();
}
else if(s==5)
{
finds();
system("pause");
system("cls");
caidan();
}
else if(s==6)
{
findx();
system("pause");
system("cls");
caidan();
}
else
{
cout<<"运行结束,再见"<<endl;
system("pause");
return 0;
}
}
}
进制转换:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"请输入你要转换的数"<<endl;
cin>>n;
int t=n;
vector<int>ans;
while(t)
{
ans.push_back(t%2);
t/=2;
}
cout<<"二进制为:";
while(ans.size())
{
cout<<ans.back();
ans.pop_back();
}
cout<<endl;
t=n;
while(t)
{
ans.push_back(t%8);
t/=8;
}
cout<<"八进制为:";
while(ans.size())
{
cout<<ans.back();
ans.pop_back();
}
cout<<endl;
t=n;
while(t)
{
ans.push_back(t%16);
t/=16;
}
cout<<"十六进制为:";
while(ans.size())
{
if(ans.back()<=9&&ans.back()>=1)cout<<ans.back();
else cout<<(char)('a'+ans.back()-10);
ans.pop_back();
}
cout<<endl;
}
快速转置:(自定义快速排序,比快速专置o(n)慢一些,时间复杂度:nlog(n) )
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int arr[N][N];
struct nood
{
int x;
int y;
int z;
};
bool cmp(nood a,nood b)
{
if(a.x-b.x)
{
return a.x<b.x;
}
else
{
if(a.y-b.y)return a.y<b.y;
}
return false;
}
int main()
{
int n,m;
cout<<"请输入矩阵的行和列"<<endl;
cin>>n>>m;
cout<<"请输入三元组的非0元素个数"<<endl;
int t;
cin>>t;
vector<nood>ans;
cout<<"请输入转置前的三元组"<<endl;
while(t--)
{
int a,b,c;
cin>>a>>b>>c;
swap(a,b);
ans.push_back({a,b,c});
}
sort(ans.begin(),ans.end(),cmp);
cout<<"转置后的三元组为:"<<endl;
for(int i=0;i<ans.size();i++)
{
cout<<ans[i].x<<" "<<ans[i].y<<" "<<ans[i].z<<endl;
}
cout<<endl;
}