蓝桥杯集锦

基础练习  特殊回文数 

网址:http://lx.lanqiao.org/problem.page?gpid=T48

#include <stdlib.h>
#include <stdio.h>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
  int n;
  int x=0;
  int a[110];
  scanf("%d",&n);
  for(int i=0;i<=9;i++)  //注意是从0开始,开始从1开始,WR了好多次
     for(int j=10;j<=99;j++)
	  if((j/10+j%10)*2+i==n)
	   a[x++]=(j*10+i)*100+j%10*10+j/10 ;
  for(int i=100;i<=999;i++)
    if((i/100+i%100/10+i%10)*2==n)
    a[x++]=i*1000+i%10*100+i%100/10*10+i/100 ;
  sort(a,a+x); 
  for(int i=0;i<x;i++)
   cout<<a[i]<<endl;
  return 0;
}


杨辉三角:http://lx.lanqiao.org/problem.page?gpid=T10

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
  int a[35][35],n;
  memset(a,0,sizeof(a));
  a[0][1]=1;
  scanf("%d",&n);
  for(int i=1;i<=n;i++) 
  {
  for(int j=1;j<=i;j++)
    {
      a[i][j]=a[i-1][j-1]+a[i-1][j];
      cout<<a[i][j]<<' ';
    } 
 cout<<endl;
  }
  return 0;
}


查找整数  网址:http://lx.lanqiao.org/problem.page?gpid=T9

#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
        int x,n;
		scanf("%d",&n);
        vector<int> v;//要查找的元素,类型要与vector<>类型一致
        for(int i=0;i<n;i++)
          {
          	
          	scanf("%d",&x);
            v.push_back(x);
		  }
		 scanf("%d",&x); 
       vector<int>::iterator iter=std::find(v.begin(),v.end(),x);//返回的是一个迭代器指针
        if(iter==v.end())
            cout<<-1;
        else               //注意迭代器指针输出元素的方式和distance用法
            cout<< distance(v.begin(), iter)+1 ;
        return 0;
}


  基础练习 字母图形  :http://lx.lanqiao.org/problem.page?gpid=T7

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
string str="";
for(int i=0;i<n;i++)
   str+=('A'+i);
cout<<str<<endl;
string temp ;
for(int i=1;i<m;i++)
 {
  string str1(str,0,n-1);  //拷贝部分字符串
  temp="";
  temp=temp+char('A'+i)+str1;//注意
  cout<<temp<<endl;
  str=temp;
 } 
return 0;
}

开始的时候理解错题意了,WR了好几次

 



基础练习 01字串(这道题挺好的知识点)  

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
     for(int i=0;i<32;i++){
     char s[10];  
     itoa(i, s, 2);   //转换成字符串,进制基数为2,i转化为   注意是整型和字符数组之间的转化 
     string str(s);    //转化为string类型 
     int number = atoi(str.c_str());
     printf("%05d
",number);  //输出控制格式0代表空位补零,5代表限制为5位,如果小于五位,通过补零形式满足,超过五位则失去作用 
	}
return 0;
}


另解:开始的时候不知道字符串也能填充(填充其他字符现在还不清楚,但是填充0为如下)

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
     for(int i=0;i<32;i++){
     char s[10];  
     itoa(i, s, 2);   //转换成字符串,进制基数为2,i转化为   注意是整型和字符数组之间的转化 
     printf("%05s
",s);   
	}
	return 0;
} 





  算法训练 Anagrams问题 (大小写转化)

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
char ch1[100],ch2[100];
cin>>ch1>>ch2;
int x;
if((x=strlen(ch1))!=strlen(ch2))
cout<<"N";
strlwr(ch1);//将大写转化为小写 网址:http://blog.csdn.net/u014665013/article/details/37994597 
strlwr(ch2);
int a[30]={0},b[30]={0};
for(int i=0;i<x;i++)
 {
 a[ch1[i]-'a']++;
 b[ch2[i]-'a']++;
 } 
 int mark=1;
for(int i=0;i<26;i++)
 if(a[i]!=b[i])
 {
 cout<<"N";
 mark=0;
 break;
}
if(mark)
cout<<"Y";

return 0;
}


 算法训练 出现次数最多的整数

(我也是醉了,好像还有n=0这样的测试数据,这还WR,好好读题)

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
     int n;
     cin>>n;
     int a,b,mark,count=1,temp=1;
     int x=n;
     if(x>0)
	 cin>>a;
	 n--;
	 mark=a;
     while((n--)>0){
     cin>>b;
	 if(a==b){
	 	temp++;
	 	if(temp>count)
	 	{
		 mark=b;
		 count=temp;
	 	}  
	 }
	 else
    	temp=1;
      a=b;	
     }
     if(x>0)
     cout<<mark;
	return 0;
} 

 算法训练 字串统计  (含字符串的部分截取)

  网址:http://lx.lanqiao.org/problem.page?gpid=T219

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
     int n;
     cin>>n;
     string str,mark="";
     cin>>str;
     string temp[65];
     int len=str.length(),markcishu=0,tempcishu;
     for(int i=n;i<=len;i++)
     {
     	int j=0;
      for(j=0;j+i<=len;j++)//截取字串 j为起始位置, 
       {
        temp[j] = str.substr(j,i);
       // cout<<"temp["<<j<<"]="<<temp[j]<<endl;
	   }
      for(int x=0;x<j&&temp[x]!="";x++)//查找长度为i的最多的串 
      {
      	tempcishu=1;
       for(int w=x;w<j;w++)
         if(temp[x]==temp[w]) {
		    tempcishu++;
			temp[x]=="";
		  }
	  if(tempcishu>markcishu||(temp[x].length()>mark.length()&&tempcishu==markcishu)){//注意条件
	    	markcishu=tempcishu;
			mark=temp[x]; 
		} 
	
      }   
	 }
	 cout<<mark; 
return 0;
} 


算法训练 矩阵乘法

 网址:http://lx.lanqiao.org/problem.page?gpid=T218

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
     int n,m,s;
     cin>>m>>s>>n;
     int a[205][205], b[205][205],temp=0;
     for(int i=1;i<=m;i++)
       for(int j=1;j<=s;j++)
       scanf("%d",&a[i][j]);
     for(int i=1;i<=s;i++)
       for(int j=1;j<=n;j++)
       scanf("%d",&b[i][j]);
    for(int i=1;i<=m;i++)
    {
     for(int j=1;j<=n;j++)
      {
      for(int x=1;x<=s;x++)
        temp=temp+a[i][x]*b[x][j];
      printf("%d ",temp);
      temp=0;
      } 
       printf("
");
     }   
return 0;
} 


算法训练 大小写转换 网址:http://lx.lanqiao.org/problem.page?gpid=T216

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
 string str;
 //cout<<char(32+'A');//<<' '<<'b'-'B'<<endl;
 cin>>str;
  int len = str.length();
  for(int i=0;i<len;i++){
  	if(str[i]<='Z'&&str[i]>='A')
  	  str[i]=char(str[i]+32);
  	else
  	 str[i]=str[i]-32;
  }
  cout<<str; 
return 0;
} 


 算法训练 动态数组使用  网址:http://lx.lanqiao.org/problem.page?gpid=T205

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
	int n,sum=0;
	scanf("%d",&n);
   int * array=(int*)calloc(n,sizeof(int));
   for(int i=0;i<n;i++)
    {
     cin>>array[i];
     sum=sum+array[i];
	}
	cout<<sum<<' '<<sum/n;
return 0;
} 



算法训练 最小乘积(基本型)   网址:http://lx.lanqiao.org/problem.page?gpid=T133

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
bool cmp(int a,int b){
return a>b;
} 

int main()
{
	int n; 
	scanf("%d",&n);
   for(int i=0;i<n;i++)  {
     int x,sum=0;
     scanf("%d",&x);
     int a[1005],b[1005];
	 //int * a=(int*)calloc(n,sizeof(int));
     //int * b=(int*)calloc(n,sizeof(int));
     for(int j=0;j<x;j++)
       cin>>a[j];
     for(int j=0;j<x;j++)
       cin>>b[j];
     sort(a,a+x);
     sort(b,b+x,cmp);
     
     for(int j=0;j<x;j++)
       sum+=(a[j]*b[j]);
	   printf("%d
",sum);	
    }
return 0;
}


算法训练 Torry的困惑(基本型)    网址:http://lx.lanqiao.org/problem.page?gpid=T129


//前n项素数求积对50000取模 
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
#define N 1300000
bool isprm[N];
void isprime()
{
    int i,j,k=0;
    int s,e=sqrt( double(N) )+1;    	//sqrt是对于double数开平方

    memset(isprm,1,sizeof(isprm));
                                                  //prm[k++]=2;
    isprm[0] = isprm[1] = 0;
    for(i=4 ;i < N; i=2+i)
		isprm[i]=0;

    for(i=3;i<e;i=2+i)
        if(isprm[i])
            for(s=i*2,j=i*i;j<N;j=j+s)
                isprm[j]=0;                        //因为j是奇数,所以+奇数后是偶数,不必处理
}

int main()
{
 int n,sum=1;
 cin>>n;
 isprime();
 int j=0;
 for(int i=0;j<n;i++)
   if(isprm[i]==true){
     sum=sum*i%50000;
     j++;
   }
 cout<<sum;
return 0;
}


算法训练 关联矩阵 网址:http://lx.lanqiao.org/problem.page?gpid=T110

//前n项素数求积对50000取模 
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main (){
 int m,n,a[105][1005]={0};
 cin>>m>>n;
 int x,y;
 for(int i=1;i<=n;i++){
 	scanf("%d%d",&x,&y);
 	a[y][i]=-1;
 	a[x][i]=1;
 }
  for(int i=1;i<=m;i++)
   {
   for(int j=1;j<=n;j++)
      cout<<a[i][j]<<' ';
      cout<<endl;
   } 
return 0;
}


算法训练 区间k大数查询

 网址:http://lx.lanqiao.org/problem.page?gpid=T11

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
   int n,a[1005],x,temp[1005],l,r,k;
   scanf("%d",&n);
   for(int i=1;i<=n;i++)
   	scanf("%d",&a[i]);
    scanf("%d",&x);
    for(int i=1;i<=x;i++){
    	int count=0;
      scanf("%d%d%d",&l,&r,&k);	
      for(int j=l;j<=r;j++)
       {
       temp[count++]=a[j];
      // cout<<a[j]<<' ';
	    ;
	   }
	  sort(temp,temp+count);
	  //cout<<endl<<count<<temp[0]<<' '<<temp[1]<<' '<<temp[2]<<' '<<temp[3]<<' '<<temp[4]<<endl; 
	  cout<<temp[count-k]<<endl;		
    }
return 0;
}


算法提高 6-17复数四则运算(写这样简单但是需要先总结对公式) 

 网址 :http://lx.lanqiao.org/problem.page?gpid=T255

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
double x1,x2,y1,y2,y3,x3;
char ch;
cin>>x1>>y1>>ch>>x2>>y2;
if(ch=='+')
  x3=x1+x2,y3=y1+y2;
else if(ch=='-')
x3=x1-x2,y3=y1-y2;
else if(ch=='*')
x3=x1*x2-y1*y2,y3=x1*y2+y1*x2;
else
  if(ch=='/')
   if(x2*x2+y2*y2==0)
   {
   cout<<"error";
    return 0;
   }
   else
    x3=(x1*x2+y1*y2)/(x2*x2+y2*y2),y3=(y1*x2-x1*y2)/(x2*x2+y2*y2);
else
 {
cout<<"error";
return 0;
 }
if(x3==0&&y3==0){
cout<<0;
return 0;
}
if(x3!=0)
  cout<<x3;
if(y3>0)
cout<<'+'<<y3<<'i';
else if(y3<0)
cout<<y3<<'i'; 
//cout<<endl<<x2*x2+y2*y2<<' '<<(y1*x2-x1*y2);
return 0;
}


算法提高 约数个数(不是完全循环,还是有点小技巧的)

 网址:http://lx.lanqiao.org/problem.page?gpid=T209

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int count=0;
	for(int i=1;i<=sqrt(double(n));i++){
		if(n%i==0)
		count++; 
	} 
	if(sqrt(double(n))-int(sqrt(double(n)))==0)
	cout<<2*count-1;
	else
	cout<<2*count;
return 0;
}


历届试题 分糖果  网址:   http://lx.lanqiao.org/problem.page?gpid=T124

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main (){
   int n,a[105],b[105];
   cin>>n;
   for(int i=0;i<n;i++){
   	scanf("%d",&a[i]) ;
   }
   int count = 0;
   char mark = 'b';
   int x=3;
   while(1){
   if(mark=='a')
   {
   	int temp=b[0],i;
   	for(i=1;i<n;i++ ) 
      if(b[i]!=temp){
       break;
	   } 
    if(i==n)
       break;	 
   	a[0]=(b[n-1]+b[0])/2;
   	if(a[0]%2==1){
   	     a[0]++;
   	     count++;
		}
  	for(i=1;i<n;i++){
      a[i]=(b[i-1]+b[i])/2;
   	   if(a[i]%2==1){
   	      a[i]++;
   	  count++;
      }
	 }
   	mark='b';
    }	
   else{
   int temp=a[0],i;
   	for(i=1;i<n;i++ )
      if(a[i]!=temp){
       break;
       } 
    if(i==n)
       break;   
	b[0]=(a[n-1]+a[0])/2;
	if(b[0]%2==1){
   	     b[0]++;
   	     count++;
		}
  	for(i=1;i<n;i++){
   	  b[i]=(a[i-1]+a[i])/2;
   	  if(b[i]%2==1){
   	     b[i]++;
   	     count++;
		}
	  }
   mark='a';
   }
}
 cout<<count;
return 0;
}






历届试题 兰顿蚂蚁 网址:http://lx.lanqiao.org/problem.page?gpid=T125

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cstring>
#include <algorithm>  
#include <iostream>
using namespace std;
int main (){
   int n,m;
   bool a[105][105];
   cin>>m>>n;
   for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
   	  scanf("%d",&a[i][j]) ;
   int row,list,find;
   scanf("%d%d",&row,&list) ;
   char ch;
   cin>>ch>>find;
   for(int i=1;i<=find;i++){
   	if(a[row][list]==1){
   		a[row][list]=0;
   		if(ch=='U'){  ch='R'; list++;}
   		else if(ch=='R'){  ch='D'; row++; }
   	     	else if(ch=='D'){  ch='L';list-- ;}
   		          else  {  ch='U';row--;}
   }
   	else{
   		a[row][list]=1;
   		if(ch=='U'){  ch='L';list-- ;} 
   		else if(ch=='R'){  ch='U';row--;} 
   	     	else if(ch=='D'){  ch='R'; list++;}
   		          else {  ch='D'; row++; }
   	}
   }
   cout<<row<<' '<<list;
return 0;
}






















原文地址:https://www.cnblogs.com/zswbky/p/5432024.html