一、求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m

//方法一,通过顺序规律写程序,同时也知道flag标志位的重要性。

static int F1(int m)
{
int sum =;
bool flag =true;
for (int i = ; i <= m; i++)
{
if (flag) //一次是默认是True,下下也为True
sum += i;
else
sum -= i;
flag = !flag; }
return sum;
} //通过奇偶性
static int F2(int m)
{
int sum = ;
for (int i = ; i <= m; i++)
{
if (i % >) //即为奇数
sum += i;
else
sum -= i;
}
return sum;
}

二,有一个字符串 "I am a good man",设计一个函数,返回 "man good a am I"。

static string Reverse()
{
string s = "I am a good man";
string[] arr = s.Split(' ');
string res = "";
for (int i = arr.Length - ; i >= ; i--)
{
res += arr[i];
if (i > )
res += " ";
}
return res;
}

三.冒泡排序

namespace BubbleSorter
{
class BubbleSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
BubbleSort(myArray);
} public static void BubbleSort(int[] myArray)
{
for (int i = ; i < myArray.Length-; i++) //由于数组的特点,从0开始,但myArray的长度为5,所以需要减1,实际进行了(0~3)4趟循环
{
for (int j =; j < myArray.Length -- i; j++) //内层循环的要点是相邻比较。当j=4的时候,就推出循环了
{
if (myArray[j] > myArray[j + ])
{
Swap(ref myArray[j], ref myArray[j + ]);
}
}
}
} private static void Swap(ref int left, ref int right)
{
int temp;
temp = left;
left = right;
right = temp;
} static void Main(string[] args)
{
int[] a = { , , , , };
BubbleSorter.Sort(a);
foreach (int i in a)
{
Console.WriteLine(i);
}
Console.Read();
}
}
}

四.选择排序

选择排序是一种简单直观的排序算法。它的工作原理如下。

首先在未排序列中找到最小的元素,存放到排序序列的起始位置。然后,在从剩余未排序元素中继续寻找最小的元素,放到排序序列末尾。以此类推,直到所有元素均排序完毕。

class SelectSorter
{
private static int[] myArray;
private static int arraySize;
public static void Sort(int[] a)
{
myArray = a;
arraySize = myArray.Length;
SelectSort(myArray);
}
public static void SelectSort(int[] myArray)
{
int i, j, smallest;
for(i=;i<myArray.Length-;i++) //数据起始位置,从0到倒数第二个数据
{
smallest = i; //记录最小数的下标
for (j = i + ; j < myArray.Length; j++) //在剩下的数据中寻找最小数
{
if (myArray[j] < myArray[smallest]) {
smallest = j; //如果有比它更小的,记录下标
}
}
Swap(ref myArray[i], ref myArray[smallest]); //将最小数据和未排序的第一个数交换
}
} private static void Swap(ref int left, ref int right)
{
int temp;
temp = left;
left = right;
right = temp;
} static void Main(string[] args)
{
int[] a = new int[] { , , , , };
SelectSorter.Sort(a);
for (int i = ; i < a.Length; i++)
{
System.Console.WriteLine(a[i]);
}
System.Console.Read();
}
}

五.有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

class Program
{
static void Main(string[] args)
{ //有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
//分解题目
//条件:四个数字1、2、3、4 ;三位数:百位、十位、个位
//要求:互不相同;无重复数字:每个数字在三位中只出现一次
//结果:多少个? 都是多少? int count = ; //统计个数
for (int bw = ; bw <= ; bw++)
{
for (int sw = ; sw <= ; sw++)
{
if (sw!= bw) //很显然,只有百位和十位不同的情况下才能谈个位。
{
for (int gw = ; gw <= ; gw++)
{
if (gw != sw && gw != bw) //百位用过的,十位就不能用;百位和十位都用过的,个位就不能用
{
count++;
Console.WriteLine("{0}{1}{2}", bw, sw, gw);
}
}
}
}
}
Console.WriteLine("一共有{0}个", count);
Console.Read(); }
}

选择排序方法2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 选择排序
{
//选择排序与冒泡排序比较的次数是一样的
//选择排序的交换次数要比冒泡排序的交换次数少 class Program
{
static void Main(string[] args)
{
int[] arr = { , , , , , , , }; //待排序数组
SelectSort(arr); //调用选择排序函数
} private static void SelectSort(int[] arr)
{
int temp = ;
for (int i = ; i < arr.Length - ; i++)
{
int minVal = arr[i]; //假设 i 下标就是最小的数
int minIndex = i; //记录我认为最小的数的下标 for (int j = i + ; j < arr.Length; j++) //这里只是找出这一趟最小的数值并记录下它的下标
{
//说明我们认为的最小值,不是最小
if (minVal > arr[j]) //这里大于号是升序(大于是找出最小值) 小于是降序(小于是找出最大值)
{
minVal = arr[j]; //更新这趟最小(或最大)的值 (上面要拿这个数来跟后面的数继续做比较)
minIndex = j; //记下它的下标
}
}
//最后把最小的数与第一的位置交换
temp = arr[i]; //把第一个原先认为是最小值的数,临时保存起来
arr[i] = arr[minIndex]; //把最终我们找到的最小值赋给这一趟的比较的第一个位置
arr[minIndex] = temp; //把原先保存好临时数值放回这个数组的空地方, 保证数组的完整性
}
//控制台输出
foreach (int item in arr)
{
Console.WriteLine("C#遍历:{0}", item);
}
}
}
}

转载至:https://www.cnblogs.com/HiWord-ToFutureGKY/p/7742559.html

最新文章

  1. JAVA 中配置IKAnalyzer扩展词库和停止词库
  2. HDU 5795 A Simple Nim 打表求SG函数的规律
  3. Unity引擎下最快的Xml读取器:UnityRapidXml
  4. .net core Jwt 添加
  5. Unity透明材质Batch
  6. MFC 修改 单文档 SDI 窗体 标题
  7. java和linux的编码
  8. 【面试虐菜】—— LVS负载均衡
  9. 不是 EPUB ,而是 ePub
  10. 检查mysql数据库是否存在坏表脚本
  11. C# 二进制替换第一弹 byte 数组替换
  12. 南阳oj-喷水装置(一)
  13. edit distance leetcode
  14. 内网穿透神器ngrok(转)
  15. wpf之ListBox横向显示所有ListBoxItem
  16. Java8学习笔记----Lambda表达式 (转)
  17. 【Android】TextView跑马灯效果
  18. (3)简单说说java中的异常体系
  19. scrapy 爬虫基础
  20. mybatis中分页插件PageHelper的使用

热门文章

  1. [makefile]如何设置不同目录的代码(.c),生成到指定目录下(./debug/.o))
  2. Win32 CRT and MFC 清单文件.manifest配制
  3. FrameLsyout
  4. day08 数字,字符串类型内置方法
  5. struts2配置 匹配原则 配置各项默认
  6. vue自定义select组件
  7. 【HiJ1m】在NOIP2017前写过的有用的东西汇总
  8. Vue的数据依赖实现原理简析
  9. TI低功耗蓝牙(BLE)介绍
  10. if判断语句