目录
第三章:
习题3.2
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double r ;
double h ;
double YZC,YMJ, YQB, YQT, YZT;
cout << "请输入半径r=" << endl;
cin >> r;
cout << "请输入高h=" << endl;
cin >> h;
YZC = 2 * 3.14 * r;
YMJ = 3.14 * r*r;
YQB =4*3.14*r*r ;
YQT = 3.14 * pow(r,3);
YZT = YMJ * h;
cout << "圆周长为:" << YZC << endl;
cout << "圆面积为:" << YMJ << endl;
cout << "圆球面积为:" << YQB<< endl;
cout << "圆球体积为:" << YQT << endl;
cout << "圆柱体体积为:" << YZT<< endl;
return 0;
}
习题3.4
#include<iostream>
using namespace std;
int main()
{
char c1, c2;
c1 = getchar();
c2 = getchar();
cout << "c1的值为:" << c1 << endl << "c2的值为:";
putchar(c2);
return 0;
}
习题3.11
#include<iostream>
using namespace std;
int main()
{
int score;
cout << "请输入您的成绩:";
cin >> score;
if (score < 60)
{
cout << "您的等级为E" << endl;
}
else
{
int level;
level = score / 10;
switch (level)
{
case 9:cout << "您的成绩为A级" << endl; break;
case 8:cout << "您的成绩为B级" << endl; break;
case 7:cout << "您的成绩为C级" << endl; break;
case 6:cout << "您的成绩为D级" << endl; break;
default:cout<< "您的成绩为A级" << endl; break;
}
}
system("pause");
return 0;
}
习题3.12
//重点领会:
//printf相对于cout的诸多便利
//定义dayin函数时的思想,特别要分清楚各个参数的作用和目的
#include<iostream>
using namespace std;
int weishu(int n);//位数函数
void dayin(int n);//逐位打印函数
void nixu(int n);//逆序打印函数
int main()
{
int number=0;
cout<< "请输入一个不多于5位的正整数:";
cin >> number;
weishu(number);
dayin(number);
nixu(number);
return 0;
}
//位数函数
int weishu(int n)
{
int i = 0;
for (;n!=0;i++)
{
n = n / 10;
}
printf("这是一个%d位数\n", i);
return i;
}//返回一个位数i方便下面的函数使用
//打印函数
void dayin(int n)
{
int i = weishu(n);//调用的函数并没有改变外部参数的值,下面可以继续使用n
int r;
for (; i > 0; i--)
{
r = n % 10;//最后一位的数
n = n/ 10;
printf("第%d位上的数是%d\n", i, r);
}
}
//逆序打印函数
void nixu(int n)
{
int i = weishu(n);//调用的函数并没有改变外部参数的值,下面可以继续使用n
int r;
for (; i > 0; i--)
{
r = n % 10;//最后一位的数
n = n / 10;
printf("%d ", r);
}
}
习题3.14
#include<iostream>
using namespace std;
int main()
{
cout << "请输入四个整数";
int a[4];
int i, s, r;
for (int s = 0; s < 4; s++)
{
cin >> a[s];
}//将数据放入数组,便于依次对数据操作
for (int i = 1; i < 4; i++)//控制排序的次数
{
for (int r = 0; r < 4 - i; r++)//控制首个数字的排序操作
{
int temp;
if (a[r] > a[r + 1])
{
temp = a[r];
a[r] = a[r + 1];
a[r + 1] = temp;
}
}
}//冒泡排序
for (i = 0; i < 4; i++)
cout << a[i] << " ";
return 0;
}
习题3.15
#include<iostream>
using namespace std;
int main()
{
int m, n;
cout << "请输入m和n的值:";
cin >> m >> n;
int temp = 0;
temp = (m < n) ? m : n;//将a,b中较小的一个赋给temp
while (temp > 0)
{
if ((m % temp == 0) && (n % temp == 0))
break;
--temp;
}//穷举法
cout << "最大公因数为:"<<temp;
temp = m * n / temp;//最大公约数*最小公倍数=两数的乘积
cout << "最小公倍数为:" << temp;
return 0;
}
习题3.16
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a, b, c, d;
char ch;
cout << "请输入一个字符串:";
a = b = c = d= 0;
while ((ch = getchar()) != '\n')//当输入回车的时候(统计完毕)跳出while打印结果
{
if (ch >= '0' && ch <= '9')
a++;//数字
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
b++;//字母
else if (ch == ' ')
c++;//空格
else
d++;//其他字符
}
cout << "数字,字母,空格和其他字符的个数分别为:" << a <<"," << b<<"," << c<<"," <<d<<"," << endl;
system("pause");
return 0;
}
习题3.17
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a, n, x, sn=0;
cout << "请输入a与n:" << endl;
cin >> a >> n;
for (int i = 1; i <=n; i++)
{
x = pow(a, i);
sn += x;//即sn=sn+x;
}
cout << "a的阶乘为:" << sn << endl;
system("pause");
return 0;
}
习题3.18
#include<iostream>
using namespace std;
int main()
{
int n;
cout << "请输入n:";
cin >> n;
int Sn=0;
int temp = 1;
for (int i = 1; i <= n; ++i)
{
temp *= i;
Sn += temp;
}
cout << "n的阶乘为:" << Sn << endl;
system("pause");
return 0;
}
习题3.19
#include<iostream>
using namespace std;
int main()
{
int num = 100;
int a = 0;//个位
int b = 0;//十位
int c = 0;//百位
do{
a = num % 10;//获取个位
b = num / 10 % 10;//获取十位
c = num / 100;//获取百位
if (a*a*a+b*b*b+c*c*c==num)//是水仙花数才打印
{
cout << num << endl;
}
num++;
}while (num < 1000);
system("pause");
return 0;
}
习题3.20
#include<iostream>
using namespace std;
int main()
{
int lim = 0;
cout << "请输入一个上限:";
cin >> lim;
cout << lim <<"以内的完数有:" << endl;
//若Sn定义在循环之外,下一次循环会在上一次所得的Sn值为起始,故没出现Sn==a的情况
for (int a=1; a <= lim; a++)
{
int Sn = 0;//在循环内定义变量可以起到重置的作用
for (int temp = 1;temp<a; temp++)
{
if (a%temp==0)
Sn += temp;
}
if (Sn == a)
cout << a << " ";
}
system("pause");
return 0;
}
习题3.21
#include<iostream>
using namespace std;
int main()
{
int num1 = 1;//两个int型变量计算的结果默认是int型
double num2 = 2;//如果要获取double型的结果,两个计算数必须有一个是double类型
int lim = 0;
double an = 0;//单项
double temp = 0;//临时变量
double Sn = 0;//单项和
cout << "请输入项数:";
cin >> lim;
for (int i=1;i<lim;i++)
{
temp = num2;
num2 = num1 + num2;
num1 = temp;
an = num2 / num1;
Sn += an;
}
printf("%0.2lf", Sn);
system("pause");
return 0;
}
习题3.22
#include<iostream>
using namespace std;
int main()
{
double p = 1;
for (int i=0;i<9;i++)
{
p = (p + 1) * 2;
}
cout << "猴子第一天摘了:" << p << "个桃子" << endl;
system("pause");
return 0;
}
习题3.23
//用迭代法求x=根号a,求平方根的迭代公式为X(n+1)=1/2(X(n)+a/Xn)
//要求前后两次求出的x的差的绝对值小于10^(-5)
#include<iostream>
using namespace std;
int main()
{
float a;
cout << "请输入a的值;";
cin >> a;
float m, n;
m = a / 2;
while (1)
{
n = m;
m = 0.5 * (m + a / m);
if (fabs(m - n) < pow(10, -5)) break;
}
cout << m << endl;
system("pause");
return 0;
}
习题3.24
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i <=7; i+=2)
{
for (int n = 0; n < i; n++)
cout << "* ";
cout << endl;
}
for (int i = 5; i >=1; i -= 2)
{
for (int n = 0; n < i; n++)
cout << "* ";
cout << endl;
}
system("pause");
return 0;
}
习题3.25
#include<iostream>
using namespace std;
int main()
{
char i, j, k;
for (i = 'X'; i <= 'Z'; j++)//i,j,k分别为A,B,C的对手
{
for (j = 'X'; j <= 'Z'; j++)
{
if (i != j)//比赛队员不能重复比赛
{
for (k = 'X'; k <= 'Z'; k++)
{
if (i != k && j != k)
{
if (i != 'X' && k != 'X' && k != 'Z')
{
cout << "A的对手是:" << i << endl;
cout << "B的对手是:" << j << endl;
cout << "C的对手是:" << k << endl;
}
}
}
}
}
}
system("pause");
return 0;
}
第四章:
习题4.1
#include<iostream>
using namespace std;
int main()
{
int a, b;
cout << "请输入两个数:" << endl;
cin >> a >> b;
//最大公因数
int num1,num2 = 0;
if (a >= b)
num1 = b;
else
num1 = a;//将较小数赋值给num1
for (; a % num1 != 0 || b % num1 != 0 ;)
{
num1--;
}
cout << "最大公因数为:" << num1 << endl;
//最小公倍数
if (a <= b)
num2 = b;
else
num2 = a;//将较大数赋值给num1
for (;num2%a!=0||num2%b!=0;)
{
num2++;
}
cout << "最小公倍数为:" << num2 << endl;
system("pause");
return 0;
}
习题4.2
#include<iostream>
#include <math.h>
using namespace std;
int main()
{
double a, b, c, x1, x2;
cout << "请输入a,b,c的值:" << endl;
cin >> a >> b >> c;
if (b * b - 4 * c > 0)
{
x1 = b/-2*a + sqrt(b * b - 4 * a * c)/-2*a;//sqrt开平方函数
x2 = b/-2*a - sqrt(b * b - 4 * a * c)/-2*a;
cout << "方程的根为:" << x1 <<"," << x2;
}
else
if (b * b - 4 * c < 0)
{
cout << "无实数根";
}
else
if (b* b - 4 * c == 0)
{
x2 = x1 = b/-2*a + sqrt(b * b - 4 * a * c)/-2*a;
cout << "方程的根为:" << x1 << "," << x2;
}
system("pause");
return 0;
}
习题4.3
#include<iostream>
using namespace std;
int main()
{
int n = 0;
int i = 2;
cout << "请输入一个整数:" << endl;
cin >> n;
if (n <= 0)
{
cout << "请重新输入一个正数" << endl;
cin >> n;
}
while(n%i!=0)
{
i++;
}
if (i == n)
cout << "这个数是素数" << endl;
else
cout << "这个数不是素数" << endl;
system("pause");
return 0;
}
习题4.4
#include<iostream>
int fac(int n);
using namespace std;
int main()
{
int a, b, c, d;
cout << "请输入a,b,c的值:" << endl;
cin >> a >> b >> c;
d = fac(a) + fac(b) + fac(c);
cout << "a,b,c的阶乘之和为:" << d << endl;
system("pause");
return 0;
}
int fac(int n)
{
int temp=1;
for (int i = 1; i <= n; i++)
{
temp *= i;
}
return temp;
}
习题4.5
#include<iostream>
#include<math.h>
using namespace std;
double ex(int i);
int main()
{
double sinh = 0;
int x ;
cout << "请输入x的值:";
cin >> x;
sinh =(ex(x)-1/ex(x))/2;
cout << "目标函数的值为:" << sinh << endl;
system("pause");
return 0;
}
double ex(int i)
{
double n = 0;
double e = 2.71;
n = pow(e, i);
return n;
}
习题4.6
#include<iostream>
#include<math.h>
double ND(double a, double b, double c, double d);
using namespace std;
int main()
{
double a, b, c, d;
cout << "请输入a,b,c,d的值:";
cin >> a >> b >> c >> d;
cout << "方程的根的值为:" << ND(a, b, c, d) << endl;
system("pause");
return 0;
}
double ND(double a, double b, double c, double d)
{
double x = 1, x0, f, f1;
do
{
x0 = x;//上次迭代的结果作为下次的x0
f = ((a * x0 + b) * x0 + c) * x0 + d;
f1 = (3 * a * x0 + 2 * b) * x0 + c;
x = x0 - f / f1;
} while (x - x0 >= 1e-5);
return x;
}
习题4.7
//找素数时一定要注意2以外的偶数
#include<iostream>
using namespace std;
int prime(int x);
int godbah(int i);
int main()
{
int i;
cout << "请输入一个不小于六的偶数:";
cin >> i;
while (i % 2 != 0)
{
cout << "请重新输入一个不小于六的偶数:";
cin >> i;
if (i % 2 == 0)
break;
}//保证是一个不小于6的偶数
godbah(i);
system("pause");
return 0;
}
int prime(int x)//判别一个数是否为素数
{
int n = 2;//传递作用
if (x == 2)
return 2;//过滤掉除了2以外的偶数(都不是素数)
else
while(x%n!=0)
{
++n;
if (x == n)
return x;//注意先后顺序
}
}
int godbah(int i)
{
int n1 = 0;
int n2 = 0;//接收两个素数
for (int s = 2; s <= i; ++s)
{
n1 = prime(s);//2是素数,从2开始也与prime函数作用契合
for (int n = 2; n <= i; ++n)
{
n2 = prime(n);
if (n1 + n2 == i)
break;
}
if (n1 + n2 == i)
break;
}
cout << i << "=" <<n1<<"+"<<n2;
return 0;
}
第五章:
习题5.1
#include<iostream>
using namespace std;
int main()
{
cout << "100以内的素数有:" << endl;
for (int i = 1; i < 101; i++)
{
int temp = 2;
while (i % temp != 0&&temp<i)
++temp;
if (temp == i)
{
cout << i << endl;
}
}
system("pause");
return 0;
}
习题5.3
#include<iostream>
using namespace std;
int main()
{
int i = 0;
int r = 0;
int temp = 0;
int arr[3][3] = { {1,2,3},
{4,5,6},
{7,8,9} };
for (; i <= 2 && r <= 2; i++, r++)
{
temp += arr[i][r];
}
for ( i = 2, r = 2; i >= 0 && r >= 0; i--, r--)
{
temp += arr[i][r];
}
cout << "该二维数组的对角线元素之和为:" << temp << endl;
system("pause");
return 0;
}
习题5.4
#include<iostream>
using namespace std;
int main()
{
int a = 0;
int arr[11] = { 1,4,6,7,9,10,12,15,46,47 };
cout << "请插入一个数:";
cin >> a;
for (int i = 0; i < 11; ++i)
{
if (arr[i] > a)
{
int temp = a;
a = arr[i];
arr[i] = temp;
}//比较完就拿出来,再作为输入的数字进行排序
else if (a > arr[10])
arr[10] = a;
}
cout << "排序之后:";
for (int i = 0; i < 11; i++)
cout << arr[i] << " ";
system("pause");
return 0;
}
习题5.7
//求一个二维数组的鞍点 二维数组可能没有鞍点,如果有,只能有一个
//鞍点:该元素在行上最大在列上最小
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int a[4][5];
int i, j, k, m = 0, n = 0, max = 0, min = 0, temp = 0;
printf("输入数组的值:\n");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
cin >> a[i][j];
}
}
//for (i = 0; i < 4; i++)
//for (j = 0; j < 5; j++)
//scanf_s("%d", &a[i][j]);//scanf_s解决scanf返回值被忽略的报错
for (i = 0; i < 4; i++)
{
for (j = 0; j < 5; j++)//获得i行最大值
if (a[i][m] < a[i][j])
{
m = j;
a[i][m] = a[i][j];
}
max = a[i][m];//存储该行最大值
min = a[0][m];//存储该行最大值所在列的最小值
for (k = 0; k < 4; k++)
if (a[k][m] < min)//获得该列最小值
{
min = a[k][m];//从该列第一个元素开始比较,所以上面用数组的方式对min初始化
n = k;//用n来接收鞍部的列数
}
if (max == min)
{//若相等则有鞍点,循环结束
temp = 1;
//printf("鞍点为:a[%d][%d]=%d", n, m, a[n][m]);
cout << "鞍点为:" << "a[" << n << "][" << m << "]" << "=" << a[n][m];
break;
}
}
if (temp == 0)
//printf("该二维数组不存在鞍点!");
cout << "该二维数组不存在鞍点";
}