解圆
#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;
}
比较算法
#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) {
swap(a,b);
}
if(a<c) {
swap(a,c);
}
if(b<c) {
swap(b,c);
}
}
int main()
{
int num1,num2,num3;
cout << "请输入三个值:";
cin >> num1 >> num2 >> num3;
sortThree(num1,num2,num3);
cout << "三个数从大到小排序: " << num1 << num2 << num3 << endl;
}
解分段函数
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x,y;
cout << "请输入x的值: ";
cin >> x;
if(x<0) {
cout << "data error!"<< endl;
} 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;
}
}
等差数列求和
#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;
}
解一元二次方程
#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;
cout << "方程的判别式formula: " << Δ << endl;
if(Δ == 0) {
x1 = (-b + sqrt(pow(b,2)-4 * a * c)) / (2*a);
x2 = x1;
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;
}
}
解两点间的距离
#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;
}
斐波那契数列
#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以内素数
#include <iostream>
using namespace std;
bool isPrime(int num) {
if(num <= 1) {
return false;
}
for(int i=2;i*i<=num; i++) {
if(num % i == 0) {
return false;
}
}
return true;
}
int main()
{
cout << "1000以内的素数有:";
for(int i=2;i<1000;i++) {
if(isPrime(i)) {
cout << i << " ";
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?