课时4作业3

news/2024/7/24 12:28:04 标签: c++, 算法

Description

某人想将手中的一张面值100元的人民币换成10元、5元、2元和1元面值的票子。要求换正好40张,且每种票子至少一张。问:有几种换法?

Input

无输入

Output

一个数,表示共有多少种换法

Sample Input 1

Sample Output 1

不能告知,因为只有一个数,偷偷告诉你小于100

方法一:四重循环遍历
时间复杂度: O ( n 4 ) O(n^4) O(n4)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int a[N];

void solve() {
    // 100 -> 10、5、2、1

    int res = 0;
    for (int i = 1; i <= 40; i ++ ) {
        for (int j = 1; j <= 40; j ++ ) {
            for (int k = 1; k <= 40; k ++ ) {
                for (int l = 1; l <= 40; l ++ ) {
                    int sum = 10 * i + 5 * j + 2 * k + l;
                    int num = i + j + k + l;
                    if (sum == 100 && num == 40) {
                        res ++ ;
                    }
                }
            }
        }
    }
    cout << res << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_ -- ) {
        solve();
    }
    return 0;
}

方法一:四重循环遍历
时间复杂度: O ( n 3 ) O(n^3) O(n3)

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int a[N];

void solve() {
    // 100 -> 10、5、2、1

    int res = 0;
    for (int i = 1; i <= 40; i ++ ) {
        for (int j = 1; j <= 40; j ++ ) {
            for (int k = 1; k <= 40; k ++ ) {
                int sum = i * 10 + j * 5 + k * 2;
                int dif = 100 - sum;
                if (dif >= 1 && i + j + k + dif == 40) {
                    res ++ ;
//                    cout << i << " " << j << " " << k << " " << dif << "\n";
                }
            }
        }
    }
    cout << res << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_ -- ) {
        solve();
    }
    return 0;
}

把1直接算出来而不是用循环遍历,可以省掉一重循环,将时间复杂度变成 O ( n 3 ) O(n^3) O(n3)

在这里插入图片描述
错了四遍的神奇题目,因为把题目的每种票子至少一张看漏了,要好好审题,不然罚时嘎嘎多!


http://www.niftyadmin.cn/n/5099082.html

相关文章

Qt基础 QPieSeries饼状图

目录 1.简单例子 2. 稍微复杂点 QPieSeries Class&#xff1a;饼状图数据 QChart 管理图表系列、图例和轴的图形表示 QChartView 可以显示图表的独立小部件 QPieSeries 在饼图中显示数据 QPieSlice 表示饼图系列中的单个切片&#xff08;其实就是标签&#xff09; 1…

代码随想录第44天 | ● 1143.最长公共子序列 ● 1035.不相交的线 ● 53. 最大子序和 动态规划

1143.最长公共子序列 //*** param {string} text1* param {string} text2* return {number}*/ var longestCommonSubsequence function(text1,text2) {let dp new Array(text2.length1)dp.fill(0)for(let i 1; i < text1.length; i) {// 这里pre相当于 dp[i - 1][j - 1]…

5.DApp-前端网页怎么连接MetaMask

题记 在前端网页连接metamask&#xff0c;以下是全部操作流程和代码。 编写index.html文件 index.html文件如下&#xff1a; <!DOCTYPE html> <html> <head> <title>My DApp</title> <!--导入用于检测Metamask提供者的JavaScript库--> &l…

【JavaEE】Callable 接口

Callable 是一个 interface . 相当于把线程封装了一个 “返回值”. 方便程序猿借助多线程的方式计算结果. 实现Callable也是创建线程的一种方法&#xff01;&#xff01;&#xff01;&#xff01; Callable的用法非常接近于Runnable&#xff0c;Runnable描述了一个任务&#…

[机缘参悟-110] :一个IT人对面具的理解:职业面具戴久了,就会忘记原本真实的自己,一个人是忠于职位,还是忠于内心?

目录 一、职业面具戴久了&#xff0c;就会忘记原本真实的自己 二、霸王别姬 三、没有对错&#xff0c;各走各路 3.1 程蝶衣&#xff1a;戏里戏外&#xff0c;忠于角色 3.2 段小楼&#xff1a;戏里戏外&#xff0c;角色分明 3.3 没有对错&#xff0c;各走各路 四、职场中…

QT_day1

#include "mywidget.h"MyWidget::MyWidget(QWidget *parent): QWidget(parent) {//窗口相关设置this->setWindowTitle("登录窗口");this->setWindowIcon(QIcon("C:\\Users\\EDY\\Desktop\\pictrue\\qq.png"));this->setWindowFlag(Qt::…

Python编译与反编译

py文件编译pyc 命令编译 python -m py_compile xxx.py脚本编译 import py_compilepy_compile.compile(rE:\mytest.py) #py文件完整的路径.pyc文件反编译 安装uncompyle uncompyle6 能反编译 Python3.0~Python3.8 的pyc文件 pip install uncompyle反编译文件 将xxx.pyc反…

SSM - Springboot - MyBatis-Plus 全栈体系(三十一)

第七章 MyBatis-Plus 二、MyBatis-Plus 核心功能 1. 基于 Mapper 接口 CRUD 通用 CRUD 封装 BaseMapper (opens new window)接口&#xff0c; Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器! 内部包含常见的单表操作&#xff01; 1.1 Insert 方…