算法-藍橋杯習題(4-2)

藍橋杯習題

 

目錄

 

入門訓練(詳見 算法-藍橋杯習題(1-1)Go

 

基礎練習(詳見 算法-藍橋杯習題(2-1)Go

基礎練習(詳見 算法-藍橋杯習題(2-2)Go

算法訓練(詳見 算法-藍橋杯習題(3-1)Go

算法訓練(詳見 算法-藍橋杯習題(3-2)Go

算法訓練(詳見 算法-藍橋杯習題(3-3)Go

算法訓練(詳見 算法-藍橋杯習題(3-4)Go

算法訓練(詳見 算法-藍橋杯習題(3-5)Go

算法訓練(詳見 算法-藍橋杯習題(3-6)Go

算法提高(詳見 算法-藍橋杯習題(4-1)Go

算法提高(詳見 算法-藍橋杯習題(4-2)Go

算法提高(詳見 算法-藍橋杯習題(4-3)Go

 

歷屆試題(詳見 算法-藍橋杯習題(5-1)Go

 

歷屆試題(詳見 算法-藍橋杯習題(5-2)Go

 

藍橋杯練習系統評測數據

鏈接: https://pan.baidu.com/s/1brjjmwv
密碼: iieq

 

 

 

算法提高(PartB-10題)

 

 

/*
Torry的困惑(提高型)
*/
#include<stdio.h>
#include<math.h>
int is_prime(int x) {
	int i,s = sqrt(x);
	for(i = 2; i <= s; i++) {
		if(x % i == 0) {
			return 0;
		}
	}
	return 1;
}
int main()
{
	//("%d",is_prime(2));
	int n,count=0,i=1;
	long long result=1;
	scanf("%d",&n);
	while(count<n)
	{
		i++;
		
		if(is_prime(i))
		{
			result=(result%50000)*(i%50000)%50000;
			count++;
		}
		
	}
	
	printf("%I64d\n",result);
	return 0;
}

 

 

 

 

/*
計算時間
*/
#include <stdio.h>
#include <stdlib.h>

int b[100000][3];
int main(int argc, char *argv[]) {
	int i,n=0,a;
	scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%d",&a);
			b[i][0]=a%60;a/=60;
			b[i][1]=a%60;
			b[i][2]=a/=60;	
		}
	for(i=0;i<n;i++)
	{
		printf("%02d:%02d:%02d\n",b[i][2],b[i][1],b[i][0]);
	}
	return 0;
}

 

 

 

 

 

 

/*
最小乘積(提高型)
*/
#include "stdio.h"
#define Size 1002
int part1(int a[],int left,int right)
{
	int x;
	int low;
	int high;
	x=a[left];
	low=left;
	high=right;
	while(low<high)
	{
		while(a[high]>x&&low<high)
		{
			high--;
		}
		if(low<high)
		{
			a[low]=a[high];
			low++;
		}
		while(a[low]<x&&low<high)
		{
			low++;
		}
		if(low<high)
		{
			a[high]=a[low];
			high--;
		}
	}
	a[low]=x;
	return low;
}
int part2(int a[],int left,int right)
{
	int x;
	int low;
	int high;
	x=a[left];
	low=left;
	high=right;
	while(low<high)
	{
		while(a[high]<x&&low<high)
		{
			high--;
		}
		if(low<high)
		{
			a[low]=a[high];
			low++;
		}
		while(a[low]>x&&low<high)
		{
			low++;
		}
		if(low<high)
		{
			a[high]=a[low];
			high--;
		}
	}
	a[low]=x;
	return low;
}
void sort2(int a[],int low,int high)
{
	int mid;
	if(low<high)
	{
		mid=part2(a,low,high);
		sort2(a,low,mid-1);
		sort2(a,mid+1,high);
	}
}
void sort1(int a[],int low,int high)
{
	int mid;
	if(low<high)
	{
		mid=part1(a,low,high);
		sort1(a,low,mid-1);
		sort1(a,mid+1,high);
	}
}
int main()
{
	int T;
	int n;
	int i;
	int a[Size];
	int b[Size];
	int l;
	int sum;

	scanf("%d",&T);
	
	while(T)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
		}
		for(i=1;i<=n;i++)
		{
			scanf("%d",&b[i]);
		}
		sort1(a,1,n);
		sort2(b,1,n);
	
		sum=0;
		for(l=1;l<=n;l++)
		{
			sum=sum+(a[l]*b[l]);
		}
		printf("%d",sum);
		printf("\n");
		T--;
	}
	return 0;
}

 

 

 

 

 

 

/*
卡勒沃夫之弱水路三千(提高型)
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char mm[105][15];
char s1[15],s2[15];
int link[105][2];
int length[105]={0};
int in[105];
int N;
typedef struct{
	char name[15];
	int num;
}girl;
void fun(int x)
{
	int i,y;
	for(i=0;i<N;i++)
		if(link[i][0]==x)
		{
			y=link[i][1];
			length[ y ]=length[x]+1>=length[y]?length[x]+1:length[y];
			fun(y);
		}
}
int comp(const void *a, const void *b)
{
	return (*(girl *)a).num>(*(girl *)b).num?1:-1;
}
int main()
{
	int i,j,T,n,f,x,y,sum=0;
	girl g[105];
	scanf("%d",&T);
	while(T--)
	{
		memset(mm,0,sizeof(mm));
		memset(link,0,sizeof(mm));
		memset(in,0,sizeof(in));
		memset(length,0,sizeof(length));
		scanf("%d",&N);
		n=0;
		for(i=0;i<N;i++)
		{
			scanf("%s%s",s1,s2);
			f=0;
			for(j=0;j<n;j++)
				if(strcmp(s1,mm[j])==0)
				{
					f=1;
					break;
				}
			if(f==1)
				x=j;
			else
			{
				strcpy(mm[n++],s1);
				x=n-1;
			}
			f=0;
			for(j=0;j<n;j++)
				if(strcmp(s2,mm[j])==0)
				{
					f=1;
					break;
				}
			if(f==1)
				y=j;
			else
			{
				strcpy(mm[n++],s2);
				y=n-1;
			}
			link[i][0]=x;
			link[i][1]=y;
		}

		for(i=0;i<N;i++)
			in[link[i][1]]++;
		for(i=0;i<n;i++)
			if(in[i]==0)
				break;
		fun(i);
		
		
	
		for(i=0;i<n;i++)
		{
			strcpy(g[i].name,mm[i]);
			g[i].num=length[i];
		}
		
		qsort(g,n,sizeof(g[0]),comp);
		
		for(i=0;i<n;i++)
		{
			printf("%s",g[i].name);
			if(i==n-1)
				printf("\n");
			else
				printf(" ");
		}

  
	}

	return 0;
}

 

 

 

 

 

 

/*
最大乘積
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int max;
void fun(int *val,int index,int size,int m,int cnt,int res);
int main()
{
	int n,m,x,i;
	int val[15];
	
	scanf("%d",&x);
	while(x--)
	{
		scanf("%d %d",&n,&m);
		for(i=0;i<n;i++)
		{
			scanf("%d",&val[i]);
		}
		max = -10000000;
		fun(val,0,n,m,0,1);
		printf("%d\n",max);
	}
	return 0;
}

void fun(int *val,int index,int size,int m,int cnt,int res)
{
	if(m==cnt)
	{
		if(res > max)
		{
			max = res;
		}
		return ;
	}
	if(index>=size)
	{
		return ;
	}
	fun(val,index+1,size,m,cnt+1,res*val[index]);
	fun(val,index+1,size,m,cnt,res);
}

 

 

 

/*
和最大子序列
*/
#include<stdio.h>
int a[100001];
int main()
{
	int n,i1;
	scanf("%d",&n);
	for(i1=0;i1<n;i1++)
	      scanf("%d",&a[i1]);
    int sum=-100098087;
    for(i1=1;i1<n;i1++)
        {if(a[i1-1]>0)
           a[i1]=a[i1-1]+a[i1];
		   
		   }
   for(i1=0;i1<n;i1++)
        if(a[i1]>sum)
           sum=a[i1];
   printf("%d\n",sum);
	return 0;
}

 

/*
統計單詞數
*/
#include<stdio.h>
#include<string.h>

struct ha
{   int n;
	char c[21];
}hasi[1000];

int main()
{  int i1,i2;
	char a[1500],b[21];
	gets(a);
	int o=0,p=0;
	for(i1=0;a[i1];i1++)
	{  if(a[i1]<='z'&&a[i1]>='a')
	     a[i1]-='a'-'A';
		
	}
	for(i1=0;a[i1+1];i1++)
	{
		if(a[i1]<'A'||a[i1]>'z'||a[i1]<'a'&&a[i1]>'Z')
		{  b[o]='\0'; 
		   if(o==0)
		       continue;
		   for(i2=0;i2<p;i2++)
		      {
      			   if(!strcmp(hasi[i2].c,b))
      			   {  hasi[i2].n++;
      			      break;
	      	        }
      		}
      		if(i2==p)
      		{
		      	strcpy(hasi[p].c,b);
  	            hasi[p].n=1;
		      	p++; 
		      }
      		o=0;
      		continue;
		}
		b[o++]=a[i1];
	}
	  b[o]='\0'; 
		 
		   for(i2=0;i2<p;i2++)
		      {
      			   if(!strcmp(hasi[i2].c,b))
      			   {  hasi[i2].n++;
      			      break;
	      	        }
      		}
      		if(i2==p)
      		{
		      	strcpy(hasi[p].c,b);
  	            hasi[p].n=1;
		      	p++; 
		      }
	for(i1=0;i1<p;i1++)
	  {
  		   for(i2=0;hasi[i1].c[i2];i2++)
  		       if(hasi[i1].c[i2]<='Z'&&hasi[i1].c[i2]>='A')
  		             printf("%c",hasi[i1].c[i2]);
                else 
                    printf("%c",hasi[i1].c[i2]-'a'+'A');
           printf(":");
           for(i2=0;i2<hasi[i1].n;i2++)
              printf("*");
           printf("%d",hasi[i1].n);
           printf("\n");
  	}
	return 0;
}

 

 

 

 

 

/*
實數相加
*/
#include<stdio.h>
int a[101],b[101],c[101],d[101],e[101],f[101];
int main()
{  char k[1000],l[1001];
   int i1,i2,a1,b1,c1,d1,e1,f1;
    gets(k);
    gets(l);
    int o=0;
    for(i1=0;k[i1];i1++)
     {
     	if(k[i1]=='.')
     	   {i1++;
			break;}
        a[o++]=k[i1]-'0';
       
     }
     a1=o;
     o=0;
	 for(;k[i1];i1++)
     {
     	
        b[o++]=k[i1]-'0';
       
     }
   
     b1=o;
     o=0;
    for(i1=0;l[i1];i1++)
     {
     	if(l[i1]=='.')
     	   {i1++;
			break;}
        c[o++]=l[i1]-'0';
      
     }
  
     c1=o;
      o=0;
	 for(;l[i1];i1++)
     {
     	
        d[o++]=l[i1]-'0';
      
     }
    
     d1=o;
     o=0;
     int jiwei=0;
     for(i1=b1-1,i2=d1-1;i1>=0||i2>=0;)
     {
     	if(i1==i2)
     	 {
 	     	f[o]=(b[i1]+d[i2]+jiwei)%10;
 	     	jiwei=(b[i1]+d[i2]+jiwei)/10;
 	     	o++;i1--;i2--;
 	     }
 	     else if(i1>i2)
 	     {  f[o]=(b[i1]+jiwei)%10;
     	 	jiwei=(b[i1]+jiwei)/10;
     	 	o++;i1--;
     	 }
     	 else 
     	 {
 	        f[o]=(d[i2]+jiwei)%10;
     	 	jiwei=(d[i2]+jiwei)/10;	
     	 	o++;i2--;
 	     }
     }
      
	 f1=o;  
     
	 o=0;

     for(i1=a1-1,i2=c1-1;i1>=0||i2>=0;i1--,i2--)
     {  
     	if(i1<0)
     	{  e[o]=(c[i2]+jiwei)%10;
     	   jiwei=(c[i2]+jiwei)/10;
	     	o++;
	     }
	     else if(i2<0)
	     {e[o]=(a[i1]+jiwei)%10;
     	   jiwei=(a[i1]+jiwei)/10;
     		o++;
     	}
     	else 
     	  {e[o]=(a[i1]+c[i2]+jiwei)%10;
     	   jiwei=(a[i1]+c[i2]+jiwei)/10;
  	     	o++;
  	     }
     }
     
     if(jiwei==1)
        e[o++]=1;
        
	 for(i1=o-1;i1>=0;i1--)
	     printf("%d",e[i1]);
    
	   if(b1==0&&d1==0)
	      return 0;
      printf(".");
  
     for(i1=f1-1;i1>=0;i1--)
	     printf("%d",f[i1]); 
      return 0;
}

 

 

 

 

 

/*
項鍊
*/
#include<stdio.h>
#include<string.h>
int main()
{   
    char a[1000];
    int i1,i2;
	gets(a);
	int max=0,n=strlen(a);
	int total=0;
	for(i1=0;a[i1];i1++)
	{     
		 char p='w';
		 total=0;
		 for(i2=i1;;i2++)
		  {   if(i2>=n)
		          i2-=n;
  		     if(p=='w'||a[i2]==p||a[i2]=='w')
			   {
   			       	if(a[i2]!='w')
   			       	    p=a[i2];
   			       	total++;
   			       	
   			   }
		     else
			      break;
			if(total>=n)
			   break;	
  		  }
  		  p='w';
  	
	     for(i2=i1-1;;i2--)
	     {   if(total>=n)
		          break;
     		  if(i2<0)
     		     i2+=n;
  		      if(p=='w'||a[i2]==p||a[i2]=='w')
  		         {    if(a[i2]!='w')
         		  	 p=a[i2];
         		  	 total++;
         		  }
      		  else
         		  break;
		     
     	 } 
	
		  if(total>max)
		      max=total;
	}
	 printf("%d\n",max);
	return 0;
}

 

 

 

 

 

 

/*
交換Easy
*/
#include <stdio.h>

int main()
{
	int n,m,ary[1000],temp,op1,op2;
	scanf("%d %d",&n,&m);
	int i;
	for(i=0;i<n;i++)
	{
		scanf(" %d",&ary[i]);
	}
	for(i=0;i<m;i++)
	{
		scanf(" %d %d",&op1,&op2);
		if(op1 != op2)
		{
			temp=ary[op1-1];
			ary[op1-1]=ary[op2-1];
			ary[op2-1]=temp;
		}
	}
	for(i=0;i<n;i++)
	{
		printf("%d\n",ary[i]);
	}
}

 

 

 

 

 

GoToTheNextPart

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章