头像

风向决定发型d


访客:399

离线:7小时前


活动打卡代码 AcWing 1634. PAT单位排行

#include<iostream>
#include<unordered_map>
#include<cstring>
#include<algorithm>
#include<vector>

using namespace std;
struct School{
    string name;
    int cnt;
    double sum;
    School():cnt(0),sum(0){}
    bool operator< (const School &t) const
    {
        if (sum != t.sum) return sum > t.sum;
        if (cnt != t.cnt) return cnt < t.cnt;
        return name < t.name;
    }
};
int main(){
    int n;
    cin>>n;
    unordered_map<string,School> mp;
    while(n--){
        string id,sch;
        double grade;
        cin>>id>>grade>>sch;
        for(auto&c:sch) c=tolower(c);
        if(id[0]=='B') grade/=1.5;
        else if(id[0]=='T') grade*=1.5;
        mp[sch].name=sch;
        mp[sch].cnt++;
        mp[sch].sum+=grade;
    }
    vector<School>school;
    for(auto &item:mp){
        item.second.sum=(int)(item.second.sum + 1e-8);
        school.push_back(item.second);
    }
    sort(school.begin(),school.end());
    cout<<school.size()<<endl;
    int rank=1;
    for(int i=0;i<school.size();i++){
        auto s=school[i];
        if(s.sum!=school[i-1].sum) rank=i+1;
        printf("%d %s %d %d\n", rank, s.name.c_str(), (int)s.sum, s.cnt);
    }
    return 0;
}


活动打卡代码 AcWing 1617. 微博转发抽奖

#include<iostream>
#include<unordered_set>

using namespace std;

const int N=1010;
int m,n,s;
string name[N];
int main(){
    cin>>m>>n>>s;
    unordered_set<string>mp;
    for(int i=1;i<=m;i++) cin>>name[i];
    while(s<=m){
        if(mp.count(name[s])) s++;
        else{
            cout<<name[s]<<endl;
            mp.insert(name[s]);
            s+=n;
        }
    }
    if(mp.empty()) cout<<"Keep going...";
    return 0;
}


活动打卡代码 AcWing 1598. 求平均值

#include<iostream>

using namespace std;
int main(){
    int n,cnt=0;
    double sum=0;
    cin>>n;
    for(int i=0;i<n;i++){
        string num;
        cin>>num;
        double x;
        bool success=true;
        try{
            size_t idx;
            x=stof(num,&idx);
            if(idx<num.size()) success=false;
        }
        catch(...){
            success=false;
        }
        if(x<-1000||x>1000) success=false;
        int k=num.find('.');
        if(k!=-1&&num.size()-k>3) success=false;
        if(success) cnt++,sum+=x;
        else printf("ERROR: %s is not a legal number\n",num.c_str());
    }
    if(cnt>1) printf("The average of %d numbers is %.2f\n",cnt,sum/cnt);
    else if(cnt==1) printf("The average of 1 number is %.2f\n",sum);
    else printf("The average of 0 numbers is Undefined\n");
    return 0;
}


活动打卡代码 AcWing 1570. 坏掉的键盘

#include<iostream>

using namespace std;
bool st[200]={false};
int main(){
    string a,b;
    cin>>a>>b;
    for(int i=0,j=0;i<a.size();i++){
        char x=toupper(a[i]),y=toupper(b[j]);
        if(x==y) j++;
        else{
            if(!st[x]) cout<<x,st[x]=true;
        }
    }
    return 0;
}


活动打卡代码 AcWing 1568. 中文读数字

#include<iostream>
#include<vector>

using namespace std;
string nums0[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
bool check(string res){
    return res.size()>=5&&res.substr(res.size()-5)=="ling ";
}
string checknum(int n){
    vector<int>nums;
    string res;
    while(n) nums.push_back(n%10),n/=10;
    string nums1[]={"","Shi","Bai","Qian"};
    for(int i=nums.size()-1;i>=0;i--){
        int t=nums[i];
        if(t) res+=nums0[t]+' ';
        else if(!check(res)) res+="ling ";
        if(t&&i) res+=nums1[i]+' ';
    }
    if(check(res)) res=res.substr(0,res.size()-5);
    return res;
}
int main(){
    int n;
    cin>>n;
    if(n<0) cout<<"Fu ",n=-n;
    if(!n){
        cout<<"ling";
        return 0;
    }
    vector<int>nums;
    string res;
    string nums2[]{"","Wan","Yi"};
    while(n) nums.push_back(n%10000),n/=10000;
    for(int i=nums.size()-1;i>=0;i--){
        int t=nums[i];
        if(res.size()&&t<1000) res+="ling ";
        if(t) res+=checknum(t);
        if (t&&i) res+=nums2[i]+' ';
    }
    while(check(res)) res=res.substr(0,res.size()-5);
    res.pop_back();
    cout<<res;
    return 0;
}


活动打卡代码 AcWing 1563. Kuchiguse

#include<iostream>

using namespace std;
const int N=110;
string s[N];
int main(){
    int n;
    cin>>n;
    getchar();
    for(int i=0;i<n;i++) getline(cin,s[i]);
    for(int k=s[0].size();k>0;k--){
        string str=s[0].substr(s[0].size()-k);
        bool match=true;
        for(int i=1;i<n;i++){
            if(k>s[i].size()||s[i].substr(s[i].size()-k)!=str){
                match=false;
                break;
            }
        }
        if(match){
            cout<<str;
            return 0;
        }
    }
    puts("nai");
    return 0;
}


活动打卡代码 AcWing 1559. 科学计数法

#include<iostream>

using namespace std;

int main(){
    string s,a;
    cin>>s;
    if(s[0]=='-') cout<<'-';
    int k=s.find("E");
    a+=s[1]+s.substr(3,k-3);
    int b=stoi(s.substr(k+1));
    b++;
    if(b<=0) a="0."+string(-b,'0')+a;
    else if(a.size()>b) a=a.substr(0,b)+"."+a.substr(b);
    else a+=string(b-a.size(),'0');
    cout<<a;
    return 0;
}


活动打卡代码 AcWing 1546. 它们是否相等

#include<iostream>

using namespace std;
int n;
string change(string a){
    int k=a.find(".");
    if(k==-1) a+=".",k=a.find(".");
    string s=a.substr(0,k)+a.substr(k+1);
    while(s.size()&&s[0]=='0') s=s.substr(1),k--;
    if(s.empty()) k=0;
    if(s.size()>n) s=s.substr(0,n);
    else s+=string(n-s.size(),'0');
    return "0."+s+"*10^"+to_string(k);
}
int main(){
    string a,b;
    cin>>n>>a>>b;
    a=change(a);
    b=change(b);
    if(a==b) cout<<"YES"<<' '<<a;
    else cout<<"NO"<<' '<<a<<' '<<b;
    return 0;
}


活动打卡代码 AcWing 1576. 再次树遍历

#include<iostream>
#include<stack>

using namespace std;
const int N=40;
int l[N],r[N],root;
void dfs(int u,int root){
    if(!u) return;
    dfs(l[u],root);
    dfs(r[u],root);
    cout<<u;
    if(u!=root) cout<<' ';
}
int main(){
    int n,last=0,type;
    cin>>n;
    stack<int>stk;
    for(int i=0;i<2*n;i++){
        string str;
        cin>>str;
        if(str=="Push"){
            int x;
            cin>>x;
            if(!last) root=x;
            else{
                if(type==0) l[last]=x;
                else r[last]=x;
            }
            type=0;
            last=x;
            stk.push(x);
        }
        else{
            last=stk.top();
            stk.pop();
            type=1;
        }
    }
    dfs(root,root);
    return 0;
}



#include<iostream>
#include<algorithm>

using namespace std;
const int N=1010;
int w[N],n,tr[N];
void dfs(int u,int &k){
    if(u*2<=n) dfs(u*2,k);
    tr[u]=w[k++];
    if(u*2+1<=n) dfs(u*2+1,k);
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++) cin>>w[i];
    sort(w,w+n);
    int k=0;
    dfs(1,k);
    cout<<tr[1];
    for(int i=2;i<=n;i++) cout<<' '<<tr[i];
    return 0;
}