2020藍橋杯校內模擬賽(1)

A

問題描述
  在計算機存儲中,15.125GB是多少MB?
答案提交
  這是一道結果填空的題,你只需要算出結果後提交即可。本題的結果爲一個整數,在提交答案時只填寫這個整數,填寫多餘的內容將無法得分。

15488

B

問題描述
  1200000有多少個約數(只計算正約數)。
答案提交
  這是一道結果填空的題,你只需要算出結果後提交即可。本題的結果爲一個整數,在提交答案時只填寫這個整數,填寫多餘的內容將無法得分。

96

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>

using namespace std;

const int N = 1200000;
int ans = 0;
int main()
{
	for (int i = 1; i * i <= N; i++)
	{
		if (N % i == 0)//能整除就是約數
		{
			printf("%d %d\n", i, N / i);
			ans += 2;
		}
	}
	cout << ans << endl;
	return 0;
}

C

問題描述
  在1至2019中,有多少個數的數位中包含數字9?
  注意,有的數中的數位中包含多個9,這個數只算一次。例如,1999這個數包含數字9,在計算只是算一個數。
答案提交
  這是一道結果填空的題,你只需要算出結果後提交即可。本題的結果爲一個整數,在提交答案時只填寫這個整數,填寫多餘的內容將無法得分。

544

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

using namespace std;
int ans = 0;
void check(int x)
{
	while (x)
	{
		int t = x % 10;
		if (t == 9)
		{
			ans++;
			return;
		}
		x = x / 10;
	}
	return;
}
int main()
{
	for (int i = 1; i <= 2019; i++)
	{
		check(i);
	}
	cout << ans << endl;
	return 0;
}

D

問題描述
  一棵包含有2019個結點的樹,最多包含多少個葉結點?
答案提交
  這是一道結果填空的題,你只需要算出結果後提交即可。本題的結果爲一個整數,在提交答案時只填寫這個整數,填寫多餘的內容將無法得分。

2018

E

問題描述
  一個正整數如果任何一個數位不大於右邊相鄰的數位,則稱爲一個數位遞增的數,例如1135是一個數位遞增的數,而1024不是一個數位遞增的數。
  給定正整數 n,請問在整數 1 至 n 中有多少個數位遞增的數?
輸入格式
  輸入的第一行包含一個整數 n。
輸出格式
  輸出一行包含一個整數,表示答案。
樣例輸入
30
樣例輸出
26
評測用例規模與約定
  對於 40% 的評測用例,1 <= n <= 1000。
  對於 80% 的評測用例,1 <= n <= 100000。
  對於所有評測用例,1 <= n <= 1000000。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<stack>

using namespace std;

//判斷當前數是否遞增
int work(int x)
{
	int pre = x % 10; // 取最低位
	int p;
	while (x > 0)
	{
		p = x % 10;
		if (p <= pre)
		{
			x = x / 10;
			pre = p;
		}
		else
		{
			return 0;
		}
	}
	return true;
}

int main()
{
	long n;
	cin >> n;
	int ans = 0;

	for (int i = 1; i <= n; i++)
	{
		ans = ans + work(i);
	}
	cout << ans << endl;
	return 0;
}

F

問題描述
  小明對類似於 hello 這種單詞非常感興趣,這種單詞可以正好分爲四段,第一段由一個或多個輔音字母組成,第二段由一個或多個元音字母組成,第三段由一個或多個輔音字母組成,第四段由一個或多個元音字母組成。
  給定一個單詞,請判斷這個單詞是否也是這種單詞,如果是請輸出yes,否則請輸出no。
  元音字母包括 a, e, i, o, u,共五個,其他均爲輔音字母。
輸入格式
  輸入一行,包含一個單詞,單詞中只包含小寫英文字母。
輸出格式
  輸出答案,或者爲yes,或者爲no。
樣例輸入
lanqiao
樣例輸出
yes
樣例輸入
world
樣例輸出
no
評測用例規模與約定
  對於所有評測用例,單詞中的字母個數不超過100。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#include<stack>

using namespace std;

//判斷字符是否爲元音
bool isYy(char str)
{
	if (str == 'a' || str == 'e' || str == 'i' || str == 'o' || str == 'u')
	{
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	string str = "";
	cin >> str;
	
	//進棧操作,從尾到首進行判斷,即 元-輔-元-輔
	stack<char> s;
	for (int i = 0; i < str.length(); i++)
	{
		s.push(str[i]);
	}
	
	char x;
	x = s.top();
	bool preflag = isYy(x);//判斷倒數第一個字符
	bool flag;
	int count = 1;//三條豎線分割4塊

	while (!s.empty())
	{
		x = s.top();
		s.pop();
		flag = isYy(x);//第一次出棧肯定相等
		if (flag == preflag)//所以繼續循環,比較將從第二個開始
		{
			continue;
		}
		else
		{
			count++;
			preflag = flag;//跟隨變化
		}
		

	}
	//cout << count << endl;
	if(count == 4) cout << "yes" << endl;
	else cout << "no" << endl;
	
	return 0;
}

上述題目直接循環字符串也是可以的,棧只是當時的做法

G

問題描述
  在數列 a[1], a[2], …, a[n] 中,如果對於下標 i, j, k 滿足 0<i<j<k<n+1 且 a[i]<a[j]<a[k],則稱 a[i], a[j], a[k] 爲一組遞增三元組,a[j]爲遞增三元組的中心。
  給定一個數列,請問數列中有多少個元素可能是遞增三元組的中心。
輸入格式
  輸入的第一行包含一個整數 n。
  第二行包含 n 個整數 a[1], a[2], …, a[n],相鄰的整數間用空格分隔,表示給定的數列。
輸出格式
  輸出一行包含一個整數,表示答案。
樣例輸入
5
1 2 5 3 5
樣例輸出
2
樣例說明
  a[2] 和 a[4] 可能是三元組的中心。
評測用例規模與約定
  對於 50% 的評測用例,2 <= n <= 100,0 <= 數列中的數 <= 1000。
  對於所有評測用例,2 <= n <= 1000,0 <= 數列中的數 <= 10000。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;
const int N = 10005;

int a[N] , b[N] ,s[N] , n;

int main()
{
    cin >> n;
    for(int i = 0;i < n ;i ++)
    {
        scanf("%d",&a[i]);
    }
    s[0] = a[0] ; b[n-1] = a[n-1];
    for(int i = 1;i <= n ;i ++)
    {
        s[i] = min(s[i-1],a[i]);
    }
    for(int i = n - 2;i >= 0;i --)
    {
        b[i] = max(b[i + 1],a[i]);
    }
    int ans = 0;
    for(int i = 1;i < n - 1;i ++)
    {
        if(s[i - 1] < a[i] && b[i + 1] > a[i])ans++;
    }
    cout << ans << endl;
    return 0;
}

H

問題描述
  小明想知道,滿足以下條件的正整數序列的數量:
  1. 第一項爲 n;
  2. 第二項不超過 n;
  3. 從第三項開始,每一項小於前兩項的差的絕對值。
  請計算,對於給定的 n,有多少種滿足條件的序列。
輸入格式
  輸入一行包含一個整數 n。
輸出格式
  輸出一個整數,表示答案。答案可能很大,請輸出答案除以10000的餘數。
樣例輸入
4
樣例輸出
7
樣例說明
  以下是滿足條件的序列:
  4 1
  4 1 1
  4 1 2
  4 2
  4 2 1
  4 3
  4 4
評測用例規模與約定
  對於 20% 的評測用例,1 <= n <= 5;
  對於 50% 的評測用例,1 <= n <= 10;
  對於 80% 的評測用例,1 <= n <= 100;
  對於所有評測用例,1 <= n <= 1000。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;


int dfs(int pre, int now)
{
	int t = 1;//默認有一個
	for (int i = 1; i < abs(now - pre); i++) //此處判斷條件也可看成出口
	{
		t += dfs(now,i);
	}
	return t;
}

int main()
{
	int n;
	cin >> n;
	int ans = 0;
	for (int i = 1; i <= n; i++)
	{
		ans += dfs(n, i);
	}
	cout << ans << endl;


	return 0;
}

I

問題描述
  小明有一塊空地,他將這塊空地劃分爲 n 行 m 列的小塊,每行和每列的長度都爲 1。
  小明選了其中的一些小塊空地,種上了草,其他小塊仍然保持是空地。
  這些草長得很快,每個月,草都會向外長出一些,如果一個小塊種了草,則它將向自己的上、下、左、右四小塊空地擴展,這四小塊空地都將變爲有草的小塊。
  請告訴小明,k 個月後空地上哪些地方有草。
輸入格式
  輸入的第一行包含兩個整數 n, m。
  接下來 n 行,每行包含 m 個字母,表示初始的空地狀態,字母之間沒有空格。如果爲小數點,表示爲空地,如果字母爲 g,表示種了草。
  接下來包含一個整數 k。
輸出格式
  輸出 n 行,每行包含 m 個字母,表示 k 個月後空地的狀態。如果爲小數點,表示爲空地,如果字母爲 g,表示長了草。
樣例輸入
4 5
.g…

…g…

2
樣例輸出
gggg.
gggg.
ggggg
.ggg.
評測用例規模與約定
  對於 30% 的評測用例,2 <= n, m <= 20。
  對於 70% 的評測用例,2 <= n, m <= 100。
  對於所有評測用例,2 <= n, m <= 1000,1 <= k <= 1000。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;

#define N 25
#define M 105

int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
char maze[N][M];

int n, m;
void dfs(int k,int x, int y)
{
	if (k < 1) return;
	else
	{
		int nx = x;
		int ny = y;
		for (int i = 0; i < 4; i++)
		{
			nx = x + dx[i];
			ny = y + dy[i];
			if ( nx >= 0 && ny >= 0 && nx < n && ny < m)
			{
				maze[nx][ny] = 'g';
				dfs(k - 1, nx, ny);
			}
			nx = x - dx[i];
			ny = y - dy[i];
		}
	}
}

int x[N];
int y[N];
int main()
{

	cin >> n >> m;
	int count = 1;

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> maze[i][j];
			if (maze[i][j] == 'g')
			{
				x[count] = i;
				y[count] = j;
				count++;
			}
		}
	}

#if 1
	int k;
	cin >> k;

	for (int i = 1; i < count; i++)
	{
		dfs(k,x[i],y[i]);
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << maze[i][j];
		}
		cout << endl;
	}
#endif

	return 0;
}

方法二:bfs

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <queue>
#include <vector>

using namespace std;
const int N = 1010;
int n, m, k;
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
char a[N][N];

struct node {
    int x, y, mon;
};
vector<node> grass;

bool inmap(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}

bool check(int mon, int x, int y)
{
    if (inmap(x, y) &&  mon < k)return 1;
    else return 0;
}

void output()
{
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            printf("%c", a[i][j]);
        }
        printf("\n");
    }
}


void bfs(node start)
{
    queue<node> q;
    q.push(start);

    while (!q.empty())
    {
        node now = q.front();

        for (int i = 0; i < 4; i++)
        {
            int nx = now.x + dir[i][0];
            int ny = now.y + dir[i][1];
            if (check(now.mon, nx, ny))
            {
                a[nx][ny] = 'g';
                q.push({ nx , ny ,now.mon + 1 });
            }
        }
        q.pop();
    }
}

void input()
{
    cin >> n >> m;
    string s;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            if (a[i][j] == 'g')
            grass.push_back({ i,j,0 });
        }
    }
    cin >> k;
}


int main()
{
    input();
    for (int i = 0; i < grass.size(); i++)
    {
        bfs(grass[i]);
    }
    output();
    return 0;
}

J

問題描述
  小明要組織一臺晚會,總共準備了 n 個節目。然後晚會的時間有限,他只能最終選擇其中的 m 個節目。
  這 n 個節目是按照小明設想的順序給定的,順序不能改變。
  小明發現,觀衆對於晚上的喜歡程度與前幾個節目的好看程度有非常大的關係,他希望選出的第一個節目儘可能好看,在此前提下希望第二個節目儘可能好看,依次類推。
  小明給每個節目定義了一個好看值,請你幫助小明選擇出 m 個節目,滿足他的要求。
輸入格式
  輸入的第一行包含兩個整數 n, m ,表示節目的數量和要選擇的數量。
  第二行包含 n 個整數,依次爲每個節目的好看值。
輸出格式
  輸出一行包含 m 個整數,爲選出的節目的好看值。
樣例輸入
5 3
3 1 2 5 4
樣例輸出
3 5 4
樣例說明
  選擇了第1, 4, 5個節目。
評測用例規模與約定
  對於 30% 的評測用例,1 <= n <= 20;
  對於 60% 的評測用例,1 <= n <= 100;
  對於所有評測用例,1 <= n <= 100000,0 <= 節目的好看值 <= 100000。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;

#define N 100005
int x[N];
int mxx[N];//存儲較大值
int main()
{
	int n, m;
	cin >> n >> m;

	for (int i = 0; i < n; i++)
	{
		cin >> x[i];
	}

	//挑選出前 m 項的較大值
	for (int i = 0; i < m; i++)
	{
		int ma = x[i];
		int k = i;
		for (int j = 0; j < n; j++)
		{
			if (x[j] > ma)
			{
				ma = x[j];
				k = j;
			}
		}
		mxx[k] = ma;
		x[k] = 0;
	}

	for (int i = 0; i < n; i++)
	{
		if (mxx[i])
		{
			cout << mxx[i] << " ";
		}
	}

	return 0;
}

方法二:容器

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>

using namespace std;

int n , m;

struct node{
    int deep;
    int id;
};

bool cmp1(node a,node b)
{
    return a.deep > b.deep;
}

bool cmp2(node a,node b)
{
    return a.id < b.id;
}

vector<node> ans;
int main()
{
    cin >> n >> m;
    
    for(int i = 0;i < n ;i ++)
    {
        node t;
        cin >> t.deep;
        t.id = i;
        ans.push_back(t);
    }
    sort(ans.begin(),ans.end(),cmp1);
    sort(ans.begin(),ans.begin() + m,cmp2);
    
    for(int i = 0;i < m ;i ++)
    {
        cout << ans[i].deep << " ";
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章