当前位置: 首页 > news >正文

笔试——Day39

文章目录

  • 第一题
    • 题目
    • 思路
    • 代码
  • 第二题
    • 题目
    • 思路
    • 代码
  • 第三题
    • 题目
    • 思路
    • 代码

第一题

题目

神奇的字母(二)

在这里插入图片描述

思路

哈希表统计字符串中每个字母出现的次数

代码

#include<iostream>
#include<string>using namespace std;int main()
{string s;int hash[26] = {0};char res;int maxCount = -1;while(cin >> s){for(auto &e : s){hash[e - 'a']++;if(hash[e - 'a'] > maxCount){res = e;maxCount = hash[e - 'a'];}}}cout << res << endl;return 0;
}

第二题

题目

字符编码

在这里插入图片描述

思路

哈夫曼编码,求最短编码长度,利用小堆进行处理

代码

#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;int main() 
{string s;while (cin >> s) { int hash[300] = {0};for(auto &e : s){hash[e]++;}priority_queue<int, vector<int>, greater<int>> pq;for(auto &e : hash){if(e) pq.push(e);}int res = 0;while(pq.size() > 1){int a = pq.top(); pq.pop();int b = pq.top(); pq.pop();res += (a + b);pq.push(a + b);}cout << res << endl;}return 0;
}
// 64 位输出请用 printf("%lld")

第三题

题目

最少的完全平方数

在这里插入图片描述

思路

动态规划

代码

#include <cstring>
#include <iostream>
using namespace std;const int N = 1e4 + 10;
int dp[N];
int n;int main()
{   // dp[i][j]表示:从前i个数中挑选(1 2 4 9 16...),恰好为j时,最少挑选出几个数// 状态转移方程:// 不选第i个数,dp[i][j] = dp[i - 1][j]// 选第i个数1个,dp[i][j] = dp[i - 1][j - i * i] + 1// 选第i个数2个,dp[i][j] = dp[i - 1][j - 2 * i * i] + 2// ...// 所有选的可能,dp[i][j] = dp[i][j - i * i] + 1cin >> n;memset(dp, 0x3f, sizeof dp);dp[0] = 0;for(int i = 1; i <= n; i++){for(int j = i * i; j <= n; j++){dp[j] = min(dp[j], dp[j - i * i] + 1);}}cout << dp[n] << endl;return 0;
}
http://www.xdnf.cn/news/1303111.html

相关文章:

  • DevEco Studio 6.0.0 元服务页面跳转失败
  • Spring事物
  • 智能座舱软件架构设计的宏观决策框架
  • 【自动驾驶】自动驾驶概述 ② ( 自动驾驶技术路径 | L0 ~ L5 级别自动驾驶 )
  • 数据结构:二叉树的表示方式(Representation of Binary Trees)
  • 【测试工具】JMeter基本使用及MySQL数据库压力测试
  • Baumer高防护相机如何通过YoloV8深度学习模型实现驾驶员疲劳的检测识别(C#代码UI界面版)
  • python的美食交流社区系统
  • @[TOC](计算机是如何⼯作的) JavaEE==网站开发
  • 前端性能优化工具Performance面板实战指南
  • 【swift开发】SwiftUI概述 SwiftUI 全面解析:苹果生态的声明式 UI 革命
  • 【C#补全计划】事件
  • 【2D】圆上数值积分(半径方向用高斯积分减少点数)
  • 综合案例:Python 函数知识整合 — 学生成绩管理系统
  • Python 类(Class)学习
  • 【新手入门】Android基础知识(一):系统架构
  • 【Golang】:流程控制语句
  • 【Vibe Coding 工程之 StockAnalyzerPro 记录】- EP1.先写 PRD
  • 【秋招笔试】2025.08.15饿了么秋招机考-第一题
  • P4069 [SDOI2016] 游戏 Solution
  • 微信小程序 拖拽签章
  • Git版本控制器
  • spring中异步任务注解@Async和@scheduled的使用
  • 2025年机械制造、机器人与计算机工程国际会议(MMRCE 2025)
  • Docker Compose 入门教程
  • MySQL、PolarDB、PolarDB-X、TableStore、MongoDB、TiDB、ClickHouse选型
  • docker入门
  • Java 调用 Python 脚本:实现 HelloWorld
  • 计算机视觉(opencv)实战五——图像平滑处理(均值滤波、方框滤波、高斯滤波、中值滤波)附加:视频逐帧平滑处理
  • 从根本上解决MAC权限问题(关闭sip)