給你一列文字,請你找出各字元出現的次數。
輸入說明 :
每筆測試資料一列。每列最大長度為1000。
輸出說明 :
對每一列輸入,請輸出各字元的ASCII值及其出現的次數。請根據出現的次數由小到大輸出。如果有2個以上的字元有相同的次數,則ASCII值較大的先輸出。 測試資料間請空一列,參考Sample Output
範例輸入 :
AAABBC
122333
範例輸出 :
67 1
66 2
65 3
49 1
50 2
51 3
出處 :
ACM 10062
PS:要注意讀入時要包含空格 && 最後一行下面只能有一行換行不能有2行 在ACM會錯
程式碼 :
#include<stdio.h>
int main()
{
int n,top,i,j,temp,check;
char s[1001],ctemp;
char arr1[1000];
int arr[1000];
check = 0;
while(gets(s)!=NULL)
{
if(check == 0)
check = 1;
else
printf("\n");
top = 0;
for(i=0; s[i]!='\0'; i++)
{
for(j=0; j<top; j++)
if(s[i] == arr1[j])
{
arr[j]++;
break;
}
if(j == top)
{
top++;
arr1[j] = s[i];
arr[j] = 1;
}
}
for(i=0; i<top; i++)
for(j=i+1; j<top; j++)
if((arr[i] > arr[j]) || (arr[i] == arr[j]) && (arr1[i] < arr1[j]))
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
ctemp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = ctemp;
}
for(i=0; i<top; i++)
printf("%d %d\n",arr1[i],arr[i]);
}
return 0;
}
http://zerojudge.tw/ShowProblem?problemid=c012
沒有留言:
張貼留言