c++恶心的char和string
统计数字出现次数。
Char的长度Strlen(char) string的长度.size()函数与.length()
Char和string都可以==比较。注意
string a=”1adbcde”,而a[0]是char类型需要转换。
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main() {
int n;
char x;
cin >> n >> x;
int s = 0;
string sNum = "";
for (int i = 1; i <= n; i++) {
sNum = to_string(i);
for (int j = 0; j < sNum.length(); j++) {
if (sNum[j]==x)
s++;
}
}
cout << s;
return 0;
}
//#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
string x;
cin >> n >> x;
int s = 0;
string sNum = "";
string stemp = "";
for (int i = 1; i <= n; i++) {
sNum = to_string(i);
for (int j = 0; j < sNum.length(); j++) {
stemp = sNum[j];
if (stemp ==x)
s++;
}
}
cout << s;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[128]={0};
char s[]="a1dsgajklbxcbvojhad";
for(int i=0;s[i];i++)
a[s[i]]++; //统计字符个数
for(int j=0;j<128;j++)
if (a[j])
cout<<(char)j<<":"<<a[j]<<endl; //输出相应的字符和出现次数
}