新生46

问题 A: 金币

题目描述

国王将金币作为工资,发放给忠诚的骑士。第一天,骑士收到一枚金币;之后两天(第二天和第三天),每天收到两枚金币;之后三天(第四、五、六天),每天收到三枚金币;之后四天(第七、八、九、十天),每天收到四枚金币……;这种工资发放模式会一直这样延续下去:当连续N天每天收到N枚金币后,骑士会在之后的连续N+1天里,每天收到N+1枚金币。

请计算在前K天里,骑士一共获得了多少金币。

输入

输入只有1行,包含一个正整数K(1≤K≤10000),表示发放金币的天数。

输出

输出只有1行,包含一个正整数,即骑士收到的金币数。

样例输入 Copy

6

样例输出 Copy

14

提示

骑士第一天收到一枚金币;第二天和第三天,每天收到两枚金币;第四、五、六天,每天收到三枚金币。因此一共收到 1+2+2+3+3+3=14 枚金币。
复制代码
#include<iostream>
using namespace std;
int main(){
    int n,sum=0,t=0;
    cin>>n;
    for(int i=1;;i++)
    {
        for(int j=1;j<=i;j++)
        {
            sum+=i;
            t++;
            if(t==n)
                break;
        }
        if(t==n)
            break;
    }
    cout<<sum<<endl;
    return 0;
}
复制代码

问题 B: 扫雷游戏

题目描述

扫雷游戏是一款十分经典的单机小游戏。在n行m列的雷区中有一些格子含有地雷(称之为地雷格),其他格子不含地雷(称之为非地雷格)。玩家翻开一个非地雷格时,该格将会出现一个数字——提示周围格子中有多少个是地雷格。游戏的目标是在不翻出任何地雷格的条件下,找出所有的非地雷格。

现在给出n行m列的雷区中的地雷分布,要求计算出每个非地雷格周围的地雷格数。

注:一个格子的周围格子包括其上、下、左、右、左上、右上、左下、右下八个方向上与之直接相邻的格子。

输入

第一行是用一个空格隔开的两个整数n和m(1≤n≤100,1≤m≤100),分别表示雷区的行数和列数。
接下来n行,每行m个字符,描述了雷区中的地雷分布情况。字符’*’表示相应格子是地雷格,字符’?’表示相应格子是非地雷格。相邻字符之间无分隔符。

输出

输出包含n行,每行m个字符,描述整个雷区。用’*’表示地雷格,用周围的地雷个数表示非地雷格。相邻字符之间无分隔符。

样例输入 Copy

3 3
*??
???
?*?

样例输出 Copy

*10
221
1*1
写的又臭又长....但除了这个我没想到其他的...还耽误了不少时间
复制代码
#include<iostream>
using namespace std;
const int N=110;
char ch[N][N];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>ch[i][j];
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int t=0;
            if(ch[i][j]=='?')
            {
                if(ch[i-1][j]=='*')
                    t++;
                if(ch[i+1][j]=='*')
                    t++;
                if(ch[i][j-1]=='*')
                    t++;
                if(ch[i][j+1]=='*')
                    t++;
                if(ch[i-1][j-1]=='*')
                    t++;
                if(ch[i-1][j+1]=='*')
                    t++;
                if(ch[i+1][j-1]=='*')
                    t++;
                if(ch[i+1][j+1]=='*')
                    t++;
                cout<<t;
            }
            else
                cout<<"*";
        }
        cout<<endl;
    }
}
复制代码

Collecting Balls I

题目描述

There are N balls in the xy-plane. The coordinates of the i-th of them is (xi,i). Thus, we have one ball on each of the N lines y=1, y=2, …, y=N.
In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B. Then, he placed the i-th type-A robot at coordinates (0,i), and the i-th type-B robot at coordinates (K,i). Thus, now we have one type-A robot and one type-B robot on each of the N lines y=1, y=2, …, y=N.
When activated, each type of robot will operate as follows.
When a type-A robot is activated at coordinates (0,a), it will move to the position of the ball on the line y=a, collect the ball, move back to its original position (0,a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.
When a type-B robot is activated at coordinates (K,b), it will move to the position of the ball on the line y=b, collect the ball, move back to its original position (K,b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.
Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.

Constraints
1≤N≤100
1≤K≤100
0<xi<K
All input values are integers.

输入

Input is given from Standard Input in the following format:
N
K
x1 x2 … xN

输出

Print the minimum possible total distance covered by robots.

样例输入 Copy

1
10
2

样例输出 Copy

4

提示

There are just one ball, one type-A robot and one type-B robot.
If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.
Similarly, if the type-B robot is used, the total distance covered will be 16.
Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4.
题目意思理解了,问题不大
复制代码
#include<iostream>
using namespace std;
int main(){
    int n,k,t,x,y,sum=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>t;
        x=t*2;
        y=(k-t)*2;
        sum+=min(x,y);
    }
    cout<<sum<<endl;
    return 0;
}
复制代码

 

posted @   小志61314  阅读(68)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示