3、计算字符串中含有不同字符串的个数
编写一个函数,计算字符串中含有不同的字符个数。字符串在ASCII码范围内(0~127,包括0和127),换行表示结束符,不算在字符里。不在范围内不做统计,多个相同的字符只计算一次,例如,对于字符串abaca而言,有a,b,c三种不同字符因此输出3。数据范围:1<=n<=500
具体实现
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
getline(cin, str);
int whitespace = 0;
int digits = 0;
int chars = 0;
int others = 0;
for(int i = 0;i < str.length(); i++){
if(isalpha(str[i])){
chars++;
}
else if(isdigit(str[i])){
digits++;
}
else if(isspace(str[i])){
whitespace++;
}
else
others++;
}
cout <<" chars :" << chars
<<" whitespace :" << whitespace
<<" digits :" << digits
<<" others :" << others <<endl;
return 0;
}
4、从键盘输入两个字符m和n,判断m和n的最小公倍数。 具体实现
#include <iostream>
using namespace std;
int main(){
int m,n;
while(cin >> m >> n){
int a = max(m,n);
while (1){
if(a%m == 0 && a%n == 0){
cout << a <<endl;
break;
}
a++;
}
}
return 0;
}