C++程序设计(第3版)谭浩强 第5章 习题

1.用筛法求100之内的素数。

【解】

#include 
#include 
using namespace std;
#include 
int main()
{
	int i, j, n, a[101];	//定义a数组包含101个元素
	for (i = 1; i <= 100; i++)	//a[0]不用, 只用a[1]~a[100]
		a[i] = i;	//使a[1]~a[100]的值为1~100
	a[1] = 0;	//先“挖掉”a[1]
	for (i = 2; i < sqrt(100); i++)
		for (j = i + 1; j <= 100; j++)
		{
			if (a[i] != 0 && a[j] != 0)
				if (a[j] % a[i] == 0)
					a[j] = 0;
		}	//把非素数“挖掉”
	cout << endl;
	for (i = 1, n = 0; i <= 100; i++)
	{
		if (a[i] != 0)	//选出值不为0的数组元素, 即素数
		{
			cout << setw(5) << a[i] << " ";	//输出素数, 宽度为5列
			n++;
		}	//累计本行已输出的数据个数
		if (n == 10)	//输出10个数后换行
		{
			cout << endl;
			n = 0;
		}
	}
	cout << endl;
	return 0;
}

运行结果:

//2    3    5    7   11   13   17   19   23   29
//31   37   41   43   47   53   59   61   67   71
//73   79   83   89   97

2.用选择法对10个整数排序。

【解】

#include 
using namespace std;
int main()
{
	int i, j, min, temp, a[11];
	cout << "enter data:" << endl;
	for (i = 1; i <= 10; i++)
	{
		cout << "a[" << i << "]=";
		cin >> a[i];	//输入10个数 
	}
	cout << endl << "The original numbers:" << endl;
	for (i = 1; i <= 10; i++)
		cout << a[i] << " ";	//输出这10个数 
	cout << endl;
	for (i = 1; i <= 9; i++)	//以下8行是对10个数排序 
	{
		min = i;
		for (j = i + 1; j <= 10; j++)
			if (a[min] > a[j])  min = j;
		temp = a[i];	//以下3行将a[i+1]~a[10]中最小者与a[i]对换
		a[i] = a[min];
		a[min] = temp;
	}
	cout << endl << "The sorted numbers:" << endl;
	for (i = 1; i <= 10; i++)	//输出已排好序的10个数 
		cout << a[i] << " ";
	cout << endl;
	return 0;
}

运行结果:

//enter data :
//a[1] = 36 ↙
//a[2] = – 9↙
//a[3] = 5↙
//a[4] = 6↙
//a[5] = –1↙
//a[6] = 35↙
//a[7] = 34↙
//a[8] = 738↙
//a[9] = 18↙
//a[10] = 11↙

//The original numbers :
//36 –9 5 6 –1 35 34 738 18 11
//The sorted numbers :
//–9 –1 5 6 11 18 34 35 36 738

3.求一个3×3矩阵对角线元素之和。

【解】

#include 
using namespace std;
int main()
{
	int a[3][3], sum = 0;
	int i, j;
	cout << "enter data:" << endl;
	for (i = 0; i < 3; i++)
		for (j = 0; j < 3; j++)
			cin >> a[i][j];
	for (i = 0; i < 3; i++)
		sum = sum + a[i][i];
	cout << "sum=" << sum << endl;
	return 0;
}

运行结果:

//enter ata:
//1 3 5 7 9 11 13 15 17↙
//sum = 27

4.有一个已排好序的数组,今输入一个数,要求按原来排序的规律将它插入数组中。

【解】

#include 
using namespace std;
int main()
{
	int a[11] = { 1, 6, 13, 17, 28, 40, 56, 78, 89, 100 };
	int num, i, j;
	cout << "array a:" << endl;
	for (i = 0; i < 10; i++)
		cout << a[i] << " ";
	cout << endl;
	cout << "insert data:";
	cin >> num;
	if (num > a[9])
		a[10] = num;
	else
	{
		for (i = 0; i < 10; i++)
		{
			if (a[i] > num)
			{
				for (j = 9; j >= i; j--)
					a[j + 1] = a[j];
				a[i] = num;
				break;
			}
		}
	}
	cout << "Now, array a:" << endl;
	for (i = 0; i < 11; i++)
		cout << a[i] << " ";
	cout << endl;
	return 0;
}

运行结果:

//array a:
//1 6 13 17 28 40 56 78 89 100
//insert data : 15↙
//Now, array a :
//1 6 13 15 17 28 40 56 78 89 100

5.将一个数组中的值按逆序重新存放。例如,原来顺序为8,6,5,4,1。要求改为1,4,5,6,8。

【解】

#include 
using namespace std;
int main()
{
	const int n = 5;
	int a[n], i, temp;
	cout << "enter array a:" << endl;
	for (i = 0; i < n; i++)
		cin >> a[i];
	cout << "array a:" << endl;
	for (i = 0; i < n; i++)
		cout << a[i] << " ";
	for (i = 0; i < n / 2; i++)            //循环的作用是将对称的元素的值互换
	{
		temp = a[i];
		a[i] = a[n-i-1];
		a[n-i-1] = temp;
	}
	cout << endl << "Now,array a:" << endl;
	for (i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
	return 0;
}

运行结果:

//enter array a:
//9 8 7 6 5↙
//array a :
//9 8 7 6 5
//Now, array a :
//5 6 7 8 9

6.打印出以下的杨辉三角形(要求打印出10行)。

【解】

#include 
#include 
using namespace std;
int main()
{
	const int n = 11;
	int i, j, a[n][n];	//数组为11行11列, 0行0列不用
	for (i = 1; i < n; i++)
	{
		a[i][1] = 1;	//使第1列元素的值为1
		a[i][i] = 1;	//使对角线元素的值为1
	}
	for (i = 3; i < n; i++)	//从第3行开始处理
		for (j = 2; j <= i - 1; j++)
			a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
	for (i = 1; i < n; i++)	//输出数组各元素的值
	{
		for (j = 1; j <= i; j++)
			cout << setw(6) << a[i][j] << " ";
		cout << endl;
	}
	cout << endl;
	return 0;
}

运行结果:

//1
//1     1
//1     2     1
//1     3     3     1
//1     4     6     4     1
//1     5    10    10     5     1
//1     6    15    20    15     6     1
//1     7    21    35    35    21     7     1
//1     8    28    56    70    56    28     8     1
//1     9    36    84   126   126    84    36     9     1

7.找出一个二维数组中的鞍点,即该位置上的元素在该行上最大,在该列上最小(也可能没有鞍点)。

【解】

#include 
using namespace std;
int main()
{
	const int n = 4, m = 5;				//假设数组为4行5列     
	int i, j, a[n][m], max, maxj;
	bool flag;
	for (i = 0; i < n; i++)				//输入数组
		for (j = 0; j < m; j++)
			cin >> a[i][j];
	for (i = 0; i < n; i++)
	{
		max = a[i][0]; maxj = 0;			//开始时假设a[i][0]最大, 将列号(0)赋给maxj保存
		for (j = 0; j < m; j++)				//找出第i行中的最大数
			if (a[i][j] > max)
			{
				max = a[i][j];				//将本行的最大数存放在max中
				maxj = j;				//将最大数所在的列号存放在maxj中
			}
		flag = true;					//先假设是鞍点, 用flag为真来代表
		for (int k = 0; k < n; k++)
			if (max > a[k][maxj])			//将最大数和其同列元素相比
			{
				flag = false;				//如果max不是同列最小, 表示不是鞍点, 令flag为假
				continue;
			}
		if (flag)					//如果flag为真, 表示是鞍点
		{
			cout << "a[" << i << "][" << "[" << maxj << "]=" << max << endl;
			//输出鞍点的值和所在行列号
			break;
		}
	}
	if (!flag)						//如果flag为假表示鞍点不存在
		cout << "It does not exist!" << endl;
	return 0;
}

运行结果:

//① 1 2 3 4 5↙                    (输入4行5列数据)
//2 4 6 8 10↙
//3 6 9 12 15↙
//4 8 12 16 20↙
//a[0][4] = 5                    (0行4列元素是鞍点, 其值为5)
//② 1 12 3 4 5↙                    (输入4行5列数据)
//2 4 16 8 10↙
//3 6 9 12 15↙
//4 8 12 16 20↙
//It does not exist!(无鞍点)

8.有15个数按由大到小顺序存放在一个数组中,输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。如果该数不在数组中,则打印出“无此数”。

【解】

#include 
using namespace std;
int main()
{
	const int n = 7;
	int i, number, top, bott, mid, loca, a[n];
	bool flag = true, sign;
	char c;
	cout << "enter data:" << endl;
	cin >> a[0];
	i = 1;
	while (i < n)
	{
		cin >> a[i];
		if (a[i] >= a[i - 1])
			i++;
		else
			cout << "enter this data again:";
	}
	cout << endl;
	for (i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
	while (flag)
	{
		cout << "input number to look for:";
		cin >> number;
		sign = false;	//sign为假表示尚未找到
		top = 0;	//top是查找区间的起始位置
		bott = n - 1;	//bott是查找区间的最末位置
		if ((number < a[0]) || (number > a[n - 1]))	//要查的数不在查找区间内
			loca =  - 1;	//表示要查找的数不在正常范围内
		while ((!sign) && (top <= bott))
		{
			mid = (bott + top) / 2;	//找出中间元素的下标
			if (number == a[mid])	//如果要查找的数正好等于中间元素
			{
				loca = mid;	//记下该下标
				cout << "Find " << number << ", its position is " << loca + 1 << endl;
				//由于下标从0算起, 而人们习惯从1算起, 因此输出数的位置时要加1
				sign = true;	//表示找到了
			}
			else if (number < a[mid])	//如果要查找的数小于中间元素的值
				bott = mid - 1;	//只须从下标为0~mid - 1的范围中找
			else	//如果要查找的数不小于中间元素的值
				top = mid + 1;	//只须从下标为mid+1~bott的范围中找
		}
		if (!sign || loca ==  - 1)	//sign为假或loca等于 - 1, 意味着找不到
			cout << number << " has not found." << endl;;
		cout << "continue or not(Y/N)?";
		cin >> c;
		if (c == 'N' || c == 'n')
			flag = false;	//flag为开关变量, 控制程序是否结束运行
	}
	return 0;
}

运行结果:

//enter data : (输入数据)
//6 8 12 10↙    (数据未按由小到大顺序输入)
//enter this data again : (要求重新输入)
//23 34 44 45 56 57 58↙
//68 69 76 90 109↙
//6 8 12 23 34 44 45 56 57 58 68 69 76 90 109    (输出这15个数)
//input number to look for:7↙    (寻找7)
//7 has not found.(在数组中找不到7)
//Continue or not(Y / N) ? y↙    (需要继续查找)
//input number to look for : 12↙    (寻找12)
//12, its position is 3    (12是第3个数)
//Continue or not(Y / N) ? n↙    (不再继续查找)
//(运行结束)

9.给出年、月、日,计算该日是该年的第几天。

【解】

#include 
using namespace std;
int main()
{
	int sum_day(int, int);
	int leap(int year);
	int year, month, day, days;
	cout << "input date(year,month,day):";
	cin >> year >> month >> day;
	cout << year << "/" << month << "/" << day;
	days = sum_day(month, day);	//调用sum_day函数计算日期
	if (leap(year) && month >= 3)	//若year是闰年, 且月份在3月以后
		days = days + 1;	//days多加1天
	cout << " is the " << days << "th day in this year." << endl;
	return 0;
}
int sum_day(int month, int day)	//计算日期 
{
	int i;
	int day_tab[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	for (i = 0; i < month - 1; i++)
		day += day_tab[i];
	return(day);
}
int leap(int year)	//判断year是否为闰年 
{
	int leap;
	leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
	return(leap);
}

运行结果:

//input date(year, month, day) :2005 10 1↙
//2005 / 10 / 1 is the 274th day in this year.

10.有一篇文章,共有3行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。

【解】

#include 
using namespace std;
int main()
{
	int i, j, upper, lower, digit, space, other;
	char text[3][80];
	upper = lower = digit = space = other = 0;
	for (i = 0; i < 3; i++)
	{
		cout << "please input line " << i + 1 << endl;
		gets_s(text[i]);
		for (j = 0; j < 80 && text[i][j] != '\0'; j++)
		{
			if (text[i][j] >= 'A'&& text[i][j] <= 'Z')
				upper++;
			else if (text[i][j] >= 'a' && text[i][j] <= 'z')
				lower++;
			else if (text[i][j] >= '0' && text[i][j] <= '9')
				digit++;
			else if (text[i][j] == ' ')
				space++;
			else
				other++;
		}
	}
	cout << "upper case:" << upper << endl;
	cout << "lower case:" << lower << endl;
	cout << "digit     :" << digit << endl;
	cout << "space     :" << space << endl;
	cout << "other     :" << other << endl;
	return 0;
}

运行结果:

//please input line 1:
//I am a boy.↙
//please input line 2:
//You are a girl; ↙
//please input line 3:
//12 + 34 = 46↙
//upper case: 2
//lower case: 16
//digit : 6
//space : 6
//other : 4

11.打印以下图案:

【解】

//(1)用字符数组方法,程序如下:
#include 
using namespace std;
int main()
{
	char a[5] = { '*','*','*','*','*' };
	int i, j, k;
	const char space = ' ';
	for (i = 0; i < 5; i++)	//输出5行 
	{
		cout << endl;	//输出每行前先换行 
		cout << "    ";	//每行前面留4个空格 
		for (j = 1; j <= i; j++)
			cout << space;	//每行再留一个空格 
		for (k = 0; k < 5; k++)
			cout << a[k];	//每行输出5个*号
	}
	cout << endl;
	return 0;
}

//(2)用string方法,程序如下:
#include 
#include 
using namespace std;
int main()
{
	string stars = "*****";
	int i, j;
	for (i = 0; i < 5; i++)	//输出5行“*****”
	{
		cout << "    ";	//每行前面留4个空格
		for (j = 1; j <= i; j++)
			cout << " ";	//每行再插入i个空格
		cout << stars << endl;	//输出“*****”
	}
	return 0;
}

运行结果:

    *****
     *****
      *****
       *****
        *****

12.有一行电文,已按下面规律译成密码:

【解】

//(1)用字符数组方法
#include 
using namespace std;
int main()
{
	int j, n;
	char ch[80], tran[80];
	cout << "?input cipher code:";
	gets_s(ch);
	cout << "cipher code:" << ch << endl;
	j = 0;
	while (ch[j] != '\0')
	{
		if ((ch[j] >= 'A') && (ch[j] <= 'Z'))	//判断是否为大写字母
			tran[j] = 155 - ch[j];	//字母转换
		else if ((ch[j] >= 'a') && (ch[j] <= 'z'))	//判断是否为小写字母
			tran[j] = 219 - ch[j];	//字母转换
		else
			tran[j] = ch[j];	//非字母不转换
		j++;
	}
	n = j;	//n为电文中字符个数
	cout << "original text:";
	for (j = 0; j < n; j++)
		putchar(tran[j]);
	cout << endl;
	return 0;
}


//(2)也可以不必定义两个字符数组,而只用一个字符数组。程序如下:
#include 
using namespace std;
int main()
{
	int j, n;
	char ch[80];
	cout << "input cipher code:";
	gets_s(ch);
	cout << "cipher code:" << ch << endl;
	j = 0;
	while (ch[j] != '\0')
	{
		if ((ch[j] >= 'A') && (ch[j] <= 'Z'))
			ch[j] = 155 - ch[j];
		else if ((ch[j] >= 'a') && (ch[j] <= 'z'))
			ch[j] = 219 - ch[j];
		else
			ch[j] = ch[j];
		j++;
	}
	n = j;
	cout << "original text:";
	for (j = 0; j < n; j++)
		putchar(ch[j]);
	cout << endl;
	return 0;
}

//(3)用string方法
#include 
#include 
using namespace std;
int main()
{
	int j;
	string ch = "I will visit China next week.", tran;
	tran = ch;	//使tran也占同样大小的存储单元
	cout << "cipher code:" << ch << endl;
	j = 0;
	while (j <= ch.size())	//用 ch.size( )测定ch的长度
	{
		if ((ch[j] >= 'A') && (ch[j] <= 'Z'))
			tran[j] = 155 - ch[j];
		else if ((ch[j] >= 'a') && (ch[j] <= 'z'))
			tran[j] = 219 - ch[j];
		else
			tran[j] = ch[j];
		j++;
	}
	cout << "original text:";
	cout << tran << endl;
	return 0;
}

//(4)也可以只用一个字符串变量ch
#include 
#include 
using namespace std;
int main()
{
	int j;
	string ch = "I will visit China next week.";
	cout << "cipher code:" << ch << endl;
	j = 0;
	while (j <= ch.size())
	{
		if ((ch[j] >= 'A') && (ch[j] <= 'Z'))
			ch[j] = 155 - ch[j];
		else if ((ch[j] >= 'a') && (ch[j] <= 'z'))
			ch[j] = 219 - ch[j];
		j++;
	}
	cout << "original text:";
	cout << ch << endl;
	return 0;
}

//(5)使用字符串变量,由键盘输入电文。程序如下:
#include 
#include 
using namespace std;
int main()
{
	int j;
	string ch = "  ";	//ch的初始值为若干空格
	char *p = &ch[0];	//定义字符指针并使之指向ch的首字符
	cout << "input cipher code:";
	gets(p);	//从键盘读入一行字符
	cout << "cipher code:" << ch << endl;
	j = 0;
	while (j <= ch.size())
	{
		if ((ch[j] >= 'A') && (ch[j] <= 'Z'))
			ch[j] = 155 - ch[j];
		else if ((ch[j] >= 'a') && (ch[j] <= 'z'))
			ch[j] = 219 - ch[j];
		j++;
	}
	cout << "original text:";
	cout << ch << endl;
	return 0;
}

运行结果:
//input cipher code : R droo erhrg Xsrmz mvcg dvvp.↙    (输入电文)
//cipher code : R droo erhrg Xsrmz mvcg dvvp.(显示电文)
//original text : I will visit China next week.(译成原文)

13.编写一程序,将两个字符串连接起来,结果取代第一个字符串。

【解】

//(1)用字符数组,不用strcat函数(即自己编写一个具有strcat函数功能的函数);
#include 
using namespace std;
int main()
{
	char s1[80], s2[40];
	int i = 0, j = 0;
	cout << "input string1:";
	cin >> s1;
	cout << "input string2:";
	cin >> s2;
	while (s1[i] != '\0')
		i++;
	while (s2[j] != '\0')
		s1[i++] = s2[j++];
	s1[i] = '\0';
	cout << "The new string is: " << s1 << endl;
	return 0;
}


//(2)用标准库中的strcat函数
#include 
using namespace std;
int main()
{
	char s1[80], s2[40];
	cout << " input string1:";
	cin >> s1;
	cout << " input string2:";
	cin >> s2;
	strcat(s1, s2);
	cout << "?The new string is:" << s1 << endl;
	return 0;
}

//(3)用string方法定义字符串变量
#include 
#include 
using namespace std;
int main()
{
	string s1 = "week", s2 = "end";
	cout << "s1=" << s1 << endl;
	cout << "s2=" << s2 << endl;
	s1 = s1 + s2;
	cout << "The new string is:" << s1 << endl;
	return 0;
}

运行结果:
//input string1:week↙
//input string2 : end↙
//The new string is : weekend

14.输入n个字符串,将它们按字母由小到大的顺序排列并输出。

【解】

#include 
#include 
using namespace std;
int main()
{
	const int n = 5;
	int i, j;
	string str[n], temp;
	cout << "please input strings:" << endl;
	for (i = 0; i < n; i++)
		cin >> str[i];
	for (i = 0; i < n - 1; i++)
		for (j = 0; j < n - i - 1; j++)
			if (str[j] > str[j + 1])
			{
				temp = str[j]; str[j] = str[j + 1]; str[j + 1] = temp;
			}
	cout << endl << "sorted strings:" << endl;
	for (i = 0; i < n; i++)
		cout << str[i] << endl;
	return 0;
}

运行结果:

//please input strings : (要求输入各字符串)
//China↙
//Japan↙
//Canada↙
//India↙
//Korea↙
//
//sorted strings : (已按要求排好序的字符串)
//
//Canada
//China
//India
//Japan
//Korea

15.输入n个字符串,把其中以字母A打头的字符串输出。

【解】

#include 
#include 
using namespace std;
int main()
{
	const int n = 5;
	string str;
	for (int i = 0; i < n; i++)
	{
		cout << "please input string:";
		cin >> str;
		if (str[0] == 'A')
			cout << str << endl;
	}
	return 0;
}

运行结果:

//please input string  China↙    (输入一个字符串)
//please input string : Again↙    (输入一个字符串)
//Again    (字符串以'A'开头, 输出此字符串)
//please input string : program↙    (输入一个字符串)
//please input string : Abort↙    (输入一个字符串)
//Abort    (字符串以'A'开头, 输出此字符串)
//please input string : word↙    (输入一个字符串)

16.输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,输出THGIL。

【解】

//(1)用字符数组方法
#include 
using namespace std;
int main()
{
	const int n = 10;
	int i;
	char a[n], temp;
	cout << "please input a string:";
	for (i = 0; i < n; i++)
		cin >> a[i];
	for (i = 0; i < n / 2; i++)
	{
		temp = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = temp;
	}	//对称位置元素交换
	for (i = 0; i < n; i++)
		cout << a[i];
	cout << endl;
	return 0;
}


//(2)用string方法
#include 
#include 
using namespace std;
int main()
{
	string a;
	int i, n;
	char temp;
	cout << "please input a string:";
	cin >> a;
	n = a.size();	//计算字符串长度
	for (i = 0; i < n / 2; i++)
	{
		temp = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = temp;
	}
	cout << a << endl;
	return 0;
}

运行结果:
//please input a string : abcdefghij↙
//jihgfedcba

17.输入10个学生的姓名、学号和成绩,将其中不及格者的姓名、学号和成绩输出。

【解】

#include 
#include 
using namespace std;
const int n = 10;
string name[n];	//定义姓名数组
int num[n], score[n];	//定义学号和成绩数组
int main()
{
	int i;
	void input_data();
	input_data();
	cout << endl << "The list failed:" << endl;
	for (i = 0; i < n; i++)
		if (score[i] < 60)
			cout << name[i] << " " << num[i] << "  " << score[i] << endl;
	return 0;
}
void input_data()
{
	int i;
	for (i = 0; i < n; i++)
	{
		cout << "input name,number and score of student " << i + 1 << ":";
		cin >> name[i] >> num[i] >> score[i];
	}
}

运行结果:

//input name, number and score of student 1:Wang 101 78↙       (输入10个学生资料)
//input name, number and score of student 2 : Fun 102 67↙
//input name, number and score of student 3 : Tan 104 98↙
//input name, number and score of student 4 : Li 106 67↙
//input name, number and score of student 5 : Zheng 108 56↙
//input name, number and score of student 6 : Ling 110 77↙
//input name, number and score of student 7 : Chen 112 96↙
//input name, number and score of student 8 : Song 114 78↙
//input name, number and score of student 9 : Xue 116 99↙
//input name, number and score of student 10 : Zhang 118 50↙
//
//The list failed : (输出不及格学生名单)
//Zheng 108 56
//Zhang 118 50

你可能感兴趣的:(C++程序设计(第3版)谭浩强,课后习题答案,c++)