70,005
社区成员




输入:The job requires an agile mind.
输出
-5 e-4 i-3 r-2
a-2 n-2 T-1 h-1
j-1 o-1 b-1 q-1
u-1 s-1 g-1 l-1
m-1 d-1 .-1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char c;
int count;
}Letter;
void swap(Letter* x,Letter* y)
{
Letter t;
t.c=x->c;
t.count=x->count;
x->c=y->c;
x->count=y->count;
y->c=t.c;
y->count=t.count;
}
void sort(Letter* L,int k)
{
//冒泡排序
int i,j;
for(i=0;i<k-1;i++)
for(j=0;j<k-i-1;j++)
{
if(L[j].count<L[j+1].count)
swap(&L[j],&L[j+1]);
}
}
int main()
{
char buf[100];
Letter L[100];
int i,j,k=0;
memset(L,0,sizeof(L));
gets(buf);
for(i=0;i<strlen(buf);i++)
{
for(j=0;j<k;j++)
{
if(L[j].c==buf[i])
{
L[j].count++;
break;
}
}
if(j==k)
{
L[k].c=buf[i];
L[k].count=1;
k++;
}
}
sort(L,k);
for(i=0;i<k;i++)
{
printf("%c-%d ",L[i].c,L[i].count);
if((i+1)%4==0)printf("\n");
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT_NUM 100
typedef struct Node
{
char ch;
int count;
Node *next;
}CHAR_COUNT;
void GetCharCount(char *pStr, char ch, int *count)
{
char *ptr;
int num = 0;
ptr = strchr(pStr, ch);
while (ptr != NULL)
{
num++;
*ptr = -1;
ptr = strchr(ptr + 1, ch);
}
*count = num;
}
void Insert(CHAR_COUNT *head, char ch, int count)
{
CHAR_COUNT *pCC, *pTmp;
pCC = (CHAR_COUNT*)malloc(sizeof(CHAR_COUNT));
pCC->ch = ch;
pCC->count = count;
pCC->next = NULL;
if (head->next == NULL)
{
head->next = pCC;
}
else
{
pTmp = head;
while (pTmp->next != NULL)
{
if (pTmp->next->count >= count)
{
pTmp = pTmp->next;
continue;
}
pCC->next = pTmp->next;
pTmp->next = pCC;
return;
}
pTmp->next = pCC; // 插入到末尾
}
}
void Print(CHAR_COUNT *head)
{
CHAR_COUNT *pTmp;
int count = 0;
pTmp = head->next;
while (pTmp != NULL)
{
count++;
printf("%c-%d ", pTmp->ch, pTmp->count);
if (count % 4 == 0)
printf("\n");
pTmp = pTmp->next;
}
printf("\n");
}
void main()
{
char szInput[MAX_INPUT_NUM];
char ch;
int count;
CHAR_COUNT head;
int i;
head.next = NULL;
printf("输入字符串:\n");
gets(szInput);
for (i = 0; i < MAX_INPUT_NUM; i++)
{
ch = szInput[i];
if (ch == '\0')
break;
if (ch == -1)
continue;
GetCharCount(szInput, ch, &count);
Insert(&head, ch, count);
}
Print(&head);
system("pause");
}