首页 > 编程语言 > 详细

C语言 完美字符串

时间:2017-01-04 12:36:31      阅读:244      评论:0      收藏:0      [点我收藏+]
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10000
 4 /*
 5                        1.把大写字母转换成小写字母
 6                        2.统计各个字母出现的次数
 7                        3.将最大的*26,其他以此减少*
 8                        4.相加输出结果
 9 */
10 int input(char s[]);
11 void transfer(char s[],int n);
12 int getCNT(int cnt[],char s[],int n);
13 void sort(int cnt[]);
14 int main()
15 {
16     char s[N];
17     int n,cnt[200]={0};
18     int i,j=26,perfect=0;
19     //printf("请输入测试字符串:\n");
20     n=input(s);
21     transfer(s,n);
22     getCNT(cnt,s,n);
23     sort(cnt);
24     for(i=a;i<=z;i++)
25         {
26             perfect+=cnt[i]*j;
27             j--;
28         }
29     printf("%d",perfect);
30 }
31 int input(char s[])
32 {
33     int i=-1;
34     do{
35         i++;
36         scanf("%c",&s[i]);
37     }while(s[i]!=\n);
38     return i;
39 }
40 void transfer(char s[],int n)
41 {
42     int i;
43     for(i=0;i<n;i++)
44         if(s[i]>=A&&s[i]<=Z)
45             s[i]+=32;
46 }
47 int getCNT(int cnt[],char s[],int n)
48 {
49     int i,j;
50     for(i=0;i<n;i++)
51     {
52         for(j=a;j<=z;j++)
53             if(s[i]==j)
54             cnt[j]++;
55     }
56 }
57 void sort(int cnt[])
58 {
59     int i,j;
60     int temp,maxIndex;
61     for(i=a;i<z;i++)
62     {
63         maxIndex=i;
64         for(j=i+1;j<=z;j++)
65             if(cnt[j]>cnt[maxIndex])
66                     maxIndex=j;
67             if(maxIndex!=i)
68                 {
69                 temp=cnt[i];
70                 cnt[i]=cnt[maxIndex];
71                 cnt[maxIndex]=temp;
72                 }
73     }
74 }

 

C语言 完美字符串

原文:http://www.cnblogs.com/jianf/p/6248115.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!