计算名字漂亮度

给出一个名字,该名字有26个字符串组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和。
每个字母都有一个“漂亮度”,范围在1到26之间。没有任何两个字母拥有相同的“漂亮度”。字母忽略大小写。
给出多个名字,计算每个名字最大可能的“漂亮度”。
 
知识点: 字符串 
题目来源: 内部整理 
练习阶段: 初级 
运行时间限制: 无限制
内存限制: 无限制
输入:  
整数N,后续N个名字
N个字符串,每个表示一个名字

输出:  
每个名称可能的最大漂亮程度
 
样例输入:
2
zhangsan
lisi
                   
样例输出:
192

101


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node
{
    char ch;
    int cnt;
    struct Node *next;
}node;

node *createlist()
{
    node *strList=(node*)malloc(sizeof(node));
    strList->next=NULL;
    return strList;
}

int getlength(node *head)
{
    int cnt=0;
    node *p=head;
    while(p->next!=NULL)
    {
        cnt++;
        p=p->next;
    }
    return cnt;
}

void insertChar(node *head,char ch)
{
    node *p=head;
    if(p==NULL)
        return;
    while(p->next!=NULL)
    {
        p=p->next;
        if(ch==p->ch)
        {
            p->cnt++;
            return;
        }
    }
    node *newNode=(node*)malloc(sizeof(node));
    newNode->ch=ch;
    newNode->cnt=1;
    p->next=newNode;
    newNode->next=NULL;
}

void sortList(node *head)
{
    int i,j,tempCnt;
    char tempChar;
    int len=getlength(head);
    node *p1,*p2;
    if(len>1)
    {
        p1=head->next;
        p2=p1->next;
        for(i=0;i<len;i++)
        {
            p2=p1->next;
            for(j=i+1;j<len;j++)
            {
                if(p1->cnt<p2->cnt)
                {
                    tempCnt=p2->cnt;
                    tempChar=p2->ch;
                    p2->cnt=p1->cnt;
                    p2->ch=p1->ch;
                    p1->cnt=tempCnt;
                    p1->ch=tempChar;
                }
                p2=p2->next;
            }
            p1=p1->next;
        }
    }
}

int getBeatuScore(char *str)
{
    node *head=createlist();
    node *p;
    int lenOfstr=strlen(str);
    int i,scoreSum,score;
    scoreSum=0;
    score=26;
    for(i=0;i<lenOfstr;i++)
    {
        insertChar(head,*(str+i));
    }
    sortList(head);
    p=head->next;
    while(p!=NULL)
    {
        scoreSum+=p->cnt*score;
        p=p->next;
        score--;
    }
    return scoreSum;
}

int main()
{
    int i,n;
    char name[100];
    int *score;
    scanf("%d",&n);
    score=(int*)malloc(sizeof(int)*(n+1));
    for(i=0;i<n;i++)
    {
        scanf("%s",name);
        *(score+i)=getBeatuScore(name);
    }
    for(i=0;i<n;i++)
    {
        printf("%d\n",*(score+i));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值