华为OJ平台试题 —— 字符串:名字的漂亮度

来源:互联网 发布:精通python网络爬虫pdf 编辑:程序博客网 时间:2024/05/22 14:14

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; background-color: rgb(255, 255, 255);">1、名字的漂亮度</span>



代码:

<pre name="code" class="cpp">

#include<stdio.h>/* * 对数组a 进行排序 */ void sort(int a[], int n){     int i, j, temp;          for( i = 0; i < n; i++)     for(j = i; j < n ; j++)     {     if(a[i] < a[j])     {     temp = a[i];     a[i] = a[j];     a[j] = temp;     }     }}int main(void){int n;  /* 名字个数 */ scanf("%d",&n);while(n--){char str[100];int  a[26]={0},     /* 名字中每个字符的个数组成的数组 */  i = 0, result = 0;    /* 漂亮度µ  */ scanf("%s",str);    /* 输入名字  */ <img src="http://img.blog.csdn.net/20150815100447310" alt="" />while(str[i] != '\0'){if(str[i] >= 'a' && str[i] <= 'z'){a[str[i]-'a']++;}if(str[i]>='A'&&str[i]<='Z'){a[str[i]-'A']++;}i++;}sort(a,26); /* 排序 *//* 对出现次数最多的字母给予最大的权值即26,其他的依次递减  */ for(i=0;i<=25;i++){result+=(26-i)*a[i];}printf("%d\n",result);}return 0;}

0 0
原创粉丝点击