1、1、2、3、5、8、13、21、34...... 求第30位数是多少,用递归算法实现
public static int Fo(int i)
{
if (i<=0)
{
return 0;
}
else if (i > 0 && i <= 2)
{
return 1;
}
else
{
return Fo(i - 1) + Fo(i - 2);
}
}
请编程实现一个冒泡排序算法?
public static void Maopo()
{
int[] array={10,9,20,15,30};
int temp = 0;//定义一个空变量
for (int i = 0; i < array.Length-1; i++)
{
for (int j =i+1; j < array.Length; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i]=array[j];
array[j] = temp;
}
}
}
for (int i = 0; i <array.Length; i++)
{
Console.WriteLine(array[i]);
}
}
求以下表达式的值:1-2+3-4+……+m
public static void Sum()
{
Console.WriteLine("请输入一个数字");
int num =int.Parse( Console.ReadLine());
int sum = 0;
for (int i = 0; i < num+1; i++)
{
if (i % 2 == 1)
{
sum += i;
}
else
{
sum = sum - i;
}
}
Console.WriteLine(sum);
}
求水仙花数
public static void SXH()
{
int a,b,c;
for (int i= 100; i < 1000; i++)
{
a = i / 100;
b = i % 100 / 10;
c = i % 100 % 10;
if (i == a * a * a + b * b * b + c * c * c)
{
Console.WriteLine("水仙花数有{0}",i);
}
}
}
计算一个数组arr所有元素的和
public static void QH()
{
int[] arr1={1,2,3,4,5,6,7,8,9};
int sum = 0;
for (int i = 0; i < arr1.Length; i++)
{
sum += arr1[i];
}
Console.WriteLine(sum.ToString());
}
编写一个方法去掉数组里面 重复元素后的集合
public static void Del()
{
//定义一个数组存放要去重的数据
var ar = new int[5] { 1, 2, 1, 1, 34 };
//Distinct去重
var q = ar.Distinct().ToArray();
for (int i = 0; i < q.Length; i++)
{
Console.WriteLine(q[i]);
}
}
判断一个字符串中每个字符出现的次数
public static void TJ()
{
string str = "jintiantianqihaoqinglang";
var charCounts = from c in str
group c by c into g
orderby g.Count() descending, g.Key
select new { Char = g.Key, Count = g.Count() };
foreach (var charCount in charCounts)
{
Console.WriteLine("字母{0}出现了 {1}次", charCount.Char, charCount.Count);
}
}
根据你输入的字符来判断这个字符出现的次数
public static void Shu()
{
Console.WriteLine("请输入字符串");
string a = Console.ReadLine();
Console.WriteLine("请输入你要判断的字符串");
string b = Console.ReadLine();
int count = 0;
for (int i = 0; i < a.Length; i++)
{
if (a[i].ToString()==b)
{
count++;
}
}
Console.WriteLine("你输入的字符是{0},出现的次数是{1}",b,count);
}