1055 SortIntegers

#include<stdio.h>
#include<stdlib.h>
//********** Specification of Sort **********
int cmp(const void* A, const void* B)
{
unsigned* a = (unsigned*) A;
unsigned* b = (unsigned*) B;
unsigned aa = *a;
unsigned bb = *b;
int len_one_a = 0;
int len_one_b = 0;
while(aa || bb)
{
len_one_a += (aa & 1);
len_one_b += (bb & 1);
aa >>= 1;
bb >>= 1;
}
if(len_one_a == len_one_b)
{
if(*a > *b)// 防止溢出
return 1;
else
return -1;
}
else
return len_one_b - len_one_a;
}
void Sort(unsigned *p, unsigned n)
{
qsort(p, n, sizeof(unsigned), cmp);
}
int main()
{ unsigned n,i,a[1000]; scanf("%u",&n);
for (i=0;i<n;i++) scanf("%u",a+i); Sort(a,n);
for (i=0;i<n;i++) printf("%u%c",a[i],i!=n-1?' ':'\n');
return 0;
}
1056 asy Sort Students
/* PreCondition:
ps points to a Student array with n positive integers
PostCondition:
Array pointed by ps is sorted based on their mean scores in descending order.
If two students have identical mean scores, the order is based on student
numbers in ascending order.
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct {
long long num;
char name[15];
int score[3];
} STUDENT;
STUDENT* Input(int n)
{
STUDENT* p = (STUDENT*) malloc(sizeof(STUDENT) * (n + 10));
for(int i = 0; i < n; i++)
{
scanf("%lld", &(p + i)->num);
scanf("%s", (p + i)->name);
scanf("%d", &(p + i)->score[0]);
scanf("%d", &(p + i)->score[1]);
scanf("%d", &(p + i)->score[2]);
}
return p;
}
int cmp(const void* A, const void* B)
{
STUDENT* a = (STUDENT*) A;
STUDENT* b = (STUDENT*) B;
int sum_a = a->score[0] + a->score[1] + a->score[2];
int sum_b = b->score[0] + b->score[1] + b->score[2];
if(sum_a == sum_b)
{
if(a->num > b->num)//防止溢出
return 1;
else return -1;
}
else
return sum_b - sum_a;
}
void Sort(STUDENT *ps, int n)
{
qsort(ps, n, sizeof(STUDENT), cmp);
}
#define N 100000//
int main() {
STUDENT* a = NULL;
int i, n;
scanf("%d\n", &n);
a = Input(n);
Sort(a, n);
for (i = 0; i < n; i++)
printf("%lld %s %d %d %d\n", a[i].num, a[i].name,
a[i].score[0], a[i].score[1], a[i].score[2]);
return 0;
}
1057. Count Word Occurrences
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char* c;
int len;
}STR;
int cmp (const void* A, const void* B)
{
return strcmp(((STR*)A)->c, ((STR*)B)->c);
}
int main()
{
STR all[900000];
int cnt = 0;
char c;
char cc[500010];
int len = 0;
memset(cc, 0 ,sizeof(cc));
while(scanf("%s", cc) != EOF)
{
len = strlen(cc);
char* tmpc = (char*)malloc(sizeof(char) * len + 10);
strcpy(tmpc, cc);
all[cnt].len = len;
all[cnt++].c = tmpc;
}
/*
while((c = getchar()) != EOF)
{
if(c == ' ' || c == '\n' || c == '\t')
{
if(len == 0) continue;
else
{
char* tmpc = (char*)malloc(sizeof(char) * len + 10);
strcpy(tmpc, cc);
all[cnt++].c = tmpc;
memset(cc, 0 ,sizeof(cc));
len = 0;
}
}
else
{
cc[len++] = c;
}
}*/
qsort(all, cnt, sizeof(STR), cmp);
int occr = 1;
char* pc = all[0].c;
for(int i = 1; i < cnt; i++)
{
if(strcmp(pc, all[i].c) != 0)
{
printf("%s %d\n", all[i-1].c, occr);
occr = 0;
pc = all[i].c;
}
occr++;
}
printf("%s %d\n", all[cnt-1].c, occr);
return 0;
}
1058. SortPoints

#include<stdio.h>
#include<stdlib.h>
typedef long long ll;
typedef unsigned long long ull;
typedef struct{
ll x;
ll y;
ull MANHATTON;
}POINT;
ull ABS(ll a)
{
ull temp;
if(a >= 0) temp = a;
else
{
a++;
temp = -a; // 防止 -a溢出
temp++;
}
return temp;
}
int cmp(const void* A, const void* B)
{
POINT a = *(POINT*)A;
POINT b = *(POINT*)B;
if(a.MANHATTON == b.MANHATTON)
{
if(a.x == b.x)
{
if(a.y > b.y) return 1;
else return -1;
}
else
{
if(a.x > b.x)
return 1;
else return -1;
}
}
else
{
if(a.MANHATTON > b.MANHATTON)
return -1;
else return 1;
}
}
int main ()
{
POINT parray[1000];
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%lld %lld", &(parray[i].x), &(parray[i].y));
//printf("(%lld,%lld)", parray[i].x, parray[i].y);
parray[i].MANHATTON = ABS(parray[i].x) + ABS(parray[i].y);
}
qsort(parray, n, sizeof(POINT), cmp);
for(int i = 0 ; i < n; i++)
printf("(%lld,%lld)", parray[i].x, parray[i].y);
return 0;
}
本文介绍了三个C/C++编程题目,涉及整数排序(1055SortIntegers)、按成绩和编号排序学生信息(1056asySortStudents),以及单词计数(1057.CountWordOccurrences)。通过qsort函数展示了如何用自定义比较函数进行高效排序,并讨论了不同场景下的排序策略。

被折叠的 条评论
为什么被折叠?



