本文来源:网易云课堂,大学计算机体系,C#程序设计,唐大仕,北京大学
1 角谷猜想
角谷猜想:角谷静夫是日本的一位著名学者.他提出了两条极简单的规则,可以对任何一个自然数进行变换,最终使它陷入“4-2-1”的死循环.
任意选一个整数N,规则如下:如果N为奇数,那么运算N*3+1;如果N为偶数,那么运算N/2。
得到第一个结果后,再重复按规则运算。这样一直算下去,你会发现最后数字会在一个循环圈里循环,这个循环圈是(4→2→1→4)。
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int N = 3333;
while(N != 1)
{
Console.Write(N + " ");
if (N % 2 == 1)
{
N = N * 3 + 1;
}
else
{
N = N / 2;
}
}
Console.Write(N + " ");
}
}
}
2 筛法求素数
//将所有的数排成一排,如果是某个数的倍数就划掉。关键点在于布尔数组的使用
//除法计算
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int N = 100;
bool[] a = new bool[N+1];//创建bool数组
//元素赋值,1不是素数
for (int i = 2; i < N; i++)
{
a[i] = true;
}
//从2开始遍历
for (int i = 2; i< N; i++)
{
//是i的倍数,则变为false
for (int j = i+1; j < N; j += 1)
{
if(a[j])
{
if (j % i == 0)//被整除,则为false
{
a[j] = false;
Console.Write(j + " ");
}
}
}
}
Console.WriteLine();
for (int i = 2; i <= N; i++)
{
if (a[i])
Console.Write(i + " ");
}
}
}
}
//将所有的数排成一排,如果是某个数的倍数就划掉。关键点在于布尔数组的使用
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int N = 100;
bool[] a = new bool[N+1];//创建bool数组
//元素赋值,1不是素数
for (int i = 2; i < N; i++)
{
a[i] = true;
}
//从2开始遍历
for (int i = 2; i< N; i++)
{
if(a[i])//如果还没被划掉,就继续
{
//是i的倍数,则变为false
for (int j = i * 2; j < N; j += i)
{
a[j] = false;
}
}
}
Console.WriteLine();
for (int i = 2; i <= N; i++)
{
if (a[i])
Console.Write(i + " ");
}
}
}
}
整体来看,乘法优于除法。
3 排块游戏
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int N = 4;//按钮的行、列数
Button[,] buttons = new Button[N, N];//按钮的数组
private void button1_Click(object sender, EventArgs e)
{
//打乱顺序
Shuffle();
}
private void Form1_Load_1(object sender, EventArgs e)
{
//产生所有按钮
GenerateAllButtons();
}
//打乱顺序
void Shuffle()
{
//多次随机交换两个按钮
Random rnd = new Random();
for (int i = 0; i < 100; i++)
{
int a = rnd.Next(N);
int b = rnd.Next(N);
int c = rnd.Next(N);
int d = rnd.Next(N);
Swap(buttons[a, b], buttons[c, d]);
}
}
//生成所有的按钮
void GenerateAllButtons()
{
int x0 = 100, y0 = 10, w = 45, d = 50;
for (int r = 0; r < N; r++)
for (int c = 0; c < N; c++)
{
int num = r * N + c;
Button btn = new Button();
btn.Text = (num + 1).ToString();
btn.Top = y0 + r * d;
btn.Left = x0 + c * d;
btn.Width = w;
btn.Height = w;
btn.Visible = true;
btn.Tag = r * N + c;//这个数据用来表示它所在行列位置
//注册事件
btn.Click += new EventHandler(btn_Click);
buttons[r, c] = btn;//放到数组中
this.Controls.Add(btn);//加到界面上
}
buttons[N - 1, N - 1].Visible = false;//最后一个不可见
//打乱顺序
Shuffle();
}
//交换两个按钮
void Swap(Button btna, Button btnb)
{
string t = btna.Text;
btna.Text = btnb.Text;
btnb.Text = t;
bool v = btna.Visible;
btna.Visible = btnb.Visible;
btnb.Visible = v;
}
//按钮点击事件处理
void btn_Click(object sender, EventArgs e)
{
Button btn = sender as Button;//当前点中的按钮
Button blank = FindHiddenButton();//空白按钮
//判断是否与空白块相邻,如果是,则交换
if (IsNeighbor(btn, blank))
{
Swap(btn, blank);
blank.Focus();
}
//判断是否完成了
if (ResultIsOk())
{
MessageBox.Show("ok");
}
}
//查找要隐藏的按钮
Button FindHiddenButton()
{
for (int r = 0; r < N; r++)
for (int c = 0; c < N; c++)
{
if (!buttons[r, c].Visible)
{
return buttons[r, c];
}
}
return null;
}
//判断是否相邻
bool IsNeighbor(Button btnA, Button btnB)
{
int a = (int)btnA.Tag;//Tag中记录是行列位置
int b = (int)btnB.Tag;
int r1 = a / N, c1 = a % N;
int r2 = b / N, c2 = b % N;
if (r1 == r2 && (c1 == c2 - 1 || c1 == c2 + 1)//左右相邻
|| c1 == c2 && (r1 == r2 - 1 || r1 == r2 + 1))
return true;
return false;
}
//检查是否完成
bool ResultIsOk()
{
for (int r = 0; r < N; r++)
for (int c = 0; c < N; c++)
{
if (buttons[r, c].Text != (r * N + c + 1).ToString())
{
return false;
}
}
return true;
}
}
}