几个常见的“算法”小程序

1. 将字符串“I am a good man” 输出为:“man good a am I”

 

using System;

namespace ConsoleApplication3
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string str = "I am a good man";
            
string[] arrayStr = str.Split(' '); //将字符串截取后存入数组中

            Array.Reverse(arrayStr);  //反序数组元素
            
foreach (var item in arrayStr)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

 

2.  有一列数1,1,2,3,5,........求第30个数. (用递归)  这个代码遍地都是,不解释......

 

using System;

namespace ConsoleApplication2
{
    
class Program
    {
        
static void Main(string[] args)
        {

            Console.WriteLine(Foo(
30));

            Console.ReadLine();
        }

        
public static int Foo(int i)
        {
            
if (i <= 0)
            {
                
return 0;
            }
            
else if (i > 0 && i <= 2)
            {
                
return 1;
            }
            
else
            {
                
return Foo(i - 1+ Foo(i - 2);
            }
        }
    }
}

 

3.有一组字符串,要求输入每种组合,例如:数组{"a","b","c"}输出组合为:ab ac ,ba bc, ca cb

 

using System;

namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {

            
string[] objstr = new string[]
            {
                
"a","b","c","d","e","f","g"
            };

            
for (int i = 0; i < objstr.Length; i++)
            {
                Console.WriteLine(
"第{0}组组合为:", i + 1);
                
for (int j = 0; j < objstr.Length; j++)
                {
                    
if (objstr[i] != objstr[j])
                    {
                        Console.Write(objstr[i] 
+ objstr[j] + " ");
                    }
                    
if (j == objstr.Length - 1)
                    {
                        Console.Write(
"\n\n");
                    }
                }
            }

            Console.ReadLine();
        }
    }
}

 

4.打印5000以内的对称数:如 11,111,121,1221(华为面试题)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DuiShu
{
    
class Program
    {
        
static void Main(string[] args)
        {

            GetA(
5000);

            Console.ReadLine();
        }

        
private static void GetA(int maxValue)
        {
            
string temp = String.Empty;
            
string partA = String.Empty;
            
string partB = String.Empty;
            
int index = 0;

            
for (int i = 0; i <= maxValue; i++)
            {
                
if (i > 10)
                {
                    temp 
= i.ToString();

                    index 
= temp.Length / 2;

                    
if (temp.Length % 2 == 0)
                    {
                        partA 
= temp.Substring(0, index);
                        partB 
= temp.Substring(index, index);
                    }

                    
else
                    {
                        partA 
= temp.Substring(0, index);
                        partB 
= temp.Substring(index + 1, index);
                    }

                    
if ((index == 1 && partA.Equals(partB)) ||
                        (index 
> 1 && partA.Equals(String.Concat(partB.ToArray().Reverse()))))
                    {
                        Console.WriteLine(temp);

                        
continue;
                    }
                }
            }
        }
    }
}

 

 

posted @ 2011-06-22 02:32  ゞ追忆o0ゞ  阅读(1768)  评论(6编辑  收藏  举报