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

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 ;
                    }
                }
            }
        }
    }
}

 

 

转载于:https://www.cnblogs.com/zhuiyi/archive/2011/06/22/2086496.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值