2021-3-22【天梯赛选拔】

}

在这里插入图片描述

#include <stdio.h>

int main()

{

printf(“______ _ _ _ _ _ _ \n”);

printf(“| ___ \ | | | | | | | | | | |\n”);

printf(“| |/ /__ | | | | | | ___ ___ __| | ___ ___| | |\n”);

printf(“| // _ \/ _ | | '_ \\| |/ _ \\ / _ \\ / _ | / __/ _ \ | |\n”);

printf(“| |\ \ __/ (| | | |) | | () | () | (| | | (| __/ | |\n”);

printf(“\| \\|\,| |.__/||\/ \/ \,| \\_|||\n”);

}

在这里插入图片描述

#include<stdio.h>

int flag=0;

void fun(int gold,int npc_1,int npc_2){

if(npc_1>0){

fun(gold-1,npc_1-1,npc_2);

}

if(npc_2>0){

fun(gold*2,npc_1,npc_2-1);

}

if(gold1&&npc_11&&npc_2==0){

flag++;

return;

}

}

int main(){

int npc_1,npc_2;

scanf(“%d %d”,&npc_1,&npc_2);

fun(2,npc_1,npc_2);

printf(“%d”,flag);

return 0;

}

在这里插入图片描述

#include<bits/stdc++.h>

using namespace std;

int main(){

int n;

cin>>n;

while (n–){

vector v;

int temp;

cin>>temp;

v.push_back(temp);

while(cin.get()!=‘\n’){

cin>>temp;

v.push_back(temp);

}

sort(v.begin(),v.end());

cout<<v.back()<< ’ ’ <<v.front()<<endl;

}

return 0;

}

在这里插入图片描述

#include<stdio.h>

#include<string.h>

int main(){

int n,len;

char s[1001];

scanf(“%d”,&n);

getchar();

gets(s);

len=strlen(s);

n=n%len;

for(int i=len-n;i<len;i++){

printf(“%c”,s[i]);

}

for(int i=0;i<len-n;i++){

printf(“%c”,s[i]);

}

return 0;

}

在这里插入图片描述

#include<stdio.h>

int main(){

int n;

scanf(“%d”,&n);

printf(“%.1lf%%”,(5000.0/n)*100.0);

}

在这里插入图片描述

#include<stdio.h>

int main(){

int num[9][9]={0};

int flag=1,sum=0;

int h,l;

for(int i=0;i<9;i++){

for(int j=0;j<9;j++){

scanf(“%d”,&num[i][j]);

if(num[i][j]==0){

h=i;

l=j;

}

}

}

for(int i=0;i<9;i++){

for(int j=1;j<9;j++){

if(num[h][j]==num[h][i]&&i!=j){

flag=0;

}

}

}

for(int i=0;i<9;i++){

for(int j=1;j<9;j++){

if(num[j][l]==num[i][l]&&i!=j){

flag=0;

}

}

}

for(int i=0;i<9;i++){

for(int j=1;j<9;j++){

for(int k=0;k<9;k++){

if(num[i][j]==num[i][k]&&j!=k){

flag=0;

}

}

}

}

for(int i=0;i<9;i++){

sum=sum+num[i][l];

}

if(flag==1){

printf(“%d”,45-sum);

}else{

printf(“NO”);

}

return 0;

}

在这里插入图片描述

本题要求找到对于每一个数组值,向后查找,如果遇到大于自身值的数组值,对应答案为向后的距离,如果不存在

则为0。

暴力查找只能通过部分测试点,因此解法可用二分查找或单调栈。

单调栈可保证栈中存放的值始终为当前所遍历到的最大值,从而避免无效查询,时间复杂度为O(n)。

本题给出单调栈的参考代码。

#include

#include

#include

using namespace std;

int main()

{

int len;

cin >> len;

vector v(len);

vector res(len, 0);

stack st;

for (int i = 0; i < len; i++)

cin >> v[i];

for (int i = 0; i < len; ++i)

{

while (!st.empty() && v[i] > v[st.top()])

{

auto t = st.top();

st.pop();

res[t] = i - t;

}

st.push(i);

}

for (auto i : res)

cout << i << ’ ';

return 0;

}

在这里插入图片描述

#include

#include

using namespace std;

int hp, att;

int ghp = 100, gatt = 10;

double rnd(int hp, int ghp, int t) {

if (t == 0) //勇者

{

if (att >= ghp)

return 1;

else

return 0.05 + 0.95 * rnd(hp, ghp - att, 1);

} else {

double rtn = 0;

rtn += 0.2 * rnd(hp, ghp, 0);

if (gatt < hp)

rtn += 0.8 * rnd(hp - gatt, ghp, 0);

return rtn;

}

}

int main() {

cin >> hp >> att;

att += 5;

printf(“%.2lf”, rnd(hp, ghp, 0));

return 0;

}

蒙特卡洛算法(百分百超时):

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

#define MAX 1000000000

int game(int hp, int attack) {

int npcHp = hp, npcAttack = 5 + attack;

int pokemonHp = 100, pokemonAttack = 10;

while (1) {

pokemonHp -= npcAttack;

if (pokemonHp <= 0)

return 1;

if (rand() % 5)

return 1;

if (rand() % 20)

continue;

npcHp -= pokemonAttack;

if (npcHp <= 0)

return 0;

}

}

int main() {

srand((unsigned) time(NULL));

int hp, attack;

int res = 0;

scanf(“%d %d”, &hp, &attack);

for (int i = 0; i < MAX; ++i)

res += game(hp, attack);

printf(“%.2lf”, res * 1.0 / MAX);

return 0;

}

在这里插入图片描述

#include

#include

using namespace std;

int mat[100][100];

bool vis[100][100];

struct node {

int x, y, dis;

node() = default;

node(int x, int y, int dis) : x(x), y(y), dis(dis) {}

};

void bfs()

{

bool visS = false;

node beg = node(1, 1, 0);

queue que;

que.push(beg);

vis[beg.x][beg.y] = true;

while (!que.empty()) {

node now = que.front();

que.pop();

if (now.x + 1 >= 1 && now.x + 1 <= 10 && now.y >= 1 && now.y <= 10

&& !vis[now.x + 1][now.y] && mat[now.x + 1][now.y] != 1) {

if (mat[now.x + 1][now.y] == 2)

visS = true;

if (mat[now.x + 1][now.y] == 3) {

if (visS)

cout << "S ";

else

cout << "R ";

cout << now.dis + 1;

}

que.push(node(now.x + 1, now.y, now.dis + 1));

vis[now.x + 1][now.y] = true;

}

if (now.x - 1 >= 1 && now.x - 1 <= 10 && now.y >= 1 && now.y <= 10

&& !vis[now.x - 1][now.y] && mat[now.x - 1][now.y] != 1) {

if (mat[now.x - 1][now.y] == 2)

visS = true;

if (mat[now.x - 1][now.y] == 3) {

if (visS)

cout << "S ";

else

cout << "R ";

cout << now.dis + 1;

}

que.push(node(now.x - 1, now.y, now.dis + 1));

vis[now.x - 1][now.y] = true;

}

if (now.x >= 1 && now.x <= 10 && now.y + 1 >= 1 && now.y + 1 <= 10

&& !vis[now.x][now.y + 1] && mat[now.x][now.y + 1] != 1) {

if (mat[now.x][now.y + 1] == 2)

visS = true;

if (mat[now.x][now.y + 1] == 3) {

if (visS)

cout << "S ";

else

cout << "R ";

cout << now.dis + 1;

}

que.push(node(now.x, now.y + 1, now.dis + 1));

vis[now.x][now.y + 1] = true;

}

if (now.x >= 1 && now.x <= 10 && now.y - 1 >= 1 && now.y - 1 <= 10

&& !vis[now.x][now.y - 1] && mat[now.x][now.y - 1] != 1) {

if (mat[now.x][now.y - 1] == 2)

visS = true;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值