您现在的位置:首页技术文章

小白入门编程六个练手的有趣的计算问题 --中享思途

1 近似求平方根

青岛it培训机构--中享思途

2 计算e的值

青岛it培训机构--中享思途

3 三角形三边求面积

青岛it培训机构--中享思途

4 斐波那契数列的三种算法

青岛it培训机构--中享思途

5 计算阶乘

青岛it培训机构--中享思途

6 一元二次方程求解

青岛it培训机构--中享思途

附代码

1 近似求平方根

#include <iostream>
using namespace std;
int main()
{
const double dif = 0.0000001;
double n;
cin>>n;
double i = 1.0;
double mul = 0.0;
int j = 0;
while(abs(n-mul)>dif)
{
mul = i * i;
i=(i+n/i)/2;
j++;
cout<<j<<endl;
}
cout<<endl;
cout<<i;
cin.get();cin.get();
return 0;
}

2 计算e的值

#include <stdio.h>
#include <stdlib.h>
int main()
{
double e = 1;
double jc = 1;
int num,i;
printf("利用无穷级数e=1+1/1!+1/2!+1/3!+1/4!+...+1/n!求e,你希望所求的e使用的级数(1-10):");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
jc=jc*i;
e+=1.0/jc;
}
printf("e近似等于:%f",e);
system("pause");
return 0;
}

3 三角形三边求面积

from math import sqrt
a = float(input("Length of edge a:"))
b = float(input("Length of edge b:"))
c = float(input("Length of edge c:"))
if a>0 and b>0 and c>0 and
a+b>c and b+c>a and a+c>b:
....s=(a+b+c)/2
....area = sqrt(s*(s-a)*(s-b)*(s-c))
....print("Area of the triangle:",area)
else:
....print("three edge:", a, b, c, "cannot form a trangle!")

4 斐波那契数列的三种算法

def fibnc(n):
....a,b=1,1
....for i in range(n-1):
........a,b = b,a+b # 元组赋值
....return a
print(fibnc(11))
def fib(n):
....if n == 1 or n == 2:
........return 1
....return fib(n-1) + fib(n-2)
print(fib(11))
def fibn(n):
....if n == 1:
........return [1]
....if n == 2:
........retunr [1,1]
....fibs = [1,1]
....for i in range(2,n):
........fibs.append(fibs[-1] + fibs[-2])
....return fibs
print(fibn(11))

5 计算阶乘

def main():
....i = eval(input("Enter an int for a Fibonacci number:"))
....print("The Fibonacci number of", i , "is", fib(i))
def fib(i):
....if i == 0:.................... # base case
........return 0
....elif i == 1:....................# base case
........return 1
....else:........................ # redcution and recursive calls
........return fib(i-1) + fib(i-2)
main()............................ # call the main function

6 一元二次方程求解

#include<iostream>
#include <math.h>
using namespace std;
int main()
{
double a,b,c,d,x,y;
cout<<"一元二次方程一般式为ax^2+bx+c=0"<<endl;
cout<<"请输入系数a,b,c。"<<endl;
cin>>a>>b>>c;
cout<<"你输入的方程是"<<a<<"x^2+"<<b<<"x+"<<c<<"=0"<<endl;
d=b*b-4*a*c;
if(d>=0)
{
if(d>0)
{
x=((-b)+sqrt(d))/(2*a);
y=((-b)-sqrt(d))/(2*a);
cout<<"方程有两个解"<<""<<x<<""<<y<<endl;
}
else
{
x=((-b)+sqrt(d))/(2*a);
cout<<"方程有一个的根:"<<x<<endl;
}
}
else
cout<<"这个方程没有实数解"<<endl;
cin.get();cin.get();
return 0;
}

【关键词:青岛it培训机构,it培训,it培训哪家好,it培训多少钱,中享思途】

 st_bottom
青岛Java培训,青岛HTML5培训,青岛UI培训,青岛web开发培训,青岛IT培训,java培训,ui培训,HTML5培训,java就业培训,专业ui设计,web开发培训,IT培训,思途教育,青岛思途,中享思途
Copyright © 青岛思途共享科技信息服务有限公司 鲁ICP备14027489号-2

鲁公网安备 37021402000988号

青岛Java培训,青岛HTML5培训,青岛UI培训,青岛web开发培训,青岛IT培训,java培训,ui培训,HTML5培训,java就业培训,专业ui设计,web开发培训,IT培训,思途教育,青岛思途,中享思途