解圆
// 解圆
// 周长format: 2*PI*r(面积求导)
// 面积format: PI*r^2
// 球体积format: 4*PI*r^3/3
// 球表面积format: 4*PI*r^2(体积求导)
// 圆柱体积format: PI*r^2h
// 圆柱表面积format: 2*PI*r^2 + 2*PI*r*h
// 圆锥体积format: PI*r^2*h/3
// 圆锥表面积format: PI*r^2 + PI*r*sqrt(r^2+h^2)
#include <iostream>
#include <cmath>
#define PI 3.14159 //宏定义,方便修改
using namespace std;
int main()
{
float r,h,area_zhouchang,area_circle; //半径,周长,面积
float ball_v,ball_circle; //球体积,表面积
float cy_v,cy_circle; //圆柱体积,表面积
float cone_v,cone_circle; //圆锥体积,表面积
cout << "请输入半价:";
cin >> r;
cout << "请输入高: ";
cin >> h;
area_zhouchang = 2 * PI * r;
area_circle = PI * pow(r,2);
ball_v = 4 * PI * pow(r,3) / 3;
ball_circle = 4 * PI * pow(r,2);
cy_v = PI * pow(r,2) * h;
cy_circle = 2 * PI * pow(r,2) + 2 * PI * r * h;
cone_v = PI * pow(r,2) * h / 3;
cone_circle = PI * pow(r,2) + PI * r * sqrt(pow(r,2)+pow(h,2));
cout << "周长为: " << area_zhouchang << endl;
cout << "面积为: " << area_circle << endl;
cout << "球体积为: " << ball_v << endl;
cout << "球表面积为: " << ball_circle << endl;
cout << "圆柱体积为: " << cy_v << endl;
cout << "圆柱表面积为: " << cy_circle << endl;
cout << "圆锥体积为: " << cone_v << endl;
cout << "圆锥表面积为: " << cone_circle << endl;
}
比较算法
//三个数目比较算法 (小学比赛)
//format: (n-1)! 次比较
//formual: 四个数比较,两两比较,得到最大值,最小值
#include <iostream>
using namespace std;
void swap(int &a,int &b) { //传入引用,对同个引用操作
int temp;
temp = a;
a = b;
b = temp;
}
void sortThree(int &a,int &b,int &c) { //多个数目通常嵌套函数处理
if(a<b) { //a为大数
swap(a,b);
}
if(a<c) { //a为大数
swap(a,c);
}
if(b<c) { //3次比较,b为大数
swap(b,c);
}
}
int main()
{
int num1,num2,num3;
cout << "请输入三个值:";
cin >> num1 >> num2 >> num3;
sortThree(num1,num2,num3);
cout << "三个数从大到小排序: " << num1 << num2 << num3 << endl;
}
解分段函数
// 分段函数求值
// format: f(x)= x (0<=x<2)
// format: f(x)= x^2+1 (2<=x<6)
// format: f(x)= sqrt(x+1) (6<=x<10)
// format: f(x)= 1/x+1 (x>=10)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
cout << "请输入x的值: ";
cin >> x;
if(x<0) { //根据else if只会取一个的特性
cout << "data error!"<< endl; //0非法输入
} else { //嵌套if-else
if(x<2) {
y = x;
} else if(x<6) {
y = x * x + 1;
} else if(x<10) {
y = sqrt(x+1);
} else if(x>=10) {
y = 1 / (x + 1); //注意括号
}
cout << "当x= " << x << "时\n" << "y= " << y << endl;
}
}
等差数列求和
//等差数列求和
//format: (a1+an)*n / 2
#include <iostream>
using namespace std;
int main()
{
int Intsum,Intresult;
cout << "输入你要从1+2+...+(n+1)+(n)的值: ";
cin >> Intsum;
Intresult = (1+Intsum) * Intsum / 2;
cout << "总和为: " << Intresult << endl;
}
解一元二次方程
//一元二次方程的根(韦达定理推导)
//format: x = -b(+-)sqrt(b^2-4ac)/2a
//formual: f(x) = ax^2+bx+c = 0 (a!=0) 配方法 (x+m)^2 = n (n>=0)
//formual: Δ=b^2-4ac 为根的判别式 Δ=0 方程有2个相等(正)实数根 Δ<0 方程无实数根 Δ>0 方程有2个不等实数根。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float a,b,c,Δ,x1,x2;
cout << "分别输入方程的a b c 参数的值:" << endl;
cin >> a >> b >> c;
if( a == 0) {
cout << "一元二次方程的a不为0" << endl;
}
Δ = pow(b,2) - 4 * a * c; //Δ=b^2-4ac
cout << "方程的判别式formula: " << Δ << endl;
if(Δ == 0) {
x1 = (-b + sqrt(pow(b,2)-4 * a * c)) / (2*a); //-b+sqrt(b^2-4ac)/2a
x2 = x1; // 1个相等的正实数根
cout << "x1=x2: " << x1 << endl;
} else if(Δ < 0) {
cout << "方程无实数根,函数图像与x不相交" << endl;
} else if(Δ > 0) {
x1 = (-b + sqrt(pow(b,2)-4 * a * c)) / (2*a);
x2 = (-b - sqrt(pow(b,2)-4 * a * c)) / (2*a); //两个不等的实数根
cout << "x1: " << x1 << endl;
cout << "x2: " << x2 << endl;
}
}
解两点间的距离
// 求两点间的距离
// format: |AB| = sqrt((x1-x2)^2+(y1-y2)^2)
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x1,x2,y1,y2,ab;
cout << "输入第一个点的坐标(x1,y1): ";
cin >> x1 >> y1;
cout << "输入第二个点的坐标(x2,y2): ";
cin >> x2 >> y2;
ab = sqrt(pow(x1-x2,2)+pow(y1-y2,2));
cout << "两点间的距离为: " << ab << endl;
}
斐波那契数列
//斐波那契数列/黄金分割数列,
//format: f(n)=f(n-1)+f(n-2) (n>=2)
//formual:后一项都等于前两项之和,从第三项开始线性呈递
#include <iostream>
using namespace std;
int fibonacci(int index) {
// 设置递归终止条件
if(index==1 || index==2) {
return 1;
}
return fibonacci(index - 1) + fibonacci(index - 2); //递归实现
}
int main() {
int i;
cout << "输入数列第几项: ";
cin >> i;
cout << "到第繁殖" << i << "次之后的数目为:" << fibonacci(i) << endl;
}
1000以内素数
// 1000以内的素数
// format: 素数又称质数,大于1,且除了1和它本身之外不能被其他数整除
#include <iostream>
using namespace std;
bool isPrime(int num) {
if(num <= 1) {
return false;
}
// 从2到该数的平方根开始遍历,1不是素数
for(int i=2;i*i<=num; i++) { //能被本身整除
if(num % i == 0) {
return false; //能被其他数整除都不是素数,为false
}
}
// 即都不能被整除
return true;
}
int main()
{
cout << "1000以内的素数有:";
for(int i=2;i<1000;i++) {
if(isPrime(i)) {
cout << i << " ";
}
}
}