SDUT 第12周周赛

题目链接:http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=425#overview

A题:转置矩阵,水题,直接交换行和列的值。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int ma[110][110],n;
void solve()
{
	for(int i=1;i<=n;i++)
		for(int j=1;j<i;j++)
		swap(ma[i][j],ma[j][i]);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
	    {
			if(j!=n)
				printf("%d ",ma[i][j]);
			else
				printf("%d\n",ma[i][j]);
	    }
}
int main()
{
	int cas=1;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			scanf("%d",&ma[i][j]);
		solve();
	}
	return 0;
}

B题:完美数,打表水过。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int ans[5]={6,28,496,8128,33550336},l,r;
int main()
{
	while(~scanf("%d%d",&l,&r))
	{
		if(!l&&!r)break;
		int t=0,ok=0;
		for(int i=0;i<=4;i++)
		{
			if(ans[i]>=l&&ans[i]<=r&&!t)
			{
				printf("%d",ans[i]);
				t++;ok=1;
			}
			else if(ans[i]>=l&&ans[i]<=r&&t)
				printf(" %d",ans[i]);
		}
		if(ok)
		puts("");
		else
		puts("No");
	}
	return 0;
}

C题:素数判定,素数筛水过。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int l,r;
bool vis[10000010];
void init()
{
	memset(vis,1,sizeof(vis));
	vis[1]=0;
	for(int i=2;i<=10000010;i++)
	{
		if(vis[i])
		{
			for(int j=2;j*i<=10000010;j++)
				vis[j*i]=0;
		}
	}
}
int main()
{
	init();
	while(scanf("%d%d",&l,&r)!=EOF)
	{
		if(!l&&!r)break;
		int ok=1;
		for(int i=l;i<=r;i++)
		{
			int tem=i*i+i+41;
			if(!vis[tem])
			{
				ok=0;
				break;
			}
		}
		if(ok)
			puts("OK");
		else
			puts("Sorry");
	}
	return 0;
}

D题:字符串水题。注意c++中 a__b(就是每个单词之间有两道下划线是不合法的)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
char s[110],word[110][110];
int num;
bool check()
{
	num=0;
	for(int i=0;i<=strlen(s);i++)
	{
		if(!isalpha(s[i]))return 0;
		int p=0;
		while(s[i]!='_'&&s[i]!='\0')
			word[num][p++]=s[i++];
		word[num][p]='\0';
		for(int j=0;j<p;j++)
			if(!islower(word[num][j]))
			return 0;
		if(num>0)
		word[num][0]=toupper(word[num][0]);
		num++;
	}
	return 1;
}
void solve()
{
	for(int i=0;i<num;i++)
		printf("%s",word[i]);
	puts("");
}
int main()
{
	while(scanf("%s",s)!=EOF)
	{
		if(!check())
		{
			puts("Error");
			continue;
		}
		solve();
	}
	return 0;
}

E题:求第k大素数,素数筛水过。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
int pri[1000100],n;
bool vis[10000010];
void init()
{
	int p=0;
	memset(vis,1,sizeof(vis));
	vis[1]=0;
	for(int i=2;i<=10000010;i++)
	{
		if(vis[i])
		{
			pri[p++]=i;
			vis[i]=0;
			for(int j=2;j*i<=10000010;j++)
				vis[j*i]=0;
		}
	}
}
int main()
{
	init();
	while(scanf("%d",&n)!=EOF)
	{
		printf("%d\n",pri[n-1]);
	}
	return 0;
}

F题:dfs。注意如果遇到‘#’会放弃当前这一步去走下一步 而不是直接跳出。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
char ma[50][50],str[10001];
int n,m,sx,sy,len,ok;
bool vis[10];
void dfs(int x,int y,int num)
{
	if(ma[x][y]>='1'&&ma[x][y]<='7')
		vis[ma[x][y]-'0']=1;
	if(num>=len)
	{
		for(int i=1;i<=7;i++)
		if(!vis[i])
		{
			ok=0;
			return ;
		}
		return ;
	}
	if(str[num]=='R'&&ma[x][y+1]!='#')
		dfs(x,y+1,num+1);
	else if(str[num]=='R'&&ma[x][y+1]=='#')
		dfs(x,y,num+1);
	else if(str[num]=='L'&&ma[x][y-1]!='#')
		dfs(x,y-1,num+1);
	else if(str[num]=='L'&&ma[x][y-1]=='#')
		dfs(x,y,num+1);
	else if(str[num]=='U'&&ma[x-1][y]!='#')
		dfs(x-1,y,num+1);
	else if(str[num]=='U'&&ma[x-1][y]=='#')
		dfs(x,y,num+1);
	else if(str[num]=='D'&&ma[x+1][y]!='#')
		dfs(x+1,y,num+1);
	else if(str[num]=='D'&&ma[x+1][y]=='#')
		dfs(x,y,num+1);
}
int main()
{

	while(~scanf("%s%d%d",str,&n,&m)){
	memset(ma,'#',sizeof(ma));
	len=strlen(str);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",ma[i]+1);
		for(int j=1;j<=m;j++)
			if(ma[i][j]=='S')
		    {
				sx=i;
				sy=j;
		    }
	}
	memset(vis,0,sizeof(vis));
	ok=1;dfs(sx,sy,0);
	if(ok)
		puts("Yes");
	else
		puts("No");
	}
	return 0;
}



版权声明:本文为qq_16255321原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。