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