C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55
//C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
OutPut4();
}
//方法1,使用while循环
public static void OutPut1()
{
int sum = 0; //输出的值
int num1 = 0;
int num0 = 0;
int i = 0;//计数器
while (i < 10)
{
if (i == 0)
{
sum = num0 = 1;
}
else if (i == 1)
{
sum = num1 = 1;
}
else
{
sum = num1 + num0;
num0 = num1;
num1 = sum;
}
Console.WriteLine(sum);
i++;
}
Console.Read();
}
//方法2,使用for循环
public static void OutPut2()
{
int num = 1;
int prev = 0;
for (int i = 0; i < 10; i++)
{
Console.WriteLine(num.ToString());//在这里输出或者存起来
int temp = num;
num += prev;
prev = temp;
}
Console.Read();
}
//方法3,使用数组
public static void OutPut3()
{
var numArray = new int[10];
int i = 0;
while (i < 10)
{
if (i == 0)
{
numArray[i] = 1;
}
else if (i == 1)
{
numArray[i] = 1;
}
else
{
numArray[i] = numArray[i - 1] + numArray[i - 2];
}
Console.WriteLine(numArray[i]);
i++;
}
Console.Read();
}
//方法4,使用递归循环
public static void OutPut4()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(Calc(i));
}
Console.Read();
}
public static int Calc(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
return Calc(num - 1) + Calc(num - 2);
}
}
}
C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55的更多相关文章
- 黑马入学基础测试(三)求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
.获得用户的输入 计算 3打印就行了. 这里用到了java.util.Scanner 具体API 我就觉得不常用.解决问题就ok了.注意的是:他们按照流体的方式读取.而不是刻意反复 ...
- Python初学者笔记:打印出斐波那契数列的前10项
问题:斐波那契数列(意大利语: Successione di Fibonacci),又称黄金分割数列.费波那西数列.费波拿契数.费氏数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.- ...
- Problem R: 求斐波那契数列的前n项值
#include<stdio.h> int main() { int n; while(scanf("%d",&n)!=EOF){ int x1,x2,i,x; ...
- 【poj3070】矩阵乘法求斐波那契数列
[题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...
- golang 闭包求斐波那契数列
题目是Go指南中的闭包求斐波那契数列 package main import "fmt" // 返回一个"返回int的函数" func fibonacci() ...
- 用JS,求斐波那契数列第n项的值
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 01-封装函数求斐波那契数列第n项
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- python3 求斐波那契数列(Fibonacci sequence)
输出斐波那契数列的前多少个数. 利用函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan # ----斐波那契数列( ...
- HDU 1568 Fibonacci【求斐波那契数的前4位/递推式】
Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
随机推荐
- C++学习笔记(十四):模板
模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数,从而实现了真正的代码可重用性.模版可以分为两类,一个是函数模版,另外一个是类模版.Java中对应的技术称为泛型. 函数模板 ...
- http://oncenote.com/2015/09/16/Security-2-HTTPS2/ (轉載)
上一篇<iOS安全系列之一:HTTPS>被CocoaChina转载,还顺便上了下头条: 打造安全的App!iOS安全系列之 HTTPS,但那篇文章只是介绍了比较偏应用的初级知识,对于想要深 ...
- Remove “System Program Problem Detected” Messages From Ubuntu
One of my Ubuntu systems would pop up the following message multiple times after logging in: System ...
- Codeforces Round #200 (Div. 1) B. Alternating Current 栈
B. Alternating Current Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343 ...
- [MODx] Build a CMP (Custom manager page) using MIGX in MODX 2.3 -- 2
We are not finishing yet... 1. Under MIGX Management, we need to add some "Actionbuttons" ...
- Android提供的系统服务之--TelephonyManager(电话管理器)
Android提供的系统服务之--TelephonyManager(电话管理器) 转载请注明出处--coder-pig TelephonyManager的作用: 用于管理手机通话状态,获取电话信息(设 ...
- android学习日记16--GridView(网格视图)
一.GridView 1.简述 GridView按照行列来显示图片或文本的一种视图,排列其实有点类似TableLayout布局, 不过和TableLayout还是差别很大的,倒比较像二维的ListVi ...
- [Effective C++ --008]别让异常逃离析构函数
这章非常容易理解:因为C++并不禁止析构函数吐出异常,只是不鼓励这样做而已. 一.原因 假设我们有10个装着鸡蛋的容器,而且现在我们还想着把它在析构函数打烂. class Egg { public : ...
- Memcached source code analysis -- Analysis of change of state--reference
This article mainly introduces the process of Memcached, libevent structure of the main thread and w ...
- 如何查找局域网的外网ip
方法一:一个简单的方法 用你电脑打开www.ip138.com 就可以看到自己的公网IP地址 方法二:如果一定要从路由器里面看 那就打开路由的配置页面 一般在系统状态里面 会有个WAN口IP 那就是你 ...