专栏文章

向阳桥小学 刘羿锋 语言基础A改错

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@minrwf05
此快照首次捕获于
2025/12/02 07:21
3 个月前
此快照最后确认于
2025/12/02 07:21
3 个月前
查看原文

第四题 book

题目描述

输入多个字符串(idid)与对应的分数(ff),先按 idid 词典序排,如果有相同的,哪个的 ff 大哪个排前面。

解题思路

建一个结构体:
CPP
struct node{
    string id;
    int f;
}book[105];
再使用自定义排序即可。

错误原因

自定义排序 cmpcmp 中的条件判断没写对。

ACcode

CPP
#include<bits/stdc++.h>
using namespace std;
struct node{
    string name;
    int id,f;
}book[105];
bool cmp(node a,node b){
    if(a.name==b.name)return a.f>b.f;
    else a.name<b.name;
}
int n;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>book[i].name>>book[i].f;
        book[i].id=i;
    }
    sort(book+1,book+1+n,cmp);
    for(int i=1;i<=n;i++){
        cout<<book[i].id<<endl;
    }
}

第五题 number7

题目描述

输入一个数 nn,求 1n1-n中有多少个数的八进制与十进制中没有 77

解题思路

写两个函数,一个判断,一个转:
CPP
bool no7(int n){
    while(n){
        if(n%10==7)return 0;
        n/=10;
    }
    return 1;
}
int tento8(int n){
    int res=0;
    while(n){
        res=res*10+n%8;
        n/=8;
    }
    string a=to_string(res);
    reverse(a.begin(),a.end());
    return stoi(a);
}
(to_string()是将数字转为字符串的函数,stoi()是将字符串转为数字的函数)

错误原因

十转八进制的函数没写对。

ACcode

CPP
#include<bits/stdc++.h>
using namespace std;
bool no7(int n){
    while(n){
        if(n%10==7)return 0;
        n/=10;
    }
    return 1;
}
int tento8(int n){
    int res=0;
    while(n){
        res=res*10+n%8;
        n/=8;
    }
    string a=to_string(res);
    reverse(a.begin(),a.end());
    return stoi(a);
}
int main(){
    int n,cnt=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        if(no7(i)&&no7(tento8(i))){
            cnt++;
        }
    }
    cout<<cnt;
}

评论

0 条评论,欢迎与作者交流。

正在加载评论...