学习过程中的一些设计模式和算法记录

好记性不如烂笔头,烂笔头不如理解于心
今天整理电脑中的文件,顺便把以前学习的记录坐下总结,便于以后随时查看

单例模式

public class Singleton
{
	private static Singleton instance=null;
	public static Singleton get Instance()
	{
		if(instance==null)
		{
			instance=new Singleton();
		}
		return instance;
	}
}

简单工厂模式

public class SimpleFactory
{
    public Pizza CreatePizza(string pizzaType)
     {
         switch (pizzaType)
         {
             case "cheese":
             	return new CheesePizza();
             case "ApplePie":
            	 return new ApplePiePizza();
             default:
             	return new SomeOtherPizza();
          }
      }
 }

计算2的n次幂,根据用户输入

var n=	parseInt(window.prompt());
var i=1;
var total=2;
while(i<n){
	total*=2;
	i++;
}
document.write(total);

//输入5,结果32

计算n的阶乘,n可输入

var n=parseInt(window.prompt());
var total=n;
	while(n>1){
	total=total*(n-1)
		n--;
	}
document.write(total);

//输入5,结果120

斐波那契数列:1 1 2 3 5 8 ?

var n=7;
var first=1,second=1,third;
	if(n>2){
	for(var i=0;i<n-2;i++){
		third=first+second;
		first=second;
		second=third;
		}
	}else{
		document.write(1);
}
document.write(third);

//13

递归算法实现

private int getValue(int i)
{
	if(i<=0)
		return 0;
	else if(i>0&&i<=2)
		return 1;
	else
		return getValue(i-1)+getValue(i-2);
}

输出2到100内的所有质数

var count=0;
for(var i=1;i<100;i++){
	for(var j=1;j<=i;j++){
		if(i%j==0){
			count++;
		}
	}
	if(count==2){
		document.write(i+" ");
	}
	count=0;
}

//2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

冒泡排序法

private void bubbleSort(int[]array)
{
	int i,j,temp;
	for(j=0;j<array.length;j++)
	{
		for(i=0;i<array.length-1-j;i++)
		{
			if(array[i]>array[i+1])
			{
				temp=array[i];
				array[i]=array[i+1];
				array[i+1]=temp;
			}
		}
	}
}

1-2+3-4+ , +m

private int getResult(int Num)
{
	int Sum = 0 ;
	for (int i = 0 ; i < Num + 1 ; i++)
	{
		if((i%2) == 1)
		{
			Sum += i ;
		}
		else
		{
			Sum = Sum - i ;
		}
	}
	return Sum;
}

99乘法表

 for (int a = 1; a <= 9; a++)
 {
     for (int b = 1; b <= a; b++)
     {
         Console.Write("{0}*{1}={2}\t", a, b, a * b);//Write
          if (a == b)
          {
               Console.WriteLine();
          }
    }
}

直角三角形

for (int a = 1; a <= 9; a++)
{
   for (int b = 1; b <= a; b++)
       {
          Console.Write("*");//Write
          if (a == b)
           {
               Console.WriteLine();
           }
       }
}

等腰三角形

for (int i = 1; i <= 5; i++)
{
     for (int a = 1; a <= 5 - i; a++)
     {
          Console.Write(" ");
     }
     for (int b = 1; b < 2 * i; b++)
     {
          Console.Write("*");
     }
     Console.WriteLine();
}

冒泡排序法

int[] age = { 25, 23, 27, 22, 20 };
int temp;
for (int a = 0; a < age.Length - 1; a++)
{
    for (int b = 0; b < age.Length - 1 - a; b++)
    {
        if (age[b] > age[b + 1])
        {
              temp = age[b];
              age[b] = age[b + 1];
              age[b + 1] = temp;
         }
    }
}

递归 5x4x3x2x1

private static int DiGui(int num)
{
   if (num == 1)
       return 1;
   else
       return num * DiGui(num - 1);
}

后续、、、、、、

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值