例5.2
统计某次考试的平均成绩和均方差
#include <iostream>
#include <cmath>
using namespace std;
int main(){
const int MAX = 100;
int score[MAX], num = 0;
double average = 0, variance = 0;
cout << "请输入成绩(-1表示结束):\n";
for(num = 0; num < MAX; ++num){
cin >> score[num];
if (score[num] == -1) break;
average += score[num];
}
average = average / num;
for(int i=0; i<num; ++i){
variance += (average - score[i]) * (average - score[i]);
}
variance = sqrt(variance / num);
cout << "平均分是:" << average << "\n均方差是:" << variance << endl;
return 0;
}
例5.3
计算两个十维向量的数量积
#include <iostream>
using namespace std;
int main(){
const int MAX = 10;
double a[MAX], b[MAX], result = 0;
int i;
//输入向量a
cout << "请输入向量a的十个分量:";
for(i = 0; i<MAX; ++i)
cin >> a[i];
//输入向量b
cout << "请输入向量b的十个分量:";
for(i=0;i<MAX;++i)
cin >> b[i];
for(i=0; i<MAX; ++i)
result += a[i] * b[i];
cout << "a,b的数量积是:" << result << endl;
return 0;
}
例5.5 二分查找
#include <iostream>
using namespace std;
int main(){
int low, high, mid, x;
int array[] = {0,1,2,3,4,5,6,7,8,9};
cout << "请输入要查找的数据:";
cin >> x;
low = 0;
high = 9;
while(low <= high){
mid = (low+high) / 2 ;
if(x == array[mid])
break;
if(x < array[mid])
high = mid - 1;
else
low = mid + 1;
}
if(low > high)
cout << "没有找到" << endl;
else
cout << x << "的位置是" << mid << endl;
return 0;
}
例5.31直接选择排序
#include <iostream>
using namespace std;
int main(){
int lh, rh, k, tmp;
int array[] = {2,5,1,9,10,0,4,8,7,6};
for(lh = 0; lh < 10; ++lh){
rh = lh;
for (k = lh; k < 10; ++k)
if(array[k] < array[rh])
rh = k;//记录
//交换位置
tmp = array[lh];
array[lh] = array[rh];
array[rh] = tmp;
}
for(int x: array)
cout << x << ' ';
return 0;
}
5.32 冒泡排序
#include <iostream>
using namespace std;
int main(){
int a[] = {0,3,5,1,8,7,9,4,2,10,6};
int i, j, tmp;
bool flag;//记录一趟冒泡有没有发生交换
for(i=1;i<11;++i){
flag = false;
for(j=0; j<11-i; ++j){
if(a[j+1] < a[j]){
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
flag = true;
}
}
if(!flag)
break;
}
for(int x:a){
cout << x <<' ';
}
return 0;
}
例5.8 矩阵乘法
#include <iostream>
using namespace std;
#define MAX_SIZE 10 //矩阵最大规模
int main(){
int a[MAX_SIZE][MAX_SIZE],b[MAX_SIZE][MAX_SIZE],c[MAX_SIZE][MAX_SIZE];
int i,j,k,NumOfRowA,NumOfColA,NumOfColB;
//输入A、B大小,A的列即为B的行
cout << "请输入A的行数、列数和B的列数:";
cin >> NumOfRowA >> NumOfColA >> NumOfColB;
//输入A
cout << "输入A:\n";
for(i=0; i<NumOfRowA; ++i){
for(j=0; j<NumOfColA; ++j){
cout << "a[" << i <<"][" << j << "] = ";
cin >> a[i][j];
}
}
//输入B
cout << "输入B:\n";
for(i=0; i<NumOfColA; ++i){
for(j=0; j<NumOfColB; ++j){
cout << "b[" << i <<"][" << j << "] = ";
cin >> b[i][j];
}
}
//计算A*B
for(i=0;i<NumOfRowA;++i){
for(j=0;j<NumOfColB;j++){
c[i][j] = 0;
for(k=0;k<NumOfColA;++k){
c[i][j] += a[i][k] * b[k][j];
}
}
}
//输出C
cout << "输出C:\n";
for(i=0; i<NumOfRowA; ++i){
cout << endl;
for(j=0; j<NumOfColB; ++j){
cout << c[i][j] << " ";
}
}
return 0;
}
例5.9 打印N阶魔阵
规则:第一个元素放第一行中间列,下一个元素放当前元素的上一行、下一列,如已有内容,则放当前列的下一行,当前行是最后一行时,下一行为第0行,当前列为最后一列时,下一列为第0列,当前行为第0行时,上一行为最后一行
思路:用(row-1+阶数)%阶数来表示上一行,用(col+1)%阶数来表示下一列,(row+1)%阶数表示下一行
#include <iostream>
using namespace std;
#define MAX 15 //最高打印15阶
int main(){
int magic[MAX][MAX] = {0}; //将magic每一个元素设为0
int row, col, count, scale;
//输入scale
cout << "input scale\n";
cin >> scale;
//生成魔阵
row = 0;
col = (scale - 1) / 2;
magic[row][col] = 1;
for(count = 2; count <= scale * scale; count++){
//看上一行下一列是否有值
if(magic[(row - 1 + scale) % scale][(col + 1) % scale] == 0){
row = (row - 1 + scale) % scale;
col = (col + 1) % scale;
}
else row = (row+1) % scale;
magic[row][col] = count;
}
//输出
for(row = 0; row < scale; row++){
for (col = 0; col < scale; col++){
cout << magic[row][col] << " ";
}
cout << endl;
}
return 0;
}
例5.10 求解三元一次方程组
#include <iostream>
using namespace std;
int main(){
double a[3][3], b[3], result[3], detA, detB, tmp[3];
int i, j;
for(i=0; i<3; ++i){
cout << "请输入第" << i+1 << "个方程的3个系数和常数项:";
cin >> a[i][0] >> a[i][1] >> a[i][2] >> b[i];
}
detA = a[0][0]*a[1][1]*a[2][2] + a[0][1]*a[1][2]*a[2][0] + a[0][2]*a[1][0]*a[2][1]
- a[0][2]*a[1][1]*a[2][0] - a[0][1]*a[1][0]*a[2][2] - a[0][0]*a[2][1]*a[1][2];
for(i=0; i<3; ++i){
for(j=0; j<3; ++j){
tmp[j] = a[i][j];
a[j][i] = b[j]; //b替换a的第i列
}
detB = a[0][0]*a[1][1]*a[2][2] + a[0][1]*a[1][2]*a[2][0] + a[0][2]*a[1][0]*a[2][1]
- a[0][2]*a[1][1]*a[2][0] - a[0][1]*a[1][0]*a[2][2] - a[0][0]*a[2][1]*a[1][2];
for(j=0; j<3; ++j){
a[j][i] = tmp[j];
}
result[i] = detB / detA;
}
cout << "x=" << result[0] << " y=" << result[1] << " z=" << result[2];
return 0;
}
例5.11
输入一个句子,统计有多少个单词,单词之间用空格分开
#include <iostream>
using namespace std;
int main(){
const int LEN = 80;
char sentence[LEN+1],pre = ' ';
int i, num = 0;
cin.getline(sentence, LEN+1);
for(i=0; sentence[i]!='\0'; ++i){
if(pre==' ' && sentence[i]!=' '){
++num;
}
pre = sentence[i];
}
cout << "单词个数为:" << num << endl;
return 0;
}
例 5.12
统计一组输入整数的和。输入时,整数之间用空格分开,这组整数可以是八进制,十进制或十六进制表示,八进制以0开头,如075,十六进制以0x开头,如0x1F9,其他均为十进制,输入以回车符结束
#include <iostream>
using namespace std;
int main(){
char str[81];
int sum = 0, data, i = 0, flag; //flag记录当前正在处理的整数的基数
cout << "请输入一组整数,用空格分开:";
cin.getline(str,81);
while(str[i] == ' ') ++i; //跳过前置的空格
while(str[i] !='\0'){
//区分基数
if(str[i]!='0') flag = 10; //十进制
else {
if(str[i+1] == 'x' || str[i+1] == 'X') {
flag = 16;
i+=2;
}
else{
flag = 8;
++i;
}
}
//转化成整型整数
data = 0;
switch(flag){
case 10 :
while (str[i] !=' ' && str[i] != '\0') data = data * 10 + str[i++] - '0';
break;
case 8:
while (str[i] !=' ' && str[i] != '\0') data = data * 8 + str[i++] - '0';
break;
case 16:
while (str[i] !=' ' && str[i] != '\0'){
data = data * 16;
if(str[i] >= 'A' && str[i] <= 'F'){
data += str[i++] -'A' + 10;
}
else if(str[i] >= 'a' && str[i] <= 'f'){
data += str[i++] - 'a' + 10;
}
else {
data += str[i++] - '0';
}
}
}
sum += data;
while(str[i] == ' ') ++i;
}
cout << "结果为:" << sum << endl;
return 0;
}