目录
1. 求最小公倍数
#include<iostream>
using namespace std;
int main() {
int a, b = 0;
cin >> a >> b;
if (a > b) {
int temp = a;
a = b;
b = temp;
}
int i = 1;
while ((b * i) % a != 0)
i++;
cout << a << "和" << b << "的最小公倍数为" << b * i;
return 0;
}
2.求一系列整数之和,个数也由io给出。
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
int sum = 0;
int num;
for (int i = 0; i < n; i++) {
cin >> num;
sum += num;
}
cout << n << "个整数之和为:" << sum << endl;
return 0;
}
3
1
22
34
3个整数之和为:57
3.读文本统计单词个数(用到了map和regex)
#include<map>
#include<fstream>
#include<iostream>
#include<string>
#include<regex>
using namespace std;
void count(map<string, int> wamp) {
map<string, int>::iterator it;
for (it = wamp.begin(); it !=wamp.end(); it++)
cout <<"("<< it->first<<"," << it->second<<")" << endl;
}
int main() {
ifstream is("D:/word.txt");
string mStr;
map<string,int> mMAP;
regex regWord("[a-zA-Z]+");
while (getline(is, mStr)) {
const std::sregex_token_iterator end;
for (sregex_token_iterator wordIter(mStr.begin(), mStr.end(), regWord); wordIter != end; wordIter++){
mMAP[*wordIter]++;
/*cout << *wordIter << endl;迭代器不是指针,是类模板,表现的像指针。
模拟了指针的一些功能,通过重载了指针的一些操作符,->,++ --等封装了指针,
是一个“可遍历STL( Standard Template Library)容器内全部或部分元素”的对象。*/
}
}
count(mMAP);
return 0;
}
Pattern pattern = Pattern.compile("[a-zA-Z]+");//“+”:表示可以有一对多
Pattern pattern = Pattern.compile("[0-9]+");
补充1:regex
补充2:map
4.Person类
#include<string>
#include<iostream>
using namespace std;
class Name {
private:
string first;
string middle;
string last;
public:
Name(string f = "", string m = "", string l = "") {
first = f;
middle = m;
last = l;
}
string printName()const {
cout << "名字:" << first << ends << middle << ends << last << endl;
return first + middle + last;
}
};
class Person {
private:
Name name;
string nationality;
string sex;
public:
Person(const Name &na, string n, string s) {
name = na;
nationality = n;
sex = s;
}
void printName() {
name.printName();
}
void printNationality() {
cout << "国籍:" << nationality << endl;
}
};
int main() {
Name n("Liu", "Ming", "Zhe");
Person p(n, "China", "Male");
n.printName();
p.printName();
p.printNationality();
}
名字:LiuMingZhe
名字:LiuMingZhe
国籍 : China
5.Conclusion
既没有考多态,也没有考继承,感觉像是少了两题。