C语言程序设计算法题 -- lab13(1055 - 1058) SortIntegers/asy Sort Students/Count Word Occurrences/SortPoints

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值