当前位置: 首页 > news >正文

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

 

相关文章:

  • code 练习之路
  • 初见kopernio
  • c++ code:(7)basics2
  • c++ code:(10)operator overload
  • c++ code:(11)inheritance
  • c++ code:(12)polymorphism
  • c++ code:(13)io and template
  • 英文论文写作素材
  • 王梓坤院士的学术思想
  • 使用EI、SCI搜索 (使用 websci中的endnote online引用 )
  • 玩转word 和 mathtype 的结合
  • paper 格式
  • 计算思维
  • 程序中的一些思想
  • 深入 理解char * ,char ** ,char a[ ] ,char *a[] 的区别
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • Angular 响应式表单之下拉框
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • golang 发送GET和POST示例
  • IIS 10 PHP CGI 设置 PHP_INI_SCAN_DIR
  • IndexedDB
  • interface和setter,getter
  • JAVA之继承和多态
  • js ES6 求数组的交集,并集,还有差集
  • MD5加密原理解析及OC版原理实现
  • miaov-React 最佳入门
  • PHP 7 修改了什么呢 -- 2
  • Python中eval与exec的使用及区别
  • 分布式事物理论与实践
  • 构建二叉树进行数值数组的去重及优化
  • 关于使用markdown的方法(引自CSDN教程)
  • 简单基于spring的redis配置(单机和集群模式)
  • 思维导图—你不知道的JavaScript中卷
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • # Pytorch 中可以直接调用的Loss Functions总结:
  • #include到底该写在哪
  • (1)虚拟机的安装与使用,linux系统安装
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (转)Linux整合apache和tomcat构建Web服务器
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • *1 计算机基础和操作系统基础及几大协议
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .net core webapi Startup 注入ConfigurePrimaryHttpMessageHandler
  • .NET Core 成都线下面基会拉开序幕
  • .NET Core 中插件式开发实现
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .net framwork4.6操作MySQL报错Character set ‘utf8mb3‘ is not supported 解决方法
  • .NET 常见的偏门问题
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调