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 2∗164−1+10∗163−1+4∗162−1+13∗161−1=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;
}