11
STL用着就是方便!
#include
#include
#include
using namespace std;
struct student
{
int num;
int score;
};
bool cmp(const student& a,const student b)
{
if(a.score
return true;
else if(a.score==b.score&&a.num
return true;
return false;
}
int main()
{
int n;
cin>>n;
vector table(n);
for(int i=0;i
cin>>table[i].num>>table[i].score;
sort(table.begin(),table.end(),cmp);
for(int i=0;i
cout<
return 0;
}
发表于 2017-07-27 21:21:01
回复(2)
11
#include
int main(){
int p,q,n,a[101][101]={0};//构造二维数组并初始化为0
while(scanf("%d",&n)!=EOF){
for(int i=0;i
scanf("%d %d",&p,&q);
a[p][q]=1;
}//输入学号和成绩,并在对应的二维数组里存1
for(int i=0;i<101;i++)
for(int j=0;j
if(a[j][i])
printf("%d %d\n",j,i);
}//对二维数组里值为1的单元按列查找并输出其行值和列值(即按成绩从小到大的顺序输出学号和成绩)
return 0;
}
编辑于 2019-03-01 17:25:59
回复(6)
8
发表于 2016-01-17 20:54:05
回复(6)
9
python solution,use a dict . while True:
try:
a,d=int(input()),{}
for i in range(a):
x,y=map(int,input().split())
d[x]=y
for i in sorted(d.items(),key=lambda c:c[1]):
print(str(i[0])+" "+str(i[1]))
except:
break
发表于 2017-10-04 13:53:18
回复(1)
4
//这道题就是把二维数组展开到一维数组里,再用取整取余的方法计算出它的学号和成绩
#include
using namespace std;
int main()
{
int N;
while (cin >> N)
{
int num, score, s[10100] = {0};//共有0~100 101个分数,每个分数最多可能有100人,因此数组大小为10100
while (N--)
{
cin >> num >> score;
s[score*100 + num] = 1;//将学生的学号和成绩唯一地编码在数组下标中
}
for (int i = 0; i < 10099; i++)
if (s[i])
cout << i%100 << " " << i/100 << endl;
}
return 0;
}
编辑于 2019-01-08 12:53:08
回复(1)
3
C++中的库函数😁 //仍然使用algorithm自带的库函数sort,针对成绩相同再比较学号这种排序方式进行特别规定Compare
//用数组存放结构体!!
#include
#include
#include
using namespace std;
struct Student{
int number,grade;
};
const int maximum = 100;
Student array[maximum];
bool Compare(Student x,Student y){
if(x.grade==y.grade){
return x.number
}
else{
return x.grade
}
}
int main(){
int CaseNumber;
cin>>CaseNumber;
for(int i=0; i
scanf("%d%d",&array[i].number,&array[i].grade);
}
sort(array,array+CaseNumber,Compare);
for(int j=0; j
printf("%d %d\n",array[j].number,array[j].grade);
}
return 0;
}
发表于 2020-02-13 10:34:02
回复(0)
2
//这里用的冒泡,依然过了,实际上可以用vector table(n);和 sort(table.begin(),table.end(),cmp),时间复杂度应该为O(nlogn)。学到了!
#include
using namespace std;
struct student {
int id;
int grade;
};
bool compare(student a, student b) {
if (a.grade
return 1;
else if (a.grade>b.grade)
return 0;
else {
if (a.id
return 1;
else
return 0;
}
}
int main() {
int n;
while (cin >> n) {
student* stu = new student[n];
for (int i = 0; i
cin >> stu[i].id >> stu[i].grade;
for (int i = 0; i
for(int j=n-2;j>=i;j--)
if (compare(stu[j + 1], stu[j])) {
student c = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = c;
}
}
for (int i = 0; i
cout <
}
}
发表于 2020-01-11 20:36:56
回复(0)
2
#include
#include
#include
typedef struct Node{
int id;
int score;
} Node;
void swap(Node * a, Node * b)
{
Node tmp; tmp.id = a->id; tmp.score = a->score;
a->id = b->id; a->score = b->score;
b->id = tmp.id; b->score = tmp.score;
}
int main(void)
{
int num;
scanf("%d", &num);
Node * stus = (Node *)malloc(sizeof(Node) * num);
for(int i = 0; i < num; ++i)
{
scanf("%d %d", &(stus[i].id), &(stus[i].score) );
}
//bubble sort
for(int i = num - 1; i > 0; --i)
{
bool flag = false;
for(int j = 0; j < i; ++j)
{
if(stus[j].score > stus[j+1].score)
{
swap( &(stus[j]), &(stus[j+1]) );
flag = true;
}
else if(stus[j].score == stus[j+1].score && stus[j].id > stus[j+1].id)
{
swap( &(stus[j]), &(stus[j+1]) );
flag = true;
}
}
if(flag == false)
break;
}
for(int i = 0; i< num; ++i)
{
printf("%d %d\n", stus[i].id, stus[i].score);
}
free(stus);
return 0;
}
发表于 2019-03-08 18:30:02
回复(2)
2
//此代码思路是用TreeMap存储学生信息,通过将map转成list进行值排序,中间定义了一个比较器
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt(); //接收学生数量
Map map = new TreeMap<>(); //创建treemap存储数据
for(int i=0;i
map.put(sc.nextInt(),sc.nextInt());
} //将treemap转成list存储
List> list = new ArrayList(map.entrySet());
//将list进行排序,用了一个匿名内部类定义比较器
Collections.sort(list,new Comparator>(){
public int compare(Entry cp1,Entry cp2){
if(cp2.getValue() > cp1.getValue()){
return -1;
}else if(cp2.getValue().equals(cp1.getValue())){ //此else可以其实可以不写的
return cp2.getKey() > cp1.getKey() ? -1: 1; //因为treemap默认是键升序排列
}
return 1;
}
});
for(Map.Entry mapping : list){
System.out.println(mapping.getKey()+" "+mapping.getValue());
}
}
}
}
发表于 2018-08-04 08:23:45
回复(0)
2
通过重载小于号< ,不需要重写cmp函数
#include #include
using namespace std;
struct E
{
int num;
int score;
bool operator < (const E &b)const{
if(score!=b.score)return score
else return num
}
}buf[101];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i
{
scanf("%d %d",&buf[i].num,&buf[i].score);
}
}
sort(buf,buf+n);
for(int i=0;i
{
printf("%d %d\n",buf[i].num,buf[i].score);
}
return 0;
}
发表于 2018-01-09 17:24:20
回复(0)
2
//快速排序算法
#include
using namespace std;
int Partion(struct student * ptr, int low, int high);
void quickSort(struct student * ptr, int low, int high);
struct student {
int id;
int score;
};
bool compare(struct student * ptr1,struct student * ptr2){
if (ptr1->score > ptr2->score) {
return true;
}
else if (ptr1->score < ptr2->score) {
return false;
}
else {
if (ptr1->id >= ptr2->id) {
return true;
}
else {
return false;
}
}
}
int main(){
int n;
cin >> n;
struct student * ptr = new struct student[n];
for (int i= 0; i < n; i++) {
cin >> ptr[i].id >> ptr[i].score;
}
quickSort(ptr,0,n-1);
for (int i = 0; i< n; i++){
cout << ptr[i].id << " " <<
ptr[i].score << endl;
}
}
int Partion(struct student * ptr, int low, int high){
struct student temp = ptr[low];
while(high > low){
while(high > low &&
compare(ptr+high,&temp)){
high --;
}
ptr[low] = ptr[high];
while(high > low &&
compare(&temp,ptr+low)){
low ++;
}
ptr[high] = ptr[low];
}
ptr[low] = temp;
return low;
}
void quickSort(struct student * ptr, int low, int high) {
if (high > low){
int partion = Partion(ptr, low, high);
quickSort(ptr,low,partion-1);
quickSort(ptr,partion+1,high);
}
}
复杂度O(nlogn),用选择,冒泡等排序不知道会不会超时,不过一般用复杂度小一点的吧。另外,对于oj系统,也许可以直接调用库文件的排序算法,但是对于笔试算法等最好直接写算法吧,毕竟算法才是考察的重点。
一起加油!!!
发表于 2016-05-31 17:12:19
回复(0)
2
Java 解法一,使用面向对象的写法,清晰
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
ArrayList list = new ArrayList<>();
for (int i = 0; i
Collections.sort(list);
for (Student student : list) System.out.println(student);
}
public static class Student implements Comparable{
Integer id;
Integer score;
public Student(Integer id,Integer score){
this.id=id;
this.score=score;
}
@Override
public int compareTo(Student o) {
return this.score.equals(o.score) ?id.compareTo(o.id):score.compareTo(o.score);
}
@Override
public String toString() {
return id+" "+score;
}
}
} 解法二:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
ArrayList list = new ArrayList<>();
for (int i = 0; i
list.sort((o1, o2) -> {
String[] s1 = o1.split(" ");
Integer id1 = Integer.parseInt(s1[0]);
Integer score1= Integer.parseInt(s1[1]);
String[] s2 = o2.split(" ");
Integer score2= Integer.parseInt(s2[1]);
Integer id2 = Integer.parseInt(s2[0]);
return score1.equals(score2) ?id1.compareTo(id2):score1.compareTo(score2);
});
for (String s : list) System.out.println(s);
}
}
编辑于 2020-03-17 16:45:03
回复(1)
1
temp记录名词,如果相等判断学号,大的temp也加一,用新数组存成绩和学号,按照名次排序
#include int main()
{
int i,j,k,temp=0;
int a[100],b[100],c[100],d[100];
scanf("%d",&i);
for(j=0;j
{
scanf("%d %d",&a[j],&b[j]);
}
for(j=0;j
{
for(k=0;k
{
if(b[j]>b[k])temp++;
else if(b[j]==b[k]&&a[j]>a[k])temp++;
}
c[temp]=b[j];
d[temp]=a[j];
temp=0;
}
for(j=0;j
{
printf("%d %d\n",d[j],c[j]);
}
return 0;
}
发表于 2021-03-23 11:20:14
回复(0)
1
STL直接秒杀。 #include
using namespace std;
int main(){
int N;
cin>>N;
vector > data;
while(N--){
int p,q;
cin>>p>>q;
data.push_back(make_pair(p,q));
}
sort(data.begin(),data.end(),
[](pair& a,pair&b){
return a.second < b.second ||
(a.second == b.second && a.first < b.first);});
for(auto& e: data){
cout<
}
return 0;
}
发表于 2021-03-07 11:10:20
回复(0)
1
/*
*定义cmp ,vector排序。
*/
#include
using namespace std;
vector > v;
int n;
bool cmp(pair a, pair b)
{
if(a.second == b.second) return a.first < b.first;
return a.second < b.second;
}
int main()
{
int x, g;
while(scanf("%d",&n) == 1)
{
for(int i = 0;i < n; i++)
{
scanf("%d%d",&x,&g); v.push_back(make_pair(x, g));
}
sort(v.begin(), v.end(), cmp);
for(int i = 0;i < n; i++)
{
printf("%d %d\n",v[i].first,v[i].second);
}
}
return 0;
}
发表于 2021-01-19 15:05:33
回复(0)
1
#include
struct Student
{
int num,score;
}stu[100];
int main()
{
int N,i,j,p,q;
scanf("%d",&N);//输入
for(i=0;i
scanf("%d%d",&stu[i].num,&stu[i].score);
for(i=0;i
for(j=0;j
if(stu[j].score>stu[j+1].score)
{//交换
p=stu[j].num;q=stu[j].score;
stu[j].num=stu[j+1].num;stu[j].score=stu[j+1].score;
stu[j+1].num=p;stu[j+1].score=q;
}
for(i=0;i
for(j=0;j
if(stu[j].score==stu[j+1].score && stu[j].num>stu[j+1].num)
{
p=stu[j].num;stu[j].num=stu[j+1].num;stu[j+1].num=p;
}
for(i=0;i
printf("%d %d\n",stu[i].num,stu[i].score);
}
发表于 2020-03-21 17:28:53
回复(0)
1
用结构体,冒泡排序。
#include
using namespace std;
struct student{
int id;
int grade;
};
int main(){
student stu[100];
int N;
cin>>N;
for(int i=0;i
cin>>stu[i].id>>stu[i].grade;
}
for(int i=0;i
for(int j=0;j
if(stu[j].grade>stu[j+1].grade){
student item=stu[j];
stu[j]=stu[j+1];
stu[j+1]=item;
}
if(stu[j].grade==stu[j+1].grade){
if(stu[j].id>stu[j+1].id){
student item=stu[j];
stu[j]=stu[j+1];
stu[j+1]=item;
}
}
}
}
for(int i=0;i
cout<
}
return 0;
}
发表于 2020-03-19 19:46:26
回复(0)
1
/*STL就是好用*/
#include
(720)#include
#include
using namespace std;
struct student
{
int id;
int grade;
};
bool cmp(student a,student b)
{
return (a.grade==b.grade)?(a.id
}
int main()
{
int n,i;
while(cin>>n)
{
vector ve(n);
for(i=0;i
{
cin>>ve[i].id>>ve[i].grade;
}
sort(ve.begin(),ve.end(),cmp);
for(i=0;i
{
cout<
}
}
return 0;
}
发表于 2020-03-17 17:36:04
回复(0)
1
因为冒泡排序是稳定排序,所以我们先按照学号为主进行排序,之后再按照成绩为主排序即可
#include int main()
{int n,r;
scanf("%d",&n);
int a[n],b[n];
for(int i=0;i
scanf("%d %d",&a[i],&b[i]);
for(int i=1;i
for(int j=i;j>0;j--)
if(a[j]
{r=a[j];a[j]=a[j-1];a[j-1]=r;r=b[j];b[j]=b[j-1];b[j-1]=r;}
for(int i=1;i
for(int j=i;j>0;j--)
if(b[j]
{r=b[j];b[j]=b[j-1];b[j-1]=r;r=a[j];a[j]=a[j-1];a[j-1]=r;}
for(int i=0;i
printf("%d %d\n",a[i],b[i]);
}
发表于 2020-03-03 09:48:45
回复(0)
1
tips:1.用struct结构储存数据
2.做成普通排序题,我这里选用的选择排序
3.多加一种情况比较(同分数时,id靠前排在前面)
#include
using namespace std;
struct student {
int id;
int grade;
};
student s[100];
int main() {
int n;
while (cin >> n) {
for (int i = 0; i
cin >> s[i].id >> s[i].grade;
}
for (int i = 0; i
for (int j = i + 1; j
if (s[i].grade > s[j].grade) {
student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
if (s[i].grade == s[j].grade&&s[i].id > s[j].id) {
student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
for (int i = 0; i
cout <
}
}
return 0;
}
发表于 2020-02-17 19:58:19
回复(0)