疯狂的排序
描述
请你将一些数据按照编号排序。
输入
共n+1行,其中第一行为一个整数n,后n行为编号和数据。
输出
共n行,为排序后的数据。
输入样例
5
3 weww
2 wwww
4 sedf
1 frtgg
5 wess
输出样例
frtgg
wwww
weww
sedf
wess
看输出,不就是按前面的行号来排序吗?
#include <iostream>
#include <cstring>
using namespace std;
struct student{
int id;
string name;
}stu[10000];
void swap(int a,int b){
int t;
t = a;
a = b;
b = t;
}
void insertSort(int length)
{
for (int i = 1; i < length; i++)
{
for (int j = i - 1; j >= 0 && stu[j + 1].id < stu[j].id; j--)
{
swap(a[j], a[j + 1]);
}
}
}
int main(){
int times;
cin >> times;
for(int i = 0;i < times;i ++) cin >> stu[i].id >> stu[i].name;
insertsort(times);
for(int i = 0;i < times;i ++)
cout <<stu[i].name << endl;
}
差不多这个思路,我还没有调试过
用一个结构体解决同时储存编号和数据,我把他直接看成学生的学号和名字做
排序用的插入排序,这里貌似只需要长度
```python
import requests
url = 'http://cms-bucket.nosdn.127.net/2018/08/31/df39aac05e0b417a80487562cdf6ca40.png'
response = requests.get(url)
with open('北京房租地图.jpg', 'wb') as f:
f.write(response.content)
个人独立编写,调试完成,希望可以帮到你。
#include <stdio.h>
#define N 128
typedef struct{
int line;
char str[N];
}NODE; //因为在输入时一行内包含不同的数据类型,所以在此定义结构体类型方便存储数据
int main()
{
int count, i, j;
printf("please input count-->\n");
scanf("%d", &count); //输如数据的总行数
NODE node[count], t_node; //将每一行看作是一个结构体数组的元素,定义结构体数据
for(i=0; i<count; i++)
scanf("%d %s", &node[i].line, node[i].str); //对结构体数组的每一个元素进行赋值,包含行数和字符串
for(i=0; i<count; i++){
for(j=i+1; j<count; j++)
if(node[i].line > node[j].line){ //冒泡排序对行数的大小及逆行判断排序
t_node = node[j];
node[j] = node[i];
node[i] = t_node;
}
}
for(i=0; i<count; i++)
printf("%s\n", node[i].str);
return 0;
}
#include
#define N 128
typedef struct{
int id;
char name[N];
}NAME; //定义一个机构体,数字为id,对应的名字为name,
int main()
{
int num, i, j; //定义行数num和比较值i,j
printf("please input num:\n");
scanf("%d", &num);
NAME name[num] ,id;
for(i=0; i scanf("%d %s", &NAME.name[i], NAME.id);
for(i=0; i { char temp[num];
for(j=0;j {
if(name(i)>name(j+1))
{ temp = name(i);
name(i) = name(j+1);
name(j+1)=name(i);
}
}
}
for(i=0; i<num; i++)
printf("%s\n", NAME.id,NAME.name);
return 0;
} //没有 验证是否正确,但大体是这样的,
#include < iostream>
#include < list>
#include < vector>
using namespace std;
int main()
{
int n,seq;
string str = "";
vector n_list;
vector str_list,out_list;
n_list.clear();
str_list.clear();
out_list.clear();
std::cout << "*********** Input **********" << std::endl;
std::cin >> n;
for(int i=0;i<n;i++)
{
std::cin >> seq >> str;
n_list.push_back(seq);
str_list.push_back(str);
}
if(n_list.size() != str_list.size())
return 0;
out_list.resize(str_list.size());
for(size_t j=0;j<n_list.size();j++)
{
if((n_list[j]-1) < out_list.size())
{
out_list[(n_list[j]-1)] = str_list[j];
}
}
std::cout << "*********** Output **********" << std::endl;
for(size_t m=0;m<out_list.size();m++)
{
std::cout << out_list[m] << std::endl;
}
return 0;
}
c++ 代码,已验证结果对的。只是开始几个#include 我故意加了空格,不然系统网页显示不出来,你把代码拷进去,去掉前面#include 的几个空格就可以了。