1.组队
题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
作为篮球队教练,你需要从以下名单中选出
1 号位至 5 号位各一名球员,组成球队的首发阵容。
每位球员担任 1 号位至 5 号位时的评分如下表所示。请你计算首发阵容
1 号位至 5 号位的评分之和最大可能是多少?
点击查看代码
#include <iostream>
using namespace std;
int main()
{
cout<<99+97+99+98+97;
return 0;
}
思路:观察图表,易找出最大且不同人的五个数,相加即可。
2.年号字串
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
小明用字母
A 对应数字 1B 对应
2,以此类推,用 Z 对应
26。对于 27 以上的数字,小明用两位或更长位的字符串来对应,例如
AA 对应 27
AB 对应 28
AZ 对应 52,
LQ 对应 329。
请问 2019 对应的字符串是什么?
点击查看代码
#include <iostream>
using namespace std;
int main()
{
cout<<"BYQ";
return 0;
}
思路:数学计算,除26算出77余17,77=26+26+25,判断出有BY,17为Q所在值。
3.数列求值
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
给定数列
1,1,1,3,5,9,17,⋯,从第
4 项开始,每项都是前 3 项的和。
求第 20190324 项的最后 4 位数字。
点击查看代码
#include <iostream>
using namespace std;
int main(){
int a=5,b=9,c=17,t;
for(int i=8;i<=20190324;i++){
t=(a+b+c)%10000;
a=b;
b=c;
c=t;
}
cout << t << endl;
return 0;
}
思路:枚举5,6,7项,用四个变量进行迭代计算,最后结果要最后四位数字,所以取模即可。
4.数的分解
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
把 2019 分解成 3 个各不相同的正整数之和,并且要求每个正整数都不包含数字
2 和 4,一共有多少种不同的分解方法?
注意交换
3 个整数的顺序被视为同一种方法,例如 1000+1001+18 和 1001+1000+18 被视为同一种。
点击查看代码
#include <iostream>
using namespace std;
int check(int a)
{
int b[4];
int i=0;
while(a)
{
b[i]=a%10;
a/=10;
i++;
}
for(int j=0;j<i;i++)
{
if(b[j]==2||b[j]==4) return false;
}
return true;
}
int main()
{
int ans=0;
for(int i=1;i<2019;i++)
{
for(int o=i+1;o<2019;o++)
{
for(int b=o+1;b<2019;b++)
{
if(check(i)&&check(o)&&check(b))
{
if(i+o+b==2019) ans++;
}
}
}
}
cout<<ans;
return 0;
}
思路:将1到2019每个不包含2和4的数字相加判断,过程有点繁琐,一定会超时,但是此题是填空题,所以本地运行后得到答案提交即可。
5.迷宫
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。
下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。
010000
000100
001001
110000
迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。
对于上面的迷宫,从入口开始,可以按 DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中
D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。
请注意在字典序中 D<L<R<U。
点击查看代码
#include <iostream>
#include<queue>
#include<map>
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
char maze[30][50] = { 0 };
char visit[30][50];
typedef struct ds {
int row;
int col;
char director;
}ds;
queue<ds> bfs_que;
int next_dir[4][2] = {
{1,0},{0,-1},{-1,0},{0,1}
};
char dir[4] = { 'D','L','U','R' };
map<char, int> dir_index;
bool is_ok(int row, int col)
{
return (row >= 0 && row <= 30 && col >= 0 && col <= 50 && visit[row][col] == char(0) && maze[row][col] == '0') ? true : false;
}
void bfs()
{
ds start = { 0,0,'S' };
bfs_que.push(start);
ds now;
now = bfs_que.front();
visit[now.row][now.col] = 'S';
bfs_que.pop();
while (!(now.row == 29 && now.col == 49)) {
for (int i = 0; i < 4; i++)
{
if (is_ok(now.row + next_dir[i][0], now.col + next_dir[i][1]))
{
ds mid = { now.row + next_dir[i][0],now.col + next_dir[i][1],dir[i] };
visit[now.row + next_dir[i][0]][ now.col + next_dir[i][1]] = now.director;
bfs_que.push(mid);
}
}
if (!(now.row == 29 && now.col == 49))
{
now = bfs_que.front();
bfs_que.pop();
}
else break;
}
}
int main()
{
fstream in;
in.open("/Users/25811/Desktop/1.txt");
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 50; j++)
{
in >> maze[i][j];
}
}
bfs();
dir_index['D'] = 0;
dir_index['L'] = 1;
dir_index['R'] = 2;
dir_index['U'] = 3;
string ans = "";
int row = 29, col = 49;
while (!(row == 0 && col == 0))
{
ans = ans + visit[row][col];
char dir = visit[row][col];
row = row - next_dir[dir_index[dir]][0];
col = col - next_dir[dir_index[dir]][1];
}
reverse(ans.begin(), ans.end());
cout << ans;
return 0;
}
思路:广搜加回溯,定义两个数组分别存储和存储是否到达过,结构体方便计算方向和当前位置,再用二维数组存储四个方向,设置函数判断方向是否能走,最后找到后用回溯算法,将路径反向回溯存入ans,再用reverse将ans翻转即可得到答案。
6.特别数的和
题目描述
小明对数位中含有 2、0、1、9 的数字很感兴趣(不包括前导 0),在 1 到 40 中这样的数包括 1、2、9、10 至 32、39 和 40,共 28 个,他们的和是 574。
请问,在 1 到 n 中,所有这样的数的和是多少?
输入格式:
输入一行包含两个整数
n(1≤n≤10 4 )。
输出描述
输出一行,包含一个整数,表示满足条件的数的和。
点击查看代码
#include <iostream>
using namespace std;
bool check(int x)
{
int a[5], i = 0;
while (x)
{
a[i] = x % 10;
x /= 10;
if (a[i] == 2 || a[i] == 1 || a[i] == 0 || a[i] == 9) return true;
i++;
}
return false;
}
int main()
{
int n, ans = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
if (check(i)) ans += i;
}
cout << ans;
return 0;
}
思路:
遍历一到四十的数字,然后设置函数判断是否有1,0,2,9,返回true的数相加。
浙公网安备 33010602011771号