1. 程式人生 > >hdu 1860&&九度1021

hdu 1860&&九度1021

題目描述:
    統計一個給定字串中指定的字元出現的次數。
輸入:
    測試輸入包含若干測試用例,每個測試用例包含2行,第1行為一個長度不超過5的字串,第2行為一個長度不超過80的字串。注意這裡的字串包含空格,即空格也可能是要求被統計的字元之一。當讀到'#'時輸入結束,相應的結果不要輸出。
輸出:
    對每個測試用例,統計第1行中字串的每個字元在第2行字串中出現的次數,按如下格式輸出:
    c0 n0
    c1 n1
    c2 n2
    ... 
    其中ci是第1行中第i個字元,ni是ci出現的次數。
樣例輸入:
I
THIS IS A TEST
i ng
this is a long test string
#
樣例輸出:
I 2
i 3
  5
n 2

g 2

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
	char str1[10],str2[105];
	int n[5];
	while(1)
	{
		gets(str1);
		if(strcmp(str1,"#")==0)
			break;
		//getline(cin,str1,'#');
		//cin>>str2;
		gets(str2);
		for(int i=0;i<strlen(str1);++i)
		{
			n[i]=0;
			for(int j=0;j<strlen(str2);++j)
			{
				if(str1[i]==str2[j])
					n[i]++;
			}
			cout<<str1[i]<<" "<<n[i]<<endl;
		}
	}
	return 0;
}