您的位置:首页 > 其它

程序设计与算法 第十周测验

2016-10-24 17:31 357 查看

1:成绩排序

#include <iostream>
#include <cstring>
using namespace std;
#define MAX_NUM 20
#define MAX_NAME 20
struct Student {
char name[MAX_NAME+1];
int score;
};
int main(void)
{
Student student[MAX_NUM] = {{'\0',-1}};
int n = 0;
cin >> n;
for(int c=0; c<n; c++)
{
Student s = {{'\0',-1}};
cin >> s.name >> s.score;
int p = c;
for(int i=0; i<c; i++)
{
if(student[i].score < s.score
|| (student[i].score == s.score && strcmp(student[i].name, s.name)>0))
{
p = i;
for(int j=c; j>i; j--)
student[j] = student[j-1];
break;
}
}
student[p] = s;
}
for(int c=0; c<n; c++)
cout << student[c].name << " " << student[c].score << endl;
return 0;
}


2:分数线划定

#include <iostream>
using namespace std;
struct Competitor {
int k;
int s;
};
#define MAX_NUM 5000
int main(void)
{
int n = 0;
int m = 0;
Competitor competitor[MAX_NUM] = {{0,0}};
cin >> n >> m;
for(int c=0; c<n; c++)
{
Competitor comp = {0,0};
cin >> comp.k >> comp.s;
int p = c;
for(int i=0; i<c; i++)
{
if(competitor[i].s < comp.s
|| (competitor[i].s == comp.s && competitor[i].k > comp.k))
{
p = i;
for(int j=c; j>i; j--)
competitor[j] = competitor[j-1];
break;
}
}
competitor[p] = comp;
}
int score = competitor[m*150/100 - 1].s;
int cnt = 0;
for(cnt=0;cnt<n && competitor[cnt].s>=score;cnt++);
cout << score << " " << cnt << endl;
for(int i=0;i<cnt;i++)
cout << competitor[i].k << " " << competitor[i].s << endl;
return 0;
}


3:病人排队

#include <iostream>
using namespace std;
#define MAX_NAME 10
#define MAX_NUM 100
struct Sick {
char id[MAX_NAME];
int age;
};
int main(void)
{
int n = 0;
int m = 0;
Sick sick_old[MAX_NUM] = {{'\0',0}};
Sick sick_young[MAX_NUM] = {{'\0',0}};
int yi = 0;
int oi = 0;
cin >> n;
for(int c=0; c<n; c++)
{
Sick sk = {'\0',0};
cin >> sk.id >> sk.age;
if (sk.age < 60)
{
sick_young[yi++] = sk;
}
else
{
oi++;
int p = c;
for(int i=0; i<c; i++)
{
if(sick_old[i].age < sk.age)
{
p = i;
for(int j=c; j>i; j--)
sick_old[j] = sick_old[j-1];
break;
}
}
sick_old[p] = sk;
}
}
for(int i=0;i<oi;i++)
cout << sick_old[i].id << endl;
for(int i=0;i<yi;i++)
cout << sick_young[i].id << endl;
return 0;
}


4:mysort

#include <iostream>
using namespace std;
struct A {
int nouse1;
int nouse2;
int n;
};
// 在此处补充你的代码
void mysort(void *list_in, int len, int size, int (*func)(const void *,const void *))
{
char *list = (char *)list_in;
for (int i = len-1; i > 0; --i)
{
for(int j = 0; j < i; ++j)
{
if(func(list+j*size, list+(j+1)*size)>0)
{
for(int c=0; c<size; c++)
{
char temp = *(list+j*size+c);
*(list+j*size+c) = *(list+(j+1)*size+c);
*(list+(j+1)*size+c) = temp;
}
}
}
}
}
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);
else
return * 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;
}


5:从字符串中取数

#include <iostream>
#include <iomanip>
using namespace std;
double GetDoubleFromString(char * str)
{
// 在此处补充你的代码
static char *s = NULL;
if(str != NULL)
{
s = str;
}
int f = 1;
double t = 0;
double n = 0;
for(;*s!='\0';s++)
{
if(*s!='.'&&(*s<'0'||'9'<*s) )
{
if(f==0)
break;
}
else if (*s=='.'&&*(s+1)=='.')
{
s++;
if(f==0)
break;
}
else if(*s=='.')
{
t=1;
}
else
{
f = 0;
if(t==0)
{
n = n*10 + (*s-'0');
}
else
{
t *= 10;
n += (*s-'0')/t;
}
}
}
if(f==1)
{
return -1;
}
else
{
return n;
}
}

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;
}


-eof-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法