第十屆藍橋杯省賽A組

試題 A: 平方和
【問題描述】 :
小明對數位中含有 2、0、1、9 的數字很感興趣,在 1 到 40 中這樣的數包 括 1、2、9、10 至 32、39 和 40,共 28 個,他們的和是 574,平方和是 14362。 注意,平方和是指將每個數分別平方後求和。
請問,在 1 到 2019 中,所有這樣的數的平方和是多少?

#include"stdio.h"
int fun(long long value) 
{     
   long long  x=0;
   x=value;
	while(x!=0)
	{   int stemp;
		 stemp =x%10;
		 x=x/10;
		if(stemp==2||stemp==0||stemp==1||stemp==9) 
		{
			
			return value*value; 
		}
	
	 } 
	 
	return 0 ;
	
}
int main()
{
	int i;
	long long sum=0;
	for (i=1;i<=2019;i++)
	{
		if (fun(i))
		{
		   	
			sum=sum+fun(i);
		}
	}
   printf("%lld",sum);
	return 0;
 } 

答案:2658417853
試題 B: 數列求值
【問題描述】
給定數列 1, 1, 1, 3, 5, 9, 17, …,從第 4 項開始,每項都是前 3 項的和。求 第 20190324 項的最後 4 位數字。
只要求最後四位數字的問題,相當於求答案對10000取模

#include"stdio.h"
int main()
{
	long long int i;
	int a=1,b=1,c=1,d=3;
	for (i=5;i<=20190324;i++)//從第5項開始平均,滑動。
	{
	a=b; 
	b=c;
	c=d;
	d=a+b+c;
	if(d>=10000) 
	{
		d=d%10000;//取最後四位 
	}
	} 
	printf("%lld ",d);
	return 0;
}

答案:4659
試題 C: 最大降雨量
【問題描述】
由於沙之國長年乾旱,法師小明準備施展自己的一個神祕法術來求雨。 這個法術需要用到他手中的 49 張法術符,上面分別寫着 1 至 49 這 49 個 數字。法術一共持續 7 周,每天小明都要使用一張法術符,法術符不能重複使 用。
每週,小明施展法術產生的能量爲這周 7 張法術符上數字的中位數。法術 施展完 7 周後,求雨將獲得成功,降雨量爲 7 周能量的中位數。
由於乾旱太久,小明希望這次求雨的降雨量儘可能大,請大最大值是多少?
在這裏插入圖片描述

在這裏插入圖片描述
採用廣度優先搜索,先測試數據較少:

在這裏插入圖片描述

//https://www.cnblogs.com/yuanhang110/p/10588575.html
#include<iostream> 
#include<string>
#include<queue>
using namespace std;
string ss[35];//儲存地圖的字符串 
int maze[35][55];//地圖35行,55列 
int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};// 向下,向左,向右,向上。 
char letter[4]={'D','L','R','U'} ;//向下,向左,向右,向上 
int cnt =10000;//記錄走出迷宮所需要的步數,初值設爲10000 
bool vis[35][55];
struct node
{
 int x;
 int y;
 string s;//儲存路徑字符串 
 int step;//儲存到達5步數 
 node (int xx,int yy,string ss,int st)//構造函數  
 {
 	x=xx;
 	y=yy;
 	s=ss;
 	step=st;
 }
};
bool in (int x,int y) //是否越界 
{
	if(x<4&&x>=0&&y<6&&y>=0) //4行,6列  
	{	
		return true;
	 } 
	return false;
}
void bfs(int x,int y,string s,int step)
{
	queue<node> q;//創建隊列 q 
	q.push(node(x,y,s,step));//初始座標入隊,賦給當前節點 
	while(!q.empty())//隊列爲空時,嘗試了所有的結果 
    {
      node now =q.front() ;//更新當前節點的值 
      q.pop(); //將頭節點出隊 
      vis[now.x][now.y]=true;//true 代表已經訪問過 
	  if(now.x==3&&now.y==5)//到達出口 
	  {
	  	if(now.step<cnt )
	  	{
	  		cnt =now.step;//記錄最短步數  
	  		cout<<now.step<<":"<<now.s<<endl;//輸出步數,路徑。 
		  }
		  continue;//進行下一次while循環,理論上可以break.這是爲了保險探索了所有可能情況 
	   } 
		for (int i=0;i<4;i++) //下左右上四個方向進行探索  
		{
			int tx=now.x+dir[i][0];//x座標計算  
			int ty=now.y+dir[i][1];//y座標計算 
			if(maze[tx][ty]!=1&&!vis[tx][ty]&&in(tx,ty))//如果座標在邊界內,有路,未曾訪問過 
			{
				
				q.push(node(tx,ty,now.s+letter[i],now.step+1));//將這個座標入隊列 ,記錄路徑,和步數。   
				
			 } 
		 } 
	}		
 } 
 
 int main()
 {
 	for (int i=0;i<4;i++)
 	{
 		cin>>ss[i];
	 }
 	for(int i=0;i<4;i++)
     {
	   for(int j=0;j<6;j++)
	   {
	     maze[i][j]=(ss[i][j]-'0');
		} 
	 }
	 int step=0;
	 string s=" ";
	 bfs(0,0,s,step);
       return 0;
   } 

在這裏插入圖片描述

//https://www.cnblogs.com/yuanhang110/p/10588575.html
#include<iostream> 
#include<string>
#include<queue>
using namespace std;
string ss[35];//儲存地圖的字符串 
int maze[35][55];//地圖35行,55列 
int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};// 向下,向左,向右,向上。 
char letter[4]={'D','L','R','U'} ;//向下,向左,向右,向上 
int cnt =10000;//記錄走出迷宮所需要的步數,初值設爲10000 
bool vis[35][55];
struct node
{
 int x;
 int y;
 string s;//儲存路徑字符串 
 int step;//儲存到達5步數 
 node (int xx,int yy,string ss,int st)//構造函數  
 {
 	x=xx;
 	y=yy;
 	s=ss;
 	step=st;
 }
};
bool in (int x,int y) //是否越界 
{
	if(x<30&&x>=0&&y<50&&y>=0) //30行,50列  
	{	
		return true;
	 } 
	return false;
}
void bfs(int x,int y,string s,int step)
{
	queue<node> q;//創建隊列 q 
	q.push(node(x,y,s,step));//初始座標入隊,賦給當前節點 
	while(!q.empty())//隊列爲空時,嘗試了所有的結果 
    {
      node now =q.front() ;//更新當前節點的值 
      q.pop(); //將頭節點出隊 
      vis[now.x][now.y]=true;//true 代表已經訪問過 
	  if(now.x==29&&now.y==49)//到達出口 
	  {
	  	if(now.step<cnt )
	  	{
	  		cnt =now.step;//記錄最短步數  
	  		cout<<now.step<<":"<<now.s<<endl;//輸出步數,路徑。 
		  }
		  continue;//進行下一次while循環,理論上可以break.這是爲了保險探索了所有可能情況 
	   } 
		for (int i=0;i<4;i++) //下左右上四個方向進行探索  
		{
			int tx=now.x+dir[i][0];//x座標計算  
			int ty=now.y+dir[i][1];//y座標計算 
			if(maze[tx][ty]!=1&&!vis[tx][ty]&&in(tx,ty))//如果座標在邊界內,有路,未曾訪問過 
			{
				
				q.push(node(tx,ty,now.s+letter[i],now.step+1));//將這個座標入隊列 ,記錄路徑,和步數。   
				
			 } 
		 } 
	}		
 } 
 
 int main()
 {
 	for (int i=0;i<30;i++)
 	{
 		cin>>ss[i];   //30行 
	 }
 	for(int i=0;i<30;i++)
     {
	   for(int j=0;j<50;j++)
	   {
	     maze[i][j]=(ss[i][j]-'0');
		} 
	 }
	 int step=0;
	 string s=" ";
	 bfs(0,0,s,step);
       return 0;
   } 

在這裏插入圖片描述

在這裏插入圖片描述
完全二叉樹 :每一層,子節點是父節點的二倍。不用用數的數據結構也可以求出來。

#include"iostream"
#include"algorithm"
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f;
int main()
{
	int n;//個數  
	cin>>n;
    long long  min=-INF,ans=0;
    int i=0,length=1,depth=1;
    for(i=0;i<n;)
    {
    	long long  sum=0;
    	for (int j=0;j<length&&i<n;j++,i++)//逐行求和 
    	{
    		int x;
    		cin>>x;
    		sum+=x;
		}
		if(sum>min)
		{
			min=sum;
			ans=depth;
		 } 
		 depth++;
		 length*=2;
	}
	cout<<ans<<endl;
 } 

在這裏插入圖片描述

在這裏插入圖片描述
用店家標識,代表“店家優先級數組”的下標。
在這裏插入圖片描述

#include<iostream>
#include"algorithm"//sort需要  
#include"string.h"//memset 需要  
#define maxn 100000          
using namespace std;
int b[maxn]={0};    //b存放每個店的優先級。 
int book[maxn]={0};//標記外賣店是否在優先緩存中。  
int book2[maxn]={0};//判斷某個時刻i,外賣店是否有訂單,如果有訂單則標記1,沒有則標記爲0 
struct waimai
{
	int ts;  //時刻 
	int id; //店標識 
 }a[maxn];
  
 
bool cmp(waimai a,waimai b)  //sort排序所需要的 cmp,是函數名可以任意起 
{
	return a.ts<b.ts;       //按時刻從到大排序,從大到小改爲大於號   
}
int main()
{
	int n,m,t;
	int count=0;
	scanf("%d%d%d",&n,&m,&t);  //輸入,店家數,訂單數,時刻 
    for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&a[i].ts,&a[i].id);
	}
	sort(a+1,a+m+1,cmp);      //所有訂單按時刻從小到大排序 
//	for(int i=1;i<=m;i++)
//	{
//		printf("%d %d\n",a[i].ts,a[i].id);
//	} 
    for (int i=1;i<=t;i++)//i是時刻的索引值,這個for循環求時刻i時,各個店家的優先級。 
	{   count = 0; 
		int x;
		for(int j=1;j<=m;j++)//時刻i,有訂單的店家更新優先級,並且對其進行標記。 
		{
			if(i==a[j].ts) //是i時刻的訂單 
			{
			  x=a[j].id;   //取出是家店x的訂單 
			  b[x]+=2;     //這家店x有一個訂單,店x的優先級加2       
			  book2[x]=1;  // 標記i時刻店家x有訂單 
			}
			
		}
		for(int l=1;l<=n;l++)//對於所有的店家,如果時刻i沒有訂單,則優先級減1,或者保持零 
		{
			if(book2[l]==0) //店家l沒有訂單 
			{
			
					if(b[l]>0)
					 b[l]--;//店家l的優先級減去一 
					 else
					 b[l]=0;
				
			 } 
		}
		for(int l=1;l<=n;l++) //計算i時刻優先級大於5的店家數量 
		{
		 if(b[l]>5)//i時刻,店家l優先級大於5 
		 {
			 	book[l]=1;//店家l,標記在優先內存中 
				count++;   
		    }
		  
		} 
	 
		    memset(book2,0,sizeof(book2));//將所有清零爲,即認爲全部沒有訂單。 
		                       		
		}
		printf("%d\n",count);
		return 0;
 } 

測試數據1

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

#include"iostream"
using namespace std;
#define N 1000005
int mark[N]={0};//數是否出現,的標記數組。 
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int m;
		cin>>m;
		while(mark[m]==1)//數已經出現  
		   m++;//數加一 
		mark[m]=1;//跳出循環後已經不會重複 
		cout<<m;		
	 } 
	
	return 0;
} 

在這裏插入圖片描述

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