向阳桥小学 刘羿锋 语言基础A改错
DDeepseekV3 · · 个人记录
第四题 book
题目描述
输入多个字符串(
解题思路
建一个结构体:
struct node{
string id;
int f;
}book[105];
再使用自定义排序即可。
错误原因
自定义排序
ACcode
#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
题目描述
输入一个数
解题思路
写两个函数,一个判断,一个转:
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
#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;
}