题目很水就是全都是坑真是丧心病狂啊
把代码留下造福后来人QωQ
结构体与链表练习
生日相同2.0
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 200
using namespace std;
struct st
{
int m,d;
string name;
bool operator<(const st& a)const{
if (m!=a.m) return m<a.m;
if (d!=a.d) return d<a.d;
if (name.length()!=a.name.length()) return name.length()<a.name.length();
return name<a.name;
}
}s[MAXN];
int n;
int lastm,lastd;
int num;
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
cin>>s[i].name>>s[i].m>>s[i].d;
sort(s+1,s+n+1);
lastm=s[1].m,lastd=s[1].d;
for (int i=2;i<=n;i++)
{
if (s[i].m!=lastm||s[i].d!=lastd)
{
lastm=s[i].m;
lastd=s[i].d;
}
else num++;
}
if (num)
{
cout<<s[1].m<<' '<<s[1].d<<' '<<s[1].name<<' ';
lastm=s[1].m,lastd=s[1].d;
for (int i=2;i<=n;i++)
{
if (s[i].m!=lastm||s[i].d!=lastd)
{
cout<<endl;
lastm=s[i].m;
lastd=s[i].d;
cout<<lastm<<' '<<lastd<<' '<<s[i].name<<' ';
}
else
cout<<s[i].name<<' ';
}
}
else cout<<"None"<<endl;
}
删除数组中的元素(链表)
P.s这个题好像并不像他说的那样强制链表
据说有人数组模拟过了…
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct s
{
s *prev,*next;
int num;
};
int n,m;
int main()
{
s *last=NULL,*temp,*tmp;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
int t;
scanf("%d",&t);
s *N=new s;
N->prev=NULL;N->next=NULL;
N->prev=last;
if (last) last->next=N;
N->num=t;
last=N;
if (i==1) temp=N;
}
tmp=temp;
scanf("%d",&m);
while (temp!=NULL)
{
if (temp->num==m)
{
if (temp->prev) temp->prev->next=temp->next;
else tmp=temp->next;
if (temp->next) temp->next->prev=temp->prev;
}
temp=temp->next;
}
while (tmp!=NULL)
{
printf("%d ",tmp->num);
s *t=tmp;
tmp=tmp->next;
delete t;
}
}
统计学生信息(使用动态链表完成)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct st
{
string num,name,add,sex;
int age;
double data;
st *prev,*next;
};
int main()
{
string s;
st *last=NULL;
while (cin>>s,s!="end")
{
st *N=new st;
N->prev=NULL;N->next=NULL;
N->num=s;
cin>>N->name>>N->sex>>N->age>>N->data>>N->add;
N->prev=last;
last=N;
}
while (last)
{
st *t=last;
cout<<t->num<<' '<<t->name<<' '<<t->sex<<' '<<t->age<<' '<<t->data<<' '<<t->add<<endl;
last=last->prev;
delete t;
}
}
指针练习
数组逆序重放
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
int n;
int a[MAXN];
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=n;i>=1;i--) printf("%d ",a[i]);
}
字符串中最长的连续出现的字符
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1000
using namespace std;
string s;
int T;
int a[MAXN];
int main()
{
cin>>T;
while (T--)
{
cin>>s;
memset(a,0,sizeof(a));
int num=1;
int maxn=0;
char t;
for (int i=1;i<=s.length();i++)
{
if (s[i]!=s[i-1])
{
if (num>maxn)
{
maxn=num;
t=s[i-1];
}
num=1;
}
else num++;
}
cout<<t<<' '<<maxn<<endl;
}
}
文字排版
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10000
using namespace std;
string s;
int num;
int n;
bool flag;
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
cin>>s;
if (!flag) num+=s.length();
else num+=s.length()+1;
if (num<=80)
{
if (!flag) cout<<s;
else cout<<' '<<s;
flag=1;
}
else
{
cout<<endl<<s;
num=s.length();
flag=1;
}
}
}
计算矩阵边缘元素之和
一边读一边处理吧…
要不然如果暴力加减再减去四角容易WA
因为有可能矩阵变成一维的话会出问题好像
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 200
using namespace std;
int T;
int m,n;
int main()
{
cin>>T;
while (T--)
{
cin>>m>>n;
int x;
long long sum=0;
for (int i=1;i<=m;i++)
for (int j=1;j<=n;j++)
{
cin>>x;
if (i==1||i==m||j==1||j==n) sum+=x;
}
cout<<sum<<endl;
}
}
二维数组右上左下遍历
全题库最蛋疼的题之一
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 200
using namespace std;
int m,n;
int a[MAXN][MAXN];
int ans[100000];
int top;
int main()
{
cin>>m>>n;
for (int i=1;i<=m;i++)
for (int j=1;j<=n;j++)
cin>>a[i][j];
for (int i=2;i<=m+n;i++)
for (int j=1;j<=m;j++)
for (int k=1;k<=n;k++)
if (j+k==i) ans[++top]=a[j][k];
for (int i=1;i<=top;i++) printf("%d\n",ans[i]);
}
函数递归练习(4)
回文串判断
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
string s1,s2;
int main()
{
cin>>s1;
for (int i=s1.length()-1;i>=0;i--) s2+=s1[i];
if (s1==s2)
cout<<1<<endl;
else
cout<<0<<endl;
}
八皇后问题
按列搜就能打印出样例的样子
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100
using namespace std;
bool a[MAXN],b[MAXN],c[MAXN],d[MAXN];
bool map[MAXN][MAXN];
int ans;
void dfs(int num,int x)
{
if (num==8)
{
ans++;
printf("No. %d\n",ans);
for (int i=1;i<=8;i++)
{
for (int j=1;j<=8;j++)
printf("%d ",map[i][j]);
printf("\n");
}
return;
}
for (int i=1;i<=8;i++)
{
if (!a[x]&&!b[i]&&!c[i+x]&&!d[i-x+8])
{
a[x]=1;
b[i]=1;
c[i+x]=1;
d[i-x+8]=1;
map[i][x]=1;
dfs(num+1,x+1);
a[x]=0;
b[i]=0;
c[i+x]=0;
d[i-x+8]=0;
map[i][x]=0;
}
}
}
int main()
{
dfs(0,1);
}
取石子游戏
在我blog里还有个什么欧几里得的游戏和这个题是一样的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a,b;
bool work(int a,int b,bool x)
{
if (a/b>=2) return x;
if (a%b) return work(b,a%b,!x);
else return x;
}
int main()
{
while (cin>>a>>b)
{
if (a==0&&b==0) break;
if (a<b) swap(a,b);
bool pd=0;
if (a/b>=2)
{
printf("win\n");
continue;
}
pd=work(a,b,1);
if (pd)
printf("win\n");
else
printf("lose\n");
}
}
函数递归练习(3)
全排列
STL大法好
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100
using namespace std;
string s;
int main()
{
cin>>s;
do
{
cout<<s<<endl;
}
while(next_permutation(s.begin(),s.end()));
}
分解因数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int ans,T,n;
void divide(int x,int last)
{
if (x==1)
{
ans++;
return;
}
for (int i=2;i<=x;i++)
if (x%i==0&&i<=last) divide(x/i,i);
}
int main()
{
scanf("%d",&T);
while (T--)
{
ans=0;
scanf("%d",&n);
divide(n,n);
printf("%d\n",ans);
}
}
走出迷宫
DFS果断的T了233最后写了个Heap-Dijkstra
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define MAXN 110
#define MAXX 100000
#define MAXINT 0x7fffffff
using namespace std;
bool map[MAXN][MAXN];
int num[MAXN][MAXN];
bool vis[MAXX];
int ans=MAXINT;
int sx,sy,ex,ey;
int S,t;
int n,m;
int top,tp;
string s;
struct edge
{
int to;
int w;
edge *next;
}e[MAXX*2],*prev[MAXX];
struct node
{
int x;
int dis;
bool operator<(const node& a)const{
return dis>a.dis;
}
};
int dis[MAXX];
void insert(int u,int v)
{
e[++tp].to=v;e[tp].w=1;e[tp].next=prev[u];prev[u]=&e[tp];
e[++tp].to=u;e[tp].w=1;e[tp].next=prev[v];prev[v]=&e[tp];
}
void dijkstra(int s)
{
priority_queue<node> q;
for (int i=1;i<=top;i++) dis[i]=MAXINT;
dis[s]=0;q.push((node){s,dis[s]});
while (!q.empty())
{
node t=q.top();q.pop();
if (!vis[t.x])
{
vis[t.x]=1;
for (edge *i=prev[t.x];i;i=i->next)
if (dis[i->to]>dis[t.x]+i->w)
{
dis[i->to]=dis[t.x]+i->w;
q.push((node){i->to,dis[i->to]});
}
}
}
}
int main()
{
cin>>n>>m;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
map[i][j]=1;
for (int i=1;i<=n;i++)
{
cin>>s;
for (int j=0;j<s.length();j++)
{
num[i][j+1]=++top;
if (s[j]=='S')
{
sx=i;sy=j+1;
map[i][j+1]=1;
S=top;
}
else
if (s[j]=='T')
{
ex=i;ey=j+1;
t=top;
}
else
if (s[j]=='#')
map[i][j+1]=0;
}
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
if (map[i][j])
{
if (map[i][j+1]) insert(num[i][j],num[i][j+1]);
if (map[i][j-1]) insert(num[i][j],num[i][j-1]);
if (map[i-1][j]) insert(num[i][j],num[i-1][j]);
if (map[i+1][j]) insert(num[i][j],num[i+1][j]);
}
}
dijkstra(S);
cout<<dis[t]<<endl;
}
函数递归练习(2)
扩号匹配问题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 110
using namespace std;
string s;
char ch[MAXN];
struct stack
{
int x;
int op;
}a[MAXN];
int top;
int main()
{
while (getline(cin,s))
{
top=0;memset(a,0,sizeof(a));
for (int i=0;i<s.length();i++)
{
if (s[i]=='(') a[++top].x=i,a[top].op=1;
if (s[i]==')')
{
if (a[top].op==1) top--;
else a[++top].x=i,a[top].op=2;
}
}
for (int i=0;i<s.length();i++) ch[i]=' ';
for (int i=1;i<=top;i++)
if (a[i].op==1) ch[a[i].x]='$';
else ch[a[i].x]='?';
cout<<s<<endl;
for (int i=0;i<s.length();i++) putchar(ch[i]);
cout<<endl;
}
}
输出二进制补码
TA说极限情况我的程序会被卡.反正我过了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100
using namespace std;
string s;
int a;
int ans[MAXN],top;
int main()
{