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

【练习2】

1.汽水瓶

在这里插入图片描述
ps:注意涉及多个输入,我就说怎么老不对,无语~

#include <cmath>
#include <iostream>
using namespace std;int main() {int n;int num,flag,kp,temp;while (cin>>n) {flag=1;num=0;temp=0;kp=n;while (flag==1) {if(kp<=2){if(kp==2){temp=1;}flag=0;}else{temp=floor(kp/3);kp=kp%3+temp;}num=num+temp;temp=0;}if(num>0){cout<<num<<endl;}}return 0;
}

在这里插入图片描述

2.十六进制

16进制中有:0-9,A-F(a-f);
对于一个16进制的数(eg:2A4D)转为10进制= 2 ∗ 1 6 4 − 1 + 10 ∗ 1 6 3 − 1 + 4 ∗ 1 6 2 − 1 + 13 ∗ 1 6 1 − 1 = 10829 2*16^{4-1}+10*16^{3-1}+4*16^{2-1}+13*16^{1-1}=10829 21641+101631+41621+131611=10829
ps:访问字符串时是从左往右,并且字符串每个字符是从0开始;
输入字符串使用getline(cin,str)即可获得字符串str。

在这里插入图片描述

#include <iostream>
#include <string>
#include <cmath>
using namespace std;int main() {string input;int i,temp,res;res=0;getline(cin,input);for(i=0;i<=input.length()-1;i++){if(input[i]>='A'&&input[i]<='F'){temp=(input[i]-'A')+10;}else if(input[i]>='a'&&input[i]<='f'){temp=(input[i]-'a')+10;}else if(input[i]>='0'&&input[i]<='9'){temp=input[i]-'0';}else{temp=0;}res+=temp*pow(16,input.length()-i-1);}cout<<res<<endl;
}

在这里插入图片描述

3.最高分是多少

在这里插入图片描述
在这里插入图片描述

示例测试正确,提交错误,说递归太多(暂不知道怎么改

ps:这里的每一行的空格也占了一个位置,索引时要注意。预设动态数组。
!!注意:U是更新,Q是询问,Q是先回答再更新,U是先更新再回答。(服了…)

#include <iostream>
using namespace std;int Max_fun(int input[], int size) {int max = input[0];for (int j = 1; j < size; j++) {if (input[j] > max) {max = input[j];}}return max;
}int main() {string str;int i = 0;int num_stu, act,max = 0; // Initialize maxint* grade = nullptr; // Declare grade outside the loopint student_index,grade_value;while (getline(cin, str)) {i += 1;if (i == 1) {num_stu = str[0]-'0'; // Convert char to intact = str[2]-'0';grade = new int[num_stu]; // Dynamically allocate memory for the arrayfor (int i = 0; i < num_stu; ++i) {grade[i] = 0; // Initialize each element to 0}}if (i == 3) { // Read and initialize gradesstudent_index = str[2]-'0';grade_value = str[4]-'0';grade[student_index-1] = grade_value;max = grade_value; // Update max after initializing grades}if (i > 3 && i <= 3 + act && str[0]=='U') { // Process operationsstudent_index = str[2]-'0';grade_value = str[4]-'0';grade[student_index-1] = grade_value;max = Max_fun(grade, num_stu); // Update max after every update operation}if (str[0] == 'Q') { // Query operationcout << max << endl;student_index = str[2]-'0';grade_value = str[4]-'0';grade[student_index-1] = grade_value;max = Max_fun(grade, num_stu); }}delete[] grade; // Free dynamically allocated memoryreturn 0;
}

在这里插入图片描述

4.简单错误记录

在这里插入图片描述

待学习,测试只通过了2组

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;// 定义结构体存储文件名和对应的代码行数统计
struct FileInfo {string filename;int count;
};int main() {unordered_map<string, int> fileCounts; // 存储文件名和对应的代码行数统计string input;while (getline(cin, input)) {// 解析输入行size_t pos = input.find_last_of('\\');string filename = input.substr(pos + 1);fileCounts[filename]++; // 更新对应文件的代码行数统计}// 将统计信息存入vector中,方便排序vector<FileInfo> fileInfoList;for (const auto& pair : fileCounts) {fileInfoList.push_back({pair.first, pair.second});}// 按照代码行数降序排序,如果行数相同则按照输入顺序排序sort(fileInfoList.begin(), fileInfoList.end(), [](const FileInfo& a, const FileInfo& b) {if (a.count == b.count) {return a.filename < b.filename;}return a.count > b.count;});// 输出前8条记录或者全部记录int count = 0;for (const auto& fileInfo : fileInfoList) {cout << fileInfo.filename.substr(max(0, (int)fileInfo.filename.size() - 16)) << " " << fileInfo.count << endl;count++;if (count == 8) {break;}}return 0;
}

在这里插入图片描述

http://www.lryc.cn/news/346523.html

相关文章:

  • oracle 新_多种块大小的支持9i
  • Collections工具类
  • java-函数式编程-jdk
  • qiankun实现微前端,vue3为主应用,分别引入vue2和vue3微应用
  • 写了 1000 条 Prompt 之后,我总结出了这 9 个框架【建议收藏】
  • 事件代理 浅谈
  • 一对多在线教育系统,疫情后,在线教育有哪些变革?
  • RabbitMQ(安装配置以及与SpringBoot整合)
  • JUC下的BlockingQueue详解
  • ChatGPT理论分析
  • 算法提高之魔板
  • 服务器内存占用不足会怎么样,解决方案
  • elasticsearch文档读写原理大致分析一下
  • 1 开发环境
  • 云视频,也称为视频云服务,是一种基于云计算技术理念的视频流媒体服务
  • [Vision Board创客营]--使用openmv识别阿尼亚
  • 【Linux:lesson1】的基本指令
  • 20240511日记
  • 蓝桥杯成绩已出
  • .kat6.l6st6r勒索病毒数据怎么处理|数据解密恢复
  • Spring Batch 是什么?主要用于什么场景?
  • SQL-慢查询的定位及优化
  • 练习题(2024/5/11)
  • linux系统服务器中常见故障及排查方法
  • 产品人生(5):从“敏捷开发”到“四化时间管理法”
  • 超级好看的html网站维护源码
  • 从零开始搭建Springboot项目脚手架2:配置文件、返回值、日志等
  • Java web第五次作业
  • Unity使用ToggleGroup对多个Toggle进行管理时,初始化默认选项失效的问题
  • Retrofit同步请求直接返回目标对象