001:成绩排序
#include <iostream>
#include<cstring>
using namespace std;
#pragma warning(disable:4996)
struct Student {
char name[100];
int score;
};
const int NUM = 20;
Student stu[NUM];
void SelectSort(Student* s, int size) {
for (int i = 0;i < size-1;i++) {
int maxIndex = i;
for (int j = i + 1;j < size;j++) {
if (s[maxIndex].score < s[j].score)
maxIndex = j;
else if (s[maxIndex].score == s[j].score)
if (strcmp(s[maxIndex].name, s[j].name) > 0)
maxIndex = j;
}
Student temp = s[i];
s[i] = s[maxIndex];
s[maxIndex] = temp;
}
}
void OutPut(Student* s, int size) {
for (int i = 0;i < size;i++)
cout << s[i].name << ' ' << s[i].score << endl;
}
int main()
{
//freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
int n;
cin >> n;
for (int i = 0;i < n;i++)
cin >> stu[i].name >> stu[i].score;
SelectSort(stu, n);
OutPut(stu, n);
return 0;
}
002:分数线划定
#include <iostream>
#include<cstring>
using namespace std;
#pragma warning(disable:4996)
struct Candidate
{
int id;
int score;
};
const int NUM = 5000;
Candidate people[NUM];
void Input(Candidate* c, int size) {
for (int i = 0;i < size;i++)
cin >> c[i].id >> c[i].score;
}
void InsertSort(Candidate* c, int size) {
for (int i = 1;i < size;i++)
for (int j = 0;j < i;j++)
if (c[i].score > c[j].score||c[i].score==c[j].score&&c[i].id<c[j].id) {
Candidate temp = c[i];
for (int k = i;k > j;k--)
c[k] = c[k - 1];
c[j] = temp;
break;
}
}
int main()
{
//freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
int n, m;
cin >> n >> m;
Input(people, n);
InsertSort(people, n);
int critetia = people[int(m * 1.5)-1].score,count=0;
for (int i = 0;people[i].score >= critetia && i < n;i++)
count++;
cout << critetia << ' ' << count << endl;
for (int i = 0;people[i].score >= critetia && i < n;i++)
cout << people[i].id << ' ' << people[i].score << endl;
return 0;
}
003:病人排队
#include <iostream>
#include<cstring>
using namespace std;
#pragma warning(disable:4996)
struct Patient
{
char id[10];
int age;
};
const int NUM = 100;
Patient p[NUM];
void Input(Patient* p, int size) {
for (int i = 0;i < size;i++)
cin >> p[i].id >> p[i].age;
}
bool rule(Patient p1, Patient p2) {
if (p1.age >= 60)
if (p2.age < 60)
return true;
else if (p1.age > p2.age)
return true;
return false;
}
void BubbleSort(Patient* p, int size) {
for (int i =0;i <size-1;i++)
for (int j = size-1;j >i;j--)
if (rule(p[j], p[j-1])) {
Patient temp = p[j-1];
p[j - 1] = p[j];
p[j] = temp;
}
}
int main()
{
//freopen("C:\\Users\\czh\\Desktop\\2.txt", "r", stdin);
int n;
cin >> n;
Input(p, n);
BubbleSort(p, n);
for (int i = 0;i < n;i++)
cout << p[i].id << endl;
return 0;
}
004:mysort
void mysort(void* base, int nelem, int ElemSzie, int(*compare)(const void* e1, const void* e2)) {
char* last = (char*)base + (nelem - 1) * ElemSzie;
for (char* p = (char*)base;p < last;) {
char* Min = p;
for (char* q = p + ElemSzie;q <= last;q += ElemSzie)
if (compare(Min, q) > 0)
Min = q;
for (int i = 0;i < ElemSzie;i++) {
char temp = *p;
*p = *Min;
*Min = temp;
p++, Min++;
}
}
}
005:从字符串中取数
double GetDoubleFromString(char* str)
{
// Your Code Here
static char* p;
if (str)
p = str;
for (;*p && !(isdigit(*p) || *p == '.');p++);
if (*p == 0)
return -1;
char* temp = p;
for (;*p && (isdigit(*p) || *p == '.');p++);
if (*p)
*p++ = '\0';
return atof(temp);
}