团体程序设计天梯赛-模拟赛(2016年)

没有写完,不定时更新

L1-1  N个数求和 

读入每个分数进行相加,每次相加都要化简。

输出要求假分数,若分母为1,则输出整数。


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
long long com(long long x,long long y);
int main(){
  int n;
  scanf("%d",&n);
  long long x,y;
  long long ansx,ansy;
  if(n>=1){	//第一个分数 要约分,有可能只有一个分数 
    scanf("%lld/%lld",&ansx,&ansy);
    long long t = com(ansx,ansy);
    ansx/=t;
    ansy/=t;
  }//对接下来的每一个分数相加 
  for(int i=1;i<n;i++){
    scanf("%lld/%lld",&x,&y);
    long long a = x*ansy + y*ansx;	//直接同分母相加 
    long long b = ansy*y;
    long long t = com(a,b);
    a/=t;
    b/=t;
    ansx = a;
    ansy = b;
  }//若分母为1,则只显示整数 
  if(ansy==1){
    printf("%lld\n",ansx);
  }
  else{	//以假分数的形式输出 
    if(ansx>ansy){
      printf("%lld %lld/%lld\n",ansx/ansy,ansx%ansy,ansy);
    }
    else
    printf("%lld/%lld\n",ansx,ansy);
  }
  return 0;
}
//最大公约数 
long long com(long long x,long long y){
  if(x==0){
    return abs(y);//要返回绝对值
  }
  else return com(y%x,x);
}



L1-2 比较大小

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
  long long x[3];
  scanf("%lld%lld%lld",&x[0],&x[1],&x[2]);
  sort(x,x+3);
  printf("%lld->%lld->%lld\n",x[0],x[1],x[2]);
  return 0;
}


L1-3 A-B


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
  char a[10100],b[10100];
  gets(a);
  gets(b);
  char book[500];
  memset(book,0,sizeof(book));
  int l = strlen(b);
  for(int i=0;i<l;i++){
    book[b[i]] = 1;
  }
  l = strlen(a);
  for(int i=0;i<l;i++){
    if(book[a[i]]==0){
      printf("%c",a[i]);
    }
  }
  printf("\n");
  return 0;
}

L1-4计算指数

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
  int n;
  scanf("%d",&n);
  int ans = 1;
  for(int i=0;i<n;i++){
    ans *= 2;
  }
  printf("%d^%d = %d\n",2,n,ans);
  return 0;
}

L1-5  计算阶乘和

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
  int n;
  scanf("%d",&n);
  int ans = 0;
  for(int i=1;i<=n;i++){
    int temp = 1;
    for(int j=2;j<=i;j++){
      temp *= j;
    }
    ans += temp;
  }
  printf("%d\n",ans);
  return 0;
}
L1-6跳过

L1-7 跟奥巴马一起画方块

注意要求四舍五入

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
  int n;
  char a;
  scanf("%d %c",&n,&a);
  for(int i=0;i<n/2;i++){
    for(int j=0;j<n;j++){
      printf("%c",a);
    }
    printf("\n");
  }
  if(n%2==1){
    for(int j=0;j<n;j++){
      printf("%c",a);
    }
    printf("\n");
  }
  return 0;
}

L1-8  查验身份证
不光要计算权重,而且要检查前17位是否为数字。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
char d[15] = "10X98765432";
int q[20] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int main(){
  int n;
  scanf("%d",&n);
  char a[20];
  int count = 0;
  for(int i=0;i<n;i++){
    scanf("%s",a);
    int temp = 0;
    int s = 0;
    for(int i=0;i<17;i++){
      if(a[i]>='0'&&a[i]<='9')
      ;
      else  s = 1;
      temp += q[i]*(a[i]-'0');
      temp %=11;
    }
    if(d[temp] != a[17]||s){
      printf("%s\n",a);
      count++;
    }
  }
  if(count==0)  printf("All passed\n");
  return 0;
}

L2-1  集合相似度
集合排列,两集合从小到大比较,计算有几个符合要求的数。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
vector<int > a[60];
int main(){
  int n;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    int t;
    scanf("%d",&t);
    for(int j=0;j<t;j++){
      int temp;
      scanf("%d",&temp);
      a[i].push_back(temp);
    }
    sort(a[i].begin(),a[i].end());	//从小到大排列 
  }
  int k;
  scanf("%d",&k);
  for(int i=0;i<k;i++){
    int x,y;
    scanf("%d%d",&x,&y);
    x--,y--;
    int num = 0;
    int count = 0;
    int p = 0,q = 0;
    for(;p<a[x].size()&&q<a[y].size();){
      if(a[x][p]==a[y][q]){
        if((p==0||a[x][p]!=a[x][p-1])&&(q==0||a[y][q]!=a[y][q-1])){
          count++;
          num++;
          p++;
          q++;
        }
        else{
          q++;
          p++;
        }
      }
      else if(a[x][p]<a[y][q]){
        if((p==0||a[x][p]!=a[x][p-1])){
          num++;
        }
        p++;
      }
      else{
        if((q==0||a[y][q]!=a[y][q-1])){
          num++;
        }
        q++;
      }
    }
    while(p<a[x].size()){
      if(p==0||a[x][p]!=a[x][p-1]){
        num++;
      }
      p++;
    }
    while(q<a[y].size()){
      if(q==0||a[y][q]!=a[y][q-1]){
        num++;
      }
      q++;
    }  
    printf("%.2lf%%\n",((double)count*100)/num);
  }
  return 0;
}

L2-2   树的遍历

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<string>
#include<algorithm>
#include<stdlib.h>

using namespace std;
int a[33];
int b[33];
void out(struct tree* t);
struct tree{
  struct tree* left;
  struct tree* right;
  int x;
};
  int n;
  struct tree* solve(int x,int y,int p,int q);
int main(){

  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    scanf("%d",&a[i]);
  }
  for(int i=1;i<=n;i++){
    scanf("%d",&b[i]);
  }
  struct tree* t = solve(1,n,1,n);
  out(t);
  
  return 0;
}

void out(struct tree* t){
  queue<struct tree*> q;
  q.push(t);
  int c = 0;
  while(!q.empty()){
    struct tree* temp = q.front();
    q.pop();
    if(temp){
      printf("%d",temp->x);
        c++;
    if(c==n)  printf("\n");
    else printf(" ");
    q.push(temp->left);
    q.push(temp->right);
    }
  }
}

struct tree* solve(int x,int y,int p,int q){
  if(x>y)  return NULL;
  struct tree* t = (struct tree*)malloc(sizeof(tree));
  t->x = a[y];
  int g = p;
  for(;g<=q;g++){
    if(b[g]==a[y]){
      break;
    }
  }
  t->left = solve(x,g-p+x-1,p,g-1);
  t->right = solve(g-p+x,y-1,g+1,q);
  return t;
}

L2-3家庭房产
Advanced Level里的1114题

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
#define MAX 10000
int p[MAX];	//标记
double sets[MAX];
double area[MAX];
struct node{
	int n;	//人数 
	int id;	//ID(min)
	double s;	//sets(sum)
	double a; 	//area(sum)
	bool operator <(const node& b)const{
		if(a==b.a){
			return id<b.id;
		}
		return a>b.a;
	}
};
vector<node> r;
vector <int >a[10000];
int K;
double S,A;
void dfs(int t);
int main(){
	int N;
	scanf("%d",&N);
	memset(p,0,sizeof(p));
	memset(sets,0,sizeof(sets));
	memset(area,0,sizeof(area));
	for(int i=0;i<N;i++){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		p[x] = 1;
		if(y!=-1){
			p[y] = 1;
			a[x].push_back(y);
			a[y].push_back(x);
		}
		if(z!=-1){
			p[z] = 1;
			a[x].push_back(z);
			a[z].push_back(x);
		}
		int k;
		scanf("%d",&k);
		for(int j=0;j<k;j++){
			int temp;
			scanf("%d",&temp);
			p[temp] = 1;
			a[temp].push_back(x);
			a[x].push_back(temp);
		}
		scanf("%lf%lf",&sets[x],&area[x]);
	}
	for(int i=0;i<MAX;i++){
		if(p[i]){
			K = 0;
			S = A = 0;
			dfs(i);
			node t;
			t.id = i;
			t.n = K;
			t.s = S/K;
			t.a = A/K;
			r.push_back(t);
		}
	}
	sort(r.begin(),r.end());
	printf("%d\n",r.size());
	for(int i=0;i<r.size();i++){
		printf("%04d %d %.3lf %.3lf\n",r[i].id,r[i].n,r[i].s,r[i].a);
	}
	return 0;
}

void dfs(int t){
	t = min(t,10000);
	S+=sets[t];
	A+=area[t];
	K++;
	p[t] = 0;
	for(int i=0;i<a[t].size();i++){
		if(p[a[t][i]])
		dfs(a[t][i]);
	}	
}


L2-4 最长对称子串

应该用manacher的,基础不扎实,暴力搜了。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
  char a[2000];
  gets(a);
  int l = strlen(a);
  int ans = 0;
  for(int i=0;i<l;i++){
    int x,y;
    int count = 1;
    x = i-1;
    y = i+1;
    while(x>=0&&y<l){
      if(a[x]==a[y])  count+=2;
      else break;
      x--;
      y++;
    }
    if(ans<count)ans = count;
    x = i;
    y = i+1;
    count = 0;
    while(x>=0&&y<l){
      if(a[x]==a[y])  count+=2;
      else break;
      x--;
      y++;
    }
    if(ans<count)ans = count;
    }
    printf("%d\n",ans);
  return 0;
}


L3-1 肿瘤诊断

BFS,标记搜就可以了。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
int a[66][1299][140];
char book[66][1299][140];
class node{
public:
	int x;
	int y;
	int z;
	node(int a,int b,int c){
		x = a;
		y = b;
		z = c;
	}
	node(){
		
	}
	node(const node& t){
		x = t.x;
		y = t.y;
		z = t.z;
	}
};
int solve(int x,int y,int z);
int d[6][3] = {0,1,0,0,0,1,0,-1,0,0,0,-1,1,0,0,-1,0,0};
int main(){
	int M,N,L,T;
	scanf("%d%d%d%d",&N,&M,&L,&T);
	for(int i=0;i<L;i++){
		for(int j=0;j<N;j++){
			for(int k=0;k<M;k++){
				scanf("%d",&a[i][j][k]);
			}
		}
	}
	memset(book,0,sizeof(book));
	int ans = 0;
	for(int i=0;i<L;i++){
		for(int j=0;j<N;j++){
			for(int k=0;k<M;k++){
				if(a[i][j][k]==1&&book[i][j][k]==0){
					int temp = solve(i,j,k);
					if(temp>=T)	ans+=temp;
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}
int solve(int x,int y,int z){
	node t(x,y,z);
	queue<node>q;
	q.push(t);
	book[x][y][z] = 1;
	int sum = 1;
	while(!q.empty()){
		t = q.front();
		q.pop();
		for(int i=0;i<6;i++){
			node temp = t;
			temp.x += d[i][0];
			temp.y += d[i][1];
			temp.z += d[i][2];
			if(a[temp.x][temp.y][temp.z]==1&&book[temp.x][temp.y][temp.z]==0){
				book[temp.x][temp.y][temp.z] = 1;
				sum++;
				q.push(temp);
			}
		}
	}
	return sum;
}


。。。待续


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值