1. 编写程序,求最小公倍数。
递归求最小公倍数:
int gcd(int a,int b)
{
if(a%b == 0)
return b;
return gcd(b, a%b);
}
int lcm(int a,int b)
{
return a*b/gcd(a,b);
}
定义求:
int gcd(int x, int y)
{
int greatest = 1;
for(int i = 2; i <= ((x<y)?x:y); i++)
{
if(x%i == 0 && y%i== 0)
greatest = i;
}
return greatest;
}
辗转相除:
int gcd(int a, int b)
{
int max = a > b ? a : b;
int min = a < b ? a : b;
int r;
while(min)
{
r = max%min;
max = min;
min = r;
}
return max;
}
2. 编写程序,计算一系列整数之和。假定:输入的第一个整数为连续输入整数的个数。
#include <iostream>
using namespace std;
int main()
{
int num;
cin >> num;
int sum = 0;
int x;
while(num)
{
cin >> x;
sum += x;
num -- ;
}
cout << endl << sum <<endl;
}
3. 编写程序,读入几行文本
- 统计文本中每个字母出现的次数
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
const int SIZE = 100;
int main()
{
int num;
cout << "Enter the number of text: ";
cin >> num;
char text[num][SIZE];
int letters[26] = {};
cout << "enter " << num << " lines of text:\n";
for(int i = 0; i <= num; ++i)
cin.getline(&text[i][0],SIZE);
for(int i = 0; i <= num; ++i)
{
for(int j = 0; text[i][j]!='\0';j++)
if(isalpha(text[i][j]))
letters[tolower(text[i][j]) - 'a']++;
}
cout << endl << "Total letter counts:\n";
for(int i = 0; i < 26; ++i)
// if(letters[i]) //只打印出现的字母
cout << setw(3) << static_cast<char> ('a' + i) << " : " << setw(3) << static_cast<int>(letters[i]) << endl;
return 0;
}
- 统计单词的长度以及不同单词长度的个数
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
using namespace std;
const int SIZE = 100;
int main()
{
int num;
cout << "Enter the number of text: ";
cin >> num;
char text[num][SIZE];
int lengths[20] = {};
cout << "enter " << num << " lines of text:\n";
for(int i = 0; i <= num; ++i)
cin.getline(&text[i][0],SIZE);
char *temp;
for(int i = 0; i <= num; ++i)
{
temp = strtok(&text[i][0],". \n");
while(temp)
{
lengths[strlen(temp)]++;
temp = strtok(NULL,". \n");//结束循环条件的
}
}
cout << endl;
cout << setw(10) << "length" << setw(10) << "times" << endl;
for(int i = 1; i < 20; i++)
{
if(lengths[i])
cout << setw(10) << i << setw(10) << lengths[i] << endl;
}
return 0;
}
- 统计不同单词在文中出现的次数:
#include <iostream>
#include <iomanip>
#include <cctype>
#include <cstring>
using namespace std;
const int SIZE = 100;
int main()
{
int num;
cout << "Enter the number of text: ";
cin >> num;
char text[num][SIZE];
char *temp;
int count[100]= {};
char words[100][20] = {""};
cout << "enter " << num << " lines of text:\n";
for(int i = 0; i <= num; ++i)
cin.getline(&text[i][0],SIZE);
for(int i = 0; i <= num; ++i)
{
temp = strtok(&text[i][0],". \n");
while(temp)
{
int j = 0;
for(j = 0; words[j][0] && strcmp(temp,&words[j][0])!= 0;j++);//word[j][0] 不为空时,且不等于temp
count[j] ++;
if(!words[j][0]) //words[j][0]为空时赋值
strcpy(&words[j][0],temp);
temp = strtok(NULL,". \n");
}
}
cout << endl;
cout << setw(20) << left << "word" << setw(10) << "times" << endl;
for(int i = 0; words[i][0]!= '\0' && i < 100 ; i++)
{
cout << setw(20) << left << &words[i][0] << setw(10) << count[i] << endl;
}
return 0;
}
4. Person类
包含三个成员数据(name,nationality和sex)和三个成员函数(构造函数,printName函数和printNationality函数),
其中name的数据类型为Name类。Name类包含三个数据成员(first,middle,last)和两个成员函数(构造函数和printName函数)
定义Person类和Name类,并编写程序测试这两个类的所有接口。
Name.h
#ifndef NAME_H
#define NAME_H
#include <string>
using namespace std;
class Name
{
public:
Name(string = "",string = "", string = "");
void printName()const;
virtual ~Name();
private:
string first;
string middle;
string last;
};
#endif // NAME_H
Name.cpp
#include "Name.h"
#include <iostream>
#include <string>
using namespace std;
Name::Name(string f,string m,string l)
:first(f),middle(m),last(l)
{
//ctor
}
void Name::printName()const
{
cout << "name is : " << first << middle << last << endl;
}
Name::~Name()
{
//dtor
}
Person.h
#ifndef PERSON_H
#define PERSON_H
#include "Name.h"
#include <string>
using namespace std;
class Person
{
public:
Person(Name &, string = "China",string = "male");
void printName()const
{
name.printName();
}
void printNationality()const;
virtual ~Person();
protected:
private:
Name name;
string nationality;
string sex;
};
#endif // PERSON_H
Person.cpp
#include "Person.h"
#include <iostream>
using namespace std;
Person::Person(Name &n,string na,string s):
name(n),nationality(na),sex(s)
{
//ctor
}
void Person::printNationality()const
{
cout << "nationality : " << nationality << endl;
}
Person::~Person()
{
//dtor
}
main.cpp
#include <iostream>
#include "Name.h"
#include "Person.h"
using namespace std;
int main()
{
Name name("王","苏","豪");
Person p(name,"China","男");
name.printName();
p.printName();
p.printNationality();
return 0;
}