int number=10,number=20,temp=0; temp=number; number1=number2; number2=number1;
通过 *和/
int number1=10,number2=20; number1=number1*number2; //200 number2=number1/number2; //10 number1= number1/number2; //20
通过 +和-
int number1=10,number=20; number1=number1+number2; //30 number2=number1-number2; //10 number1=number1-number2; //20
string name1="Dotnet", name2="C#"; name1=name1+name2; //DotnetC# name2=name1.Substring(0,name1.Length-name2.Length); //Dotnet name1=name1.Substring(name2.Length); //C#
斐波那契数列是按以下顺序排列的数字序列:
在C#中实现斐波那契数列的方法
迭代打印斐波那契数列的方法:
int firstNumber=0;SecondNumber=1;nextNumber; Console.Write("输入打印的元素数量:"); int numberOfElement=int.Parse(Console.ReadLine()); if(numberOfElements>2) { Console.Write($"{firstNumber} {SecondNumber}"); for(int i=2;i<numberOfElements;i++) { nextNumber=firstNumber+SecondNumber; Console.Write($"{nextNumber} "); firstNumber=SecondNumber; SecondNumber=nextNumber; } }
递归打印斐波那契数列的方法:
Console.Write("输入打印的元素数量:"); int numberOfElement=int.Parse(Console.ReadLine()); FibonacciSeries(0,1,1,numberOfElement); //递归打印方法 FibonacciSeries(int firstNumber,int secondNumber,int Counter,int number) { Console.Write($"{firstNumber} "); if(counter>number) FibonacciSeries(secondNumber,firstNumber+secondNumber,counter+1); }
static void Main(string[] args) { Console.Write("请输入需要获取的斐波那契数值的编号: "); int number = int.Parse(Console.ReadLine()); //因为序列是从0开始, 所以我们计算某一位时,需要-1; number = number - 1; Console.Write(NthFibonacciNumber(number)); Console.ReadKey(); } private static int NthFibonacciNumber(int number) { int firstNumber = 0, secondNumber = 1, nextNumber = 0; //如果输入0,则返回0 if (number == 0) return firstNumber; for (int i = 2; i <= number; i++) { nextNumber = firstNumber + secondNumber; firstNumber = secondNumber; secondNumber = nextNumber; } return secondNumber; }
质数又称素数, 一个大于1的自然数, 除了1和它自身外, 不能被其他自然数整除的数叫做质数。,例如: 2,3,5,7,11,13,17,19,23... 是质数。
Console.Write("输入数字: "); int number = int.Parse(Console.ReadLine()); bool IsPrime = true; for (int i = 2; i < number / 2; i++) { if (number % i == 0) { IsPrime = false; break; } } if (IsPrime) Console.Write($"{number}是质数"); else Console.Write($"{number}不是质数"); Console.ReadKey();
Console.Write("输入起始数字: "); int startNumber = int.Parse(Console.ReadLine()); Console.Write("输入结束数字: "); int endNumber = Convert.ToInt32(Console.ReadLine()); for (int i = startNumber; i <= endNumber; i++) { int counter = 0; for (int j = 2; j <= i / 2; j++) { if (i % j == 0) { counter++; break; } } if (counter == 0 && i != 1) { Console.Write("{0} ", i); } } Console.ReadKey(); }
回文数
是一组数字, 反转该数字之后与原来相同。例如: 121, 343, 7887 等。
回文字符串
一组文字, 返回该文字之后与原来相同。例如: ABA、我爱我等。
static void Main(string[] args) { Console.Write("请输入要检查的数字 : "); int number = int.Parse(Console.ReadLine()); int remineder, sum = 0; int temp = number; while (number > 0) { //得到余数 remineder = number % 10; //总和*10, 然后与余数相加 sum = (sum * 10) + remineder; //除以10,得到商 number = number / 10; } if (temp == sum) Console.WriteLine($"{temp}是回文"); else Console.WriteLine($"{temp}不是回文"); Console.ReadKey(); }
static void Main(string[] args) { Console.Write("输入要检查的回文: "); string name = Console.ReadLine(); string reverse = string.Empty; for (int i = name.Length - 1; i >= 0; i--) reverse += name[i]; if (name == reverse) Console.WriteLine($"{name}是回文"); else Console.WriteLine($"{name}不是回文"); Console.ReadKey(); }
static void Main() { Console.Write("输入要检查的回文: "); string name = Console.ReadLine(); string reverse = string.Empty; foreach (char c in name) reverse = c + reverse; if (name.Equals(reverse, StringComparison.OrdinalIgnoreCase)) Console.WriteLine($"{name}是回文"); else Console.WriteLine($"{name}不是回文"); Console.ReadKey(); }
static void Main() { Console.Write("输入要检查的回文: "); string name = Console.ReadLine(); char[] nameArray = name.ToCharArray(); Array.Reverse(nameArray); string reverse = new string(nameArray); if (name.Equals(reverse, StringComparison.OrdinalIgnoreCase)) Console.WriteLine($"{name}是回文"); else Console.WriteLine($"{name}不是回文"); Console.ReadKey(); }