@[TOC](灵动ICPC冬令营基础-1)

灵动ICPC冬令营基础-1

A - Specialized Four-Digit Numbers

#include<stdio.h>
int Calc(int a,int b){
	int sum=0,hi;
	for(;b;b/=a)
	sum+=b%a;
	return sum;
}
int main(){
	int i,t;
	for(i=2992;i<=9999;i++)
	{
	  t=Calc(10,i);
      if(t==Calc(12,i)&&t==Calc(16,i))
      {
		printf("%d\n",i);
	  }
    }
	return 0;
}

进制转换

B - Pig-Latin

#include<stdio.h>
char b[1000001];
int zimu(char n )
{
	if(n>='a'&&n<='z'||n>='A'&&n<='Z') return 1;
	return 0;
}
int yuanyin(char a)
{
	if(a=='a'||a=='o'||a=='e'||a=='i'||a=='u'||a=='A'||a=='O'||a=='E'||a=='I'||a=='U') return 1;
	return 0;
}
int main(){
	while(gets(b))
	{
	  int s=0,t=0,i;
	  while(b[s]) 
	  {
	  	if(zimu(b[s])==0)
	  	{
	  		printf("%c",b[s++]);
	  		t=s;
		}
		else if(zimu(b[t]))	t++;
		else 
		{
			if(yuanyin(b[s])==0)
			{
				for(i=s+1;i<t;i++)
				   printf("%c",b[i]);
				printf("%c",b[s]);
			}
			else
			{
				for(i=s;i<t;i++)
				   printf("%c",b[i]);
			}
			printf("ay");
				s=t;
		} 
	  }printf("\n");
    }
	return 0;
}

判断是否为字母以及是否为元音字母

C - Tic Tac Toe

#include<stdio.h>
char a[4][4];
int panduan(char c){
	int i,j;
	for(i=0;i<3;++i)
	{
		for(j=0;j<3&&a[i][j]==c;++j)
		if(j==2) return 1;//行 
	    for(j=0;j<3&&a[j][i]==c;++j)
		if(j==2) return 1;//列 
	}
		for(i=0;i<3&&a[i][i]==c;++i)
		if(i==2) return 1;//主 
		for(i=0;i<3&&a[i][2-i]==c;++i)
		if(i==2) return 1;//副 
		return 0;
}
int main(){
	int o,x,flag,n,i,j;
	while(scanf("%d",&n)!=EOF)
	{
		while(n--)
		{
			x=0;
			o=0;
			for(i=0;i<3;++i)
			scanf("%s",a[i]);
			flag=1;
			for(i=0;i<3;i++)
			{
				for(j=0;j<3;j++)
				{
					if(a[i][j]=='X') x++;
					if(a[i][j]=='O') o++;				 
				}
			}
			if(panduan('X')&&panduan('O')) flag=0;
			if(x<o)  flag=0;
			if(x-o>=2) flag=0;
			if(panduan('O')==1&&x!=o) flag=0;
			if(panduan('X')==1&&x==o) flag=0;
		 if(flag) printf("yes\n");
		 else printf("no\n");
		}
	} 
	return 0;
}

井字棋要先判断是否胜利再看是否满足正常的条件

D - Factorial! You Must be Kidding!!!

#include<stdio.h>
long long f(int n)
{
	int i,a=1;
	for(i=1;i<n;i++)
	{
		a=i*a;
	}
	return a;
}
int main(){
	int n;
	while(scanf("%d",&n))
	{
	if(n<=7||n<0&&(-n)%2==0) printf("Underflow!\n");
	if(n>=13||n<0&&(-n%2)==1) printf("Overflow!\n");
	else printf("%lld\n",f(n)); 
    }
	return 0;
}

要注意一下n可以小于0

E - Function Run Fun

#include<stdio.h>
int f[21][21][21];
int w(int a,int b,int c)
{
    if (a<=0||b<=0||c<=0) return 1;
    if (a>20||b>20||c>20)return w(20,20,20);
    if (f[a][b][c]) return f[a][b][c];
    return f[a][b][c] = w(a-1,b,c) + w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
}
int main(){
	int a,b,c;
	while(scanf("%d%d%d",&a,&b,&c)!=EOF)
	{
		if(a==-1&&b==-1&&c==-1) break;
		else printf("w(%d, %d, %d) = %d\n",a,b,c,w(a,b,c));
	}
	return 0;
}

阶乘函数

F - Simple Addition

#include<stdio.h>
int f(int n)
{
	if(n%10) return n%10;
	if(n==0) return 0;
	else return f(n/10); 
}
long long s(int q,int p)
{
	long long sum=0;
	while(q<=p)
	sum+=f(q++);
	return sum;
}
long long a(int q,int p)
{
	if(p-q<9) return s(q,p);
	long long ans=0;
	int l=((q/10+1)*10);
	int r=((p/10-1)*10);
	ans+=s(q,l-1);
	ans+=s(r+1,p);
	ans+=(((r-l)/10)*45);
	ans+=a(l/10,r/10);
	return ans;
}
int main(){
	int q,p;
	while(scanf("%d%d",&q,&p))
	{
	if(p<0&&q<0) break;
	printf("%d\n",a(q,p));
    }
	return 0;
} 

递归函数

G - A Contesting Decision

#include<stdio.h>
#include<string.h>
struct team{
	char name[11];
	int p[4];
	int t[4];
	int sum;
	int fa;
};
int main(){
	int i,j,n,wsol,wfa;
	char wname[11];
	scanf("%d",&n);
	team team [1000];
	for(i=0;i<n;i++)
	{
		scanf("%s",&team[i].name);
		team[i].sum=team[i].fa =0;
			for(j=0;j<4;j++)
		{
			scanf("%d%d",&team[i].p[j],&team[i].t[j]);
			if(team[i].t[j])   
			{
			  team[i].sum+=1;
			  team[i].fa+=(team[i].p[j]-1)*20+team[i].t[j];
		    }
		}
	} 
	strcpy(wname,team[0].name), wsol=team[0].sum,wfa=team[0].fa;
	for(i=1;i<n;i++)
	{
	   if((team[i].sum>wsol) ||(team[i].sum==wsol && team[i].fa<wfa))
	   strcpy(wname,team[i].name), wsol=team[i].sum,wfa=team[i].fa;
    }
    printf("%s %d %d",wname, wsol,wfa);
	return 0;
}

结构体的排序

上一篇:如何做好技术 Team Leader


下一篇:MMM 高可用