首页 > 其他分享 >2023/03/12刷题

2023/03/12刷题

时间:2023-03-17 21:23:23浏览次数:48  
标签:03 12 cout int number long 刷题 include define

A. Appleman and Toastman

链接

A. Appleman and Toastman

这个题要计算最大值所以我们肯定直接,每次都减少最少的那个,然后使用一个变量每次把值加上最后打印出来结果就可以了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no     cout<<"NO"<<'\n'

using namespace std;
const int N = 300008;

int a[N];
signed main () {
    int n;
    scanf("%lld", &n);
    int sum = 0;
    for (int i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
        sum = sum + a[i];
    }
    //先计算sum的总和
    sort(a, a + n);//然后对数组进行排序
    int ans = sum;//先让ans加上整个数组
    for (int i = 1; i < n; i++) {
        ans = ans + a[i - 1];//每一次循环加上
        sum = sum - a[i - 1];
        ans = ans + sum;

    }

    printf("%lld", ans);




    return 0;
}

C. Corners

链接

C. Corners

找规律我们可以发现,如果图形中4个中有两个1的话,或者少于两个1.只要有一个图形满足这种情况的话

就肯定可以使用L这种操作一次只删除一个1

如果不存在这种情况的话,如果最多一个这种图形里面有3个1就要将1的总数减去1,如果只存在4个1的话,就必须要从全部的1里面减去2

image20230313222802914

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no     cout<<"NO"<<'\n'

using namespace std;
const int N = 600;
char mp[N][N];
void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        scanf("%s", mp[i] + 1);//读入数据
    }
    int flag = 0;
    int ling = 0;
    int one = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (mp[i][j] == '0') {
                ling++;
            } else {
                one++;
            }
        }

    }//统计有几个0,几个1


    for (int i = 1; i <= n - 1; i++) {
        for (int j = 1; j <= m - 1; j++) {
            int x = 0;
            if (mp[i][j] == '0') {
                x++;
            }
            if (mp[i + 1][j] == '0') {
                x++;
            }

            if (mp[i][j + 1] == '0') {
                x++;
            }
            if (mp[i + 1][j + 1] == '0') {
                x++;
            }
            if (x >= 2) {//如果一个4个如图组成的正方形里面的0大于等于两个
                flag = 1;
            }
        }
    }
    if (flag == 1) {//就打印出全部1的数量
        cout << one << '\n';
        if (ling == 0) {//如果一个0都没有就打印出1的数量减2
            cout << one - 2 << '\n';
        } else {//其他情况加上4个字符只有一个0的情况,打印1的数量减一
            cout << one - 1 << '\n';
        }
    }


}
signed main () {
    int t;
    cin >> t;
    while (t) {
        solve();
        t--;
    }


    return 0;
}

A. Buying Torches

链接

A. Buying Torches

具体分析看一下代码,不好解释

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no     cout<<"NO"<<'\n'

using namespace std;

void solve() {
    int x, y, k;
    cin >> x >> y >> k;//读入数据
    int number = (y + 1) * k;//制作一个火把需要一个煤炭和一个棍子,,,,所需要的总的棍子数
    number--;//最开始有一个棍子了,让number成为还需要的棍子的数量
    int ans = (number + (x - 1) - 1) / (x - 1);//(x-1)为一次增加x-1个这样,让number除以(x-1)向上取整
    cout << ans + k << '\n';//打印ans加k....ans是兑换木棍的操作,k是兑换煤炭的数量







}
signed main () {
    int t;
    cin >> t;
    while (t) {
        solve();
        t--;
    }


    return 0;
}

C. Paint the Array

链接

C. Paint the Array

这个题我也是看了别人的代码才会的,就是说从第1,3,5,7索引中找到他们的最大公约数.然后判断索引0,2,4,6,8里面能不能被他们的最大公约数整除..

同时求出0,2,4,6,8.的最大公约数,然后判断一下这些索引1,3,5,7,9....的数组能不能整除上面的最大公约数

这两种情况满足那一种我们就打印哪一种的最大公约数就可以了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no     cout<<"NO"<<'\n'

using namespace std;
const int     N = 200;
int a[N];
void solve() {
    int n;
    scanf("%lld", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);//读入
    }
    int gcd1 = a[1];
    int gcd2 = a[2];
    for (int i = 1; i <= n; i += 2) {
        gcd1 = __gcd(a[i], gcd1);//求出1,3,5.....为索引的数组的最大公约数gcd1
    }
    for (int i = 2; i <= n; i += 2) {
        gcd2 = __gcd(a[i], gcd2);//求出2,4,6.....为索引的数组的最大公约数gcd2
    }
    int flag1 = 1, flag2 = 1;
    for (int i = 2; i <= n; i += 2) {
        if (a[i] % gcd1 == 0) {//如果2,4,6.....为索引的数组不能整除gcd1的话,就打印gcd1
            flag1 = 0;
            break;
        }
    }
    for (int i = 1; i <= n; i += 2) {
        if (a[i] % gcd2 == 0) {//如果1,3,5.....为索引的数组不能整除gcd2的话,就打印gcd2
            flag2 = 0;
            break;
        }
    }


    if (flag1 == 1) {
        cout << gcd1 << '\n';
    } else if (flag2 == 1) {
        cout << gcd2 << '\n';
    } else {//如果两种情况都不满足的话,打印0

        cout << 0 << '\n';

    }



}
signed main () {
    int t;
    cin >> t;
    while (t) {
        solve();
        t--;
    }


    return 0;
}

标签:03,12,cout,int,number,long,刷题,include,define
From: https://www.cnblogs.com/harper886/p/17228186.html

相关文章

  • 2023/03/13刷题
    C.BoxesPacking链接C.BoxesPacking这个题就是找相同的数字的最大值.因为每一个数字都要放在一个盒子里面打印就可以#include<iostream>#include<algorithm>#i......
  • 2023/03/14刷题
    A.IQtest链接A.IQtest这个题就是给一个数数组,数组有两种情况。要么有n-1个奇数和一个偶数要么有n-1个偶数和一个奇数让我们求出这一个奇数和一个偶数所在数组......
  • day3 | 203. 移除链表元素,206. 反转链表,707. 设计链表
    203.移除链表元素 题目描述 给你一个链表的头节点head和一个整数val,删除链表中的那些存储的值为val的节点,并且返回新的头节点。 思路: 1.创建一个虚拟头节点,......
  • 20230317
    数据结构remake第一天线性表的操作////babyDataStructrue.cpp//dataStructure////Createdbyon2023/3/17.#include<stdio.h>#defineN10#defineMAX20......
  • HTTP 返回状态码403,404,502等不同报错原因及解决思路
    要学会看日志rpm的默认路径/var/log/nginx/源码的默认路径安装路径/logs/排错思路:1)服务器启动失败,直接"nginx-t"测试语法  看配置文件是否正确2)服务器启动成功,访......
  • Ubuntu中安装包时提示:you might want to run 'sudo dpkg --configure -a' to correct
    场景在Ubuntu中执行sudoapt-getinstall-ynano时提示:youmightwanttorun'sudodpkg--configure-a'tocorrecttheproblem实现按照提示输入:sudodpkg--configure......
  • 狂神说的MyBatisPlus笔记 -https://blog.csdn.net/weixin_43070148/article/details/1
    狂神说的MyBatisPlus笔记https://blog.csdn.net/weixin_43070148/article/details/127313367学习MyBatis-Plus之前要先学MyBatis–>Spring—>SpringMVC为什么要学它?MyB......
  • 前端-笔试刷题-JavaScript
    基本数据类型检测题目描述请补全JavaScript函数,要求以字符串的形式返回参数的类型。注意:只需检测基本数据类型。点击查看代码function_typeof(value){//......
  • LeetCode|412. Fizz Buzz
    题目链接:412.FizzBuzz给你一个整数n,找出从1到n各个整数的FizzBuzz表示,并用字符串数组answer(下标从1开始)返回结果,其中:answer[i]=="FizzBuzz"如果i......
  • 034Linux磁盘空间未释放并且在不能重启情况下最佳处理方法
    Linux磁盘空间未释放并且在服务器和程序不能重启时处理方法:之前遇到的磁盘df-h和du-sh*查看文件大小结果不一致,发生这种情况的原因一般有两种:1.有隐藏文件2.磁盘空......