当前位置: 代码迷 >> 综合 >> 程序设计与算法 | (18) Lecture(9) OJ作业
  详细解决方案

程序设计与算法 | (18) Lecture(9) OJ作业

热度:33   发布时间:2024-01-15 00:12:26.0

OJ地址

Lecture (9)包含以下五道编程题,可以在上面的OJ提交。

054:成绩排序

在这里插入图片描述

#include <iostream>
using namespace std;
#include <algorithm>
#include <cstring>struct Student
{
    char name[25];int score;
};#define N 20
Student s[N+10];struct Rule{
    bool operator()(const Student& s1,const Student& s2) const{
    if(s1.score != s2.score)return s1.score>s2.score;elsereturn strcmp(s1.name, s2.name)<0;}
};int main(int argc, const char * argv[]) {
    int n;cin>>n;for(int i=0;i<n;i++)cin>>s[i].name>>s[i].score;sort(s,s+n,Rule());for(int i=0;i<n;i++)cout<<s[i].name<<" "<<s[i].score<<endl;return 0;
}

055:分数线划定

在这里插入图片描述

#include <iostream>
using namespace std;
#include <algorithm>
#include <cmath>struct Volunteer
{
    int id;int score;
};
struct Rule{
    bool operator()(const Volunteer& v1,const Volunteer& v2) const{
    if(v1.score != v2.score)return v1.score>v2.score;elsereturn v1.id<v2.id;}
};
#define N 5000
Volunteer vol[N+10];int main(int argc, const char * argv[]) {
    int n,m;cin>>n>>m;for(int i=0;i<n;i++)cin>>vol[i].id>>vol[i].score;int rank = floor(m*1.5);sort(vol, vol+n, Rule());int score = vol[rank-1].score;Volunteer r;r.id = 10000;r.score = score;int count = upper_bound(vol,vol+n,r, Rule())-vol;cout<<score<<" "<<count<<endl;for(int i=0;i<count;i++)cout<<vol[i].id<<" "<<vol[i].score<<endl;return 0;
}

056:病人排队

在这里插入图片描述

#include <iostream>
using namespace std;
#include <algorithm>struct Patient
{
    char ID[15];int num;int age;
};
#define N 100
Patient p[N+10];struct Rule
{
    bool operator()(const Patient& p1,const Patient& p2) const{
    if(p1.age>=60&&p2.age>=60){
    if(p1.age!=p2.age)return p1.age > p2.age;elsereturn p1.num<p2.num;}else if(p1.age<60&&p2.age<60)return p1.num<p2.num;elsereturn p1.age>p2.age;}
};int main(int argc, const char * argv[]) {
    int n;cin>>n;for(int i=0;i<n;i++){
    cin>>p[i].ID>>p[i].age;p[i].num = i;}sort(p, p+n, Rule());for(int i=0;i<n;i++)cout<<p[i].ID<<endl;return 0;
}

057:mysort

在这里插入图片描述
在这里插入图片描述

#include <iostream>
using namespace std;
struct A {
    int nouse1;int nouse2;int n;
};
void mysort(void* a,int num,int size,int (*f)(const void *,const void *))
{
    char* c = (char*) a;for(int i=num-1;i>0;i--){
    for(int j=0;j<i;j++){
    char *p1 = c+j*size;char *p2 = c+(j+1)*size;if(f(p1,p2)>0){
    for(int k=0;k<size;k++){
    char tmp = *(p1+k);*(p1+k) = *(p2+k);*(p2+k) = tmp;}}}}
}
int MyCompare1( const void * e1,const void * e2) 
{
    int * p1 = (int * ) e1;int * p2 = (int * ) e2;return * p1 - * p2;
}
int MyCompare2( const void * e1,const void * e2) 
{
    int * p1 = (int * ) e1;int * p2 = (int * ) e2;if( (* p1 %10) - (* p2 % 10))return (* p1 %10) - (* p2 % 10);elsereturn * p1 - * p2;
}
int MyCompare3( const void * e1,const void * e2) 
{
    A * p1 = (A*) e1;A * p2 = (A*) e2;return p1->n - p2->n;
}
int a[20];
A b[20];
int main ()
{
    	int n;while(cin >> n) {
    for(int i = 0;i < n; ++i) {
    cin >> a[i];b[i].n = a[i];}mysort(a,n,sizeof(int),MyCompare1);for(int i = 0;i < n; ++i) cout << a[i] << "," ;cout << endl;mysort(a,n,sizeof(int),MyCompare2);for(int i = 0;i < n; ++i) cout << a[i] << "," ;cout << endl;mysort(b,n,sizeof(A),MyCompare3);for(int i = 0;i < n; ++i) cout << b[i].n << "," ;cout << endl;}return 0;
}

058:从字符串中取数

在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <iomanip>
using namespace std;
double GetDoubleFromString(char * str)
{
    
static char *start;if(str)start = str;for(;*start&&!isdigit(*start);start++);char *q;if(!*start)return -1;q = start;for(;*start&&(isdigit(*start)||*start=='.');start++);if(*start){
    *start = 0;start++;}return atof(q);
}int main()
{
    char line[300];while(cin.getline(line,280)) {
    double n;n = GetDoubleFromString(line);while( n > 0) {
    cout << fixed << setprecision(6) << n << endl;n = GetDoubleFromString(NULL);}}return 0;
}
  相关解决方案