【练习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 max
    int* grade = nullptr; // Declare grade outside the loop
    int student_index,grade_value;
    while (getline(cin, str)) {
        i += 1;
        if (i == 1) {
            num_stu = str[0]-'0'; // Convert char to int
            act = str[2]-'0';
            grade = new int[num_stu]; // Dynamically allocate memory for the array
            for (int i = 0; i < num_stu; ++i) {
                grade[i] = 0; // Initialize each element to 0
            }
        }
        if (i == 3) { // Read and initialize grades
            student_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 operations
            student_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 operation
            cout << 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 memory
    return 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;
}

在这里插入图片描述

相关推荐

  1. python练习2

    2024-05-12 17:34:04       24 阅读
  2. C语言练习(2)

    2024-05-12 17:34:04       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-12 17:34:04       5 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 17:34:04       5 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 17:34:04       4 阅读
  4. Python语言-面向对象

    2024-05-12 17:34:04       6 阅读

热门阅读

  1. Android App开机启动

    2024-05-12 17:34:04       19 阅读
  2. vim 查找字符串的命令

    2024-05-12 17:34:04       22 阅读
  3. 关于XDC 约束固化flash流程

    2024-05-12 17:34:04       40 阅读
  4. 学习Uni-app开发小程序Day8

    2024-05-12 17:34:04       19 阅读
  5. 基于springboot的校园社团信息管理系统源码数据库

    2024-05-12 17:34:04       20 阅读
  6. 程序分享--常见算法/编程面试题:跳跃游戏 II

    2024-05-12 17:34:04       19 阅读
  7. 【docker】容器优化:一行命令换源

    2024-05-12 17:34:04       17 阅读
  8. Vue.js介绍

    2024-05-12 17:34:04       15 阅读