【解】
#include <iostream>
using namespace std;
int main()
{
int hcf(int, int);
int lcd(int, int, int);
int u, v, h, l;
cin >> u >> v;
h = hcf(u, v);
cout << "H.C.F=" << h << endl;
l = lcd(u, v, h);
cout << "L.C.D=" << l << endl;
return 0;
}
int hcf(int u, int v)
{
int t, r;
if (v > u)
{
t = u; u = v; v = t;
}
while ((r = u % v) != 0)
{
u = v;
v = r;
}
return(v);
}
int lcd(int u, int v, int h)
{
return(u*v / h);
}
运行结果:
//24 16↙?? ?(输入两个整数)
//H.C.F = 8?? ?(最大公约数)
//L.C.D = 48?? ?(最小公倍数)
【解】
#include <iostream>
#include <math.h>
using namespace std;
float x1, x2, disc, p, q;
int main()
{
void greater_than_zero(float, float);
void equal_to_zero(float, float);
void smaller_than_zero(float, float);
float a, b, c;
cout << "input a,b,c:";
cin >> a >> b >> c;
disc = b * b - 4 * a * c;
cout << "root:" << endl;
if (disc > 0)
{
greater_than_zero(a, b);
cout << "x1=" << x1 << ", x2=" << x2 << endl;
}
else if (disc == 0)
{
equal_to_zero(a, b);
cout << "x1=" << x1 << ", x2=" << x2 << endl;
}
else
{
smaller_than_zero(a, b);
cout << "x1=" << p << " + " << q << " i" << endl;
cout << "x2=" << p << " - " << q << " i" << endl;
}
return 0;
}
void greater_than_zero(float a, float b)//定义一个函数, 用来求disc>0时方程的根
{
x1 = (-b + sqrt(disc)) / (2 * a);
x2 = (-b - sqrt(disc)) / (2 * a);
}
void equal_to_zero(float a, float b)//定义一个函数, 用来求disc=0时方程的根
{
x1 = x2 = (-b) / (2 * a);
}
void smaller_than_zero(float a, float b)//定义一个函数, 用来求disc<0时方程的根
{
p = -b / (2 * a);
q = sqrt(-disc) / (2 * a);
}
运行结果:
//① input a, b, c:1 2 1↙
//root :
//x1??= –1, x2??= –1
//② input a, b, c : 2 4 1↙
//root :
//x1??= –0.2928931, x2??= –1.70711
//③ input a, b, c : 2 4 3↙
//x1??= –1 + 0.70107i, x2??= –1–?0.701107i
【解】
#include < iostream >
using namespace std;
int main()
{
int prime(int); //函数原型声明
int n;
cout << "input an integer:";
cin >> n;
if (prime(n))
cout << n << " is a prime." << endl;
else
cout << n << " is not a prime." << endl;
return 0;
}
int prime(int n)
{
int flag = 1, i;
for (i = 2; i < n / 2 && flag == 1; i++)
if (n%i == 0)
flag = 0;
return(flag);
}
运行结果:
//input an integer : 17↙
//17 is a prime.
【解】
#include <iostream>
using namespace std;
int main()
{
int fac(int);
int a, b, c, sum = 0;
cout << "enter a,b,c:";
cin >> a >> b >> c;
sum = sum + fac(a) + fac(b) + fac(c);
cout << a << "!+" << b << "!+" << c << "!=" << sum << endl;
return 0;
}
int fac(int n)
{
int f = 1;
for (int i = 1; i <= n; i++)
f = f * i;
return f;
}
运行结果:
//enter a, b, c:4 5 6↙
//4!+ 5!+ 6 != 864
【解】
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double e(double);
double x, sinh;
cout << "enter x:";
cin >> x;
sinh = (e(x) + e(-x)) / 2; //计算sinh(x)
cout << "sinh(" << x << ")=" << sinh << endl;
return 0;
}
double e(double x) //求ex的函数
{
return exp(x);
}
运行结果:
//enter x : 1.5↙
//sinh(1.5) = 2.35241
【解】
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double solut(double, double, double, double);
double a, b, c, d;
cout << "input a,b,c,d:";
cin >> a >> b >> c >> d;
cout << solut(a, b, c, d) << endl;
return 0;
}
double solut(double a, double b, double c, double d)
{
double x = 1, x0, f, f1;
do
{
x0 = x;
f = ((a*x0 + b)*x0 + c)*x0 + d;
f1 = (3 * a*x0 + 2 * b)*x0 + c;
x = x0 - f / f1;
} while (fabs(x - x0) >= 1e-5);
return(x);
}
运行结果:
//input a, b, c, d:4 3 2 1↙
//x??= –?0.60583
【解】
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
void godbaha(int);
int n;
cout << "input n:";
cin >> n;
godbaha(n); //调用godbaha函数, 选出满足要求的组合
return 0;
}
void godbaha(int n)
{
int prime(int);
int a, b;
for (a = 3; a <= n / 2; a = a + 2) //a由3变到n/2(取其整数), 每次增值2
{
if (prime(a)) //调用prime函数, 如果a是素数则prime(a)?的值为1
{
b = n - a; //a是素数, 应检查b是否为素数
if (prime(b)) //如果b也是素数则输出结果
cout << n << " = " << a << " + " << b << endl;
}
}
}
int prime(int m) //判别m是否为素数的函数
{
int i, k = sqrt(m);
for (i = 2; i <= k; i++)
if (m%i == 0) break;
if (i > k) return 1;
else return 0;
}
运行结果:
//input n : 20↙
//20 = 3 + 17
//20 = 7 + 13
【解】
#include <iostream>
using namespace std;
int main()
{
int x, n;
float p(int, int);
cout << "input n & x: ";
cin >> n >> x;
cout << "n = " << n << ", x = " << x << endl;
cout << "P" << n << "(" << x << ")=" << p(n, x) << endl;
return 0;
}
float p(int n, int x)
{
if (n == 0)
return(1);
else if (n == 1)
return(x);
else
return(((2 * n - 1)*x*p((n - 1), x) - (n - 1)*p((n - 2), x)) / n);
}
运行结果:
//① input n & x:0 7↙
//n = 0, x = 7
//P0(7) = 1
//② input n & x : 1 2↙
//n = 1, x = 2
//P1(2) = 2
//③ input n & x : 3 4↙
//n = 3, x = 4
//P3(4) = 154
【解】
#include <iostream>
using namespace std;
int main()
{
void hanoi(int n, char one, char two, char three);
int m;
cout << " input the number of disks:";
cin >> m;
cout << " The steps of moving " << m << " disks:" << endl;
hanoi(m, 'A', 'B', 'C');
return 0;
}
void hanoi(int n, char one, char two, char three)
//将n个盘从one座借助two座移到three座
{
void move(char x, char y);
if (n == 1) move(one, three);
else
{
hanoi(n - 1, one, three, two);
move(one, three);
hanoi(n - 1, two, one, three);
}
}
void move(char x, char y)
{
cout << x << "->" << y << endl;
}
运行结果:
//input the number of disks : 4↙
//The steps of moving 4 disks :
//A→B
//A→C
//B→C
//A→B
//C→A
//C→B
//A→B
//A→C
//B→C
//B→A
//C→A
//B→C
//A→B
//A→C
//B→C
【解】
#include <iostream>
using namespace std;
int main()
{
void convert(int n);
int number;
cout << "input an integer:";
cin >> number; //输入一个整数
cout << "output:" << endl;
if (number < 0)
{
cout << "–";
number = -number; //如果是负数, 把它变为正数再处理
}
convert(number); //调用convert函数
cout << endl;
return 0;
}
void convert(int n)
{
int i;
char c;
if ((i = n / 10) != 0) //检查n是否为个位数
convert(i); //如果不是, 递归调用convert函数
c = n % 10 + '0';
cout << " " << c;
}
运行结果:
//input an integer : 345↙
//output : 3 4 5
【解】
#include <iostream>
using namespace std;
int main()
{
int f(int);
int n, s;
cout << "input the number n:";
cin >> n;
s = f(n);
cout << "The result is " << s << endl;
return 0;
}
int f(int n)
{
if (n == 1)
return 1;
else
return (n*n + f(n - 1));
}
运行结果:
//input the number n : 5↙
//The result is 55
【解】
#include <iostream>
#include <cmath>
using namespace std;
#define S(a,b,c) (a+b+c)/2
#define AREA(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)–a)*(S(a,b,c)–b)*(S(a,b,c)–c))
int main()
{
float a, b, c;
cout << "input a,b,c:";
cin >> a >> b >> c;
if (a + b > c && a + c > b && b + c > a)
cout << "area=" << AREA(a, b, c) << endl;
else
cout << "It is not a triangle!" << endl;
return 0;
}
运行结果:
//① input a, b, c:3 4 5↙
//area = 6
//② input a, b, c : 12 3 5↙
//It is not a triangle!
?