From 0faa7d10d126c1c2d594b8ef99d7f0b2e2c55f74 Mon Sep 17 00:00:00 2001 From: feilong Date: Wed, 15 Dec 2021 21:57:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=90=E6=B3=A2=E9=82=A3?= =?UTF-8?q?=E5=A5=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Loop.json" | 0 .../Loop.md" | 120 ++++++++++++++++++ .../sample/Program.cs" | 105 ++++++++++++++- 3 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 "data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.json" create mode 100644 "data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.md" diff --git "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.json" "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.json" new file mode 100644 index 0000000..e69de29 diff --git "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.md" "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.md" new file mode 100644 index 0000000..b7312fe --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/Loop.md" @@ -0,0 +1,120 @@ +# 斐波那契数列 + +C# 的 foreach 可以遍历实现了迭代器接口的数据。以下哪个选项的代码不对? + +## 答案 + +```csharp +int FibNumbers(int n){ + if(n==0) return 0; + if(n==1) return 1; + return FibNumbers(n-1) + FibNumbers(n-2); +} + +foreach (int number in FibNumbers(7)) { + Console.WriteLine($"{number}"); +} +``` + +## 选项 + +### A + +```csharp +int[] fibNumbers = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 }; +foreach (int number in fibNumbers) { + Console.WriteLine($"{number}"); +} +``` + +### B + +```csharp +IEnumerable FibNumbers(int n){ + yield return 0; + yield return 1; + + int a = 0; + int b = 1; + for(int i=1;i{ + int n; + FibNumbersEnumerator e; + public FibNumbers(int n){ + this.n = n; + this.e = new FibNumbersEnumerator(this.n); + } + public IEnumerator GetEnumerator(){ + return this.e; + } + IEnumerator IEnumerable.GetEnumerator(){ + return this.GetEnumerator(); + } +} + +class FibNumbersEnumerator : IEnumerator{ + int n; + int a; + int b; + int index; + public FibNumbersEnumerator(int n){ + this.n = n; + this.Reset(); + } + + public int Current{ + get { return this.a; } + } + + object IEnumerator.Current{ + get { return this.Current; } + } + + public bool MoveNext(){ + if(this.index<1){ + this.a = 0; + this.b = 1; + }else{ + int next = this.a+this.b; + this.a = this.b; + this.b = next; + + } + + this.index +=1; + if(this.index==this.n+2){ + return false; + }else{ + return true; + } + } + + public void Reset(){ + this.a = 0; + this.b = 0; + this.index = 0; + } + + public void Dispose(){} +} +``` diff --git "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/sample/Program.cs" "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/sample/Program.cs" index 83fa4f4..dbba38c 100644 --- "a/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/sample/Program.cs" +++ "b/data/1..NET\345\210\235\351\230\266/2.C#\350\257\255\346\263\225/7.C# \346\225\260\347\273\204\345\222\214 foreach \350\257\255\345\217\245/sample/Program.cs" @@ -1,2 +1,103 @@ -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); + +using System.Collections; + +int[] fibNumbers = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 }; +foreach (int number in fibNumbers) { + Console.WriteLine($"{number}"); +} + +IEnumerable FibNumbers(int n){ + yield return 0; + yield return 1; + + int a = 0; + int b = 1; + for(int i=1;i{ + int n; + FibNumbersEnumerator e; + public FibNumbers(int n){ + this.n = n; + this.e = new FibNumbersEnumerator(this.n); + } + public IEnumerator GetEnumerator(){ + return this.e; + } + IEnumerator IEnumerable.GetEnumerator(){ + return this.GetEnumerator(); + } +} + +class FibNumbersEnumerator : IEnumerator{ + int n; + int a; + int b; + int index; + public FibNumbersEnumerator(int n){ + this.n = n; + this.Reset(); + } + + public int Current{ + get { return this.a; } + } + + object IEnumerator.Current{ + get { return this.Current; } + } + + public bool MoveNext(){ + if(this.index<1){ + this.a = 0; + this.b = 1; + }else{ + int next = this.a+this.b; + this.a = this.b; + this.b = next; + + } + + this.index +=1; + if(this.index==this.n+2){ + return false; + }else{ + return true; + } + } + + public void Reset(){ + this.a = 0; + this.b = 0; + this.index = 0; + } + + public void Dispose(){} +} + + + +int FibNumbers(int n){ + if(n==0) return 0; + if(n==1) return 1; + return FibNumbers(n-1) + FibNumbers(n-2); +} + +foreach (int number in FibNumbers(7)) { + Console.WriteLine($"{number}"); +} \ No newline at end of file -- GitLab