c++ code:(5)sort
/*
程序填空题,自己编写排序函数 mysort,使得其能够对任意类型的数组排序
输入:
多组数据。每组数据以整数 n开头(n<10),然后是n个整数
输出:
对每组数据,输出三行。
第一行是整数从小倒大排序的结果
第二行是按个位数从小到大排序的结果(个位数相同,则小的排在前面)
第三行还是整数从小倒大排序的结果
样例输入:
5 21 3 76 48 445
6 73 29 45 8737 2 1
样例输出
3,21,48,76,445,
21,3,445,76,48,
3,21,48,76,445,
1,2,29,45,73,8737,
1,2,73,45,8737,29,
1,2,29,45,73,8737,
*/
#include <iostream>
using namespace std;
struct A {
int nouse1;
int nouse2;
int n;
};
//your code starts here
//bubble sort
void mysort(void * _array, int memnum, int typenum, int (*compare)(const void *,const void *))
{
char * poi = (char *)_array;
for (int i = memnum-1; i >=0; i--)
{
for (int j = 0; j<i; j++)
{
char * p1 = (char *)_array + j*typenum;
char * p2 = (char *)_array + j*typenum+ typenum;
int tmp = compare(p1,p2);
if (tmp>0)
{
for (int k = 0; k < typenum; ++k)
{
char element= p1[k];
p1[k] = p2[k];
p2[k] = element;
}
}
}
}
}
//your code ends here
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()
{
freopen("f:\\freopen.txt", "r", stdin);
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;
}
//*****************This version can pass openjudge test
#include <iostream>
#include <cstring>
using namespace std;
struct Student{
char name[30];
int score;
} students[30];
int main()
{
//freopen("f:\\freopen.txt", "r", stdin);
int n;
cin >> n;
for(int i = 0;i < n; ++i)
cin >> students[i].name >> students[i].score;
for(int i = n-1; i >= 0; --i)
for(int j = 0; j < i; ++ j)
if( students[j].score < students[j+1].score ||
students[j].score == students[j+1].score &&
strcmp( students[j].name ,students[j+1].name ) > 0) {
Student tmp;
tmp = students[j];
students[j] = students[j+1];
students[j+1] = tmp;
}
for(int i = 0;i < n; ++i)
cout << students[i].name<< " " << students[i].score<< endl;
return 0;
}
//*************************This version can't pass openjudge test but can't figure out
//5.4 2019
#include<iostream>
using namespace std;
struct student
{
char name[20 + 10];
short score;
};
typedef struct student stu;
int compare1(const void * a, const void * b)
{
return (*(stu *)a).score < (*(stu *)b).score;
}
int compare2(const void * a, const void * b)
{
if ((*(stu *)a).score == (*(stu *)b).score)
return (*(stu *)a).name < (*(stu *)b).name;
}
int main()
{
freopen("f:\\freopen.txt", "r", stdin);
short num;
cin >> num;
stu group[20+5];
for (short i = 0; i < num; i++)
{
cin>>group[i].name;
cin >> group[i].score;
}
qsort(group, num, sizeof(stu), compare1);
qsort(group, num, sizeof(stu), compare2);
for (short i = 0; i < num; i++)
{
cout << group[i].name << " ";
cout << group[i].score << endl;
}
return 0;
}
/*
编写GetDoubleFromString函数,该函数可以不断从字符串中取出非负浮点数,再无浮点数可取,则返回值小于0
输入样例
please 121a1 stand 0.7 9.2 1010.3983 0.00001 black stand what 1324.3
12.34 45 78ab78.34
输出样例
121.000000
1.000000
0.700000
9.200000
1010.398300
0.000010
1324.300000
12.340000
45.000000
78.000000
78.340000
*/
#include <iostream>
#include <iomanip>
using namespace std;
double GetDoubleFromString(char * str)
{
//your code starts here
static char * p;
if( str )
p = str;
double num = 0;
while( *p && !(*p >= '0' && *p <= '9'))
++p;
if( *p == 0)
return -1;
while( *p >= '0' && *p <= '9' ) {
num = num * 10 + *p - '0';
++p;
}
if( *p == '.') {
++p;
double i = 10;
while( *p >= '0' && *p <= '9' ) {
num += (*p-'0') / i;
++p;
i*=10;
}
}
return num;
//your code ends here
}
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;
}