回文算法

//所谓回文是指向前和向后拼写都完全一样的字符。例如,“dad”、“madam”都是回文。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace vc_test
{
    class Program
    {
        private  ArrayList list;
        private int p_index;
        public Program()
        {
            list = new ArrayList();
            p_index = -1;
        }
        static void Main(string[] args)
        {           
            Program myprograme = new Program();                              
                string ch;
                string word = " 1221 ";
                bool ispalindrome = true;
                for (int x = 0; x < word.Length; x++)          
                    //添加到动态数组中
                    myprograme.push(word.Substring(x, 1));

                    int pos=0;
                    while (myprograme.list.Count > 0)
                    {
                        ch = myprograme.pop().ToString();
                        if (ch != word.Substring(pos, 1))
                        {
                            ispalindrome = false;
                            break;
                        }
                        pos++;
                    }
                    if (ispalindrome)
                        Console.WriteLine(word + " is a paliddrome");
                    else
                        Console.WriteLine(word+" is not a palindrome.");       
            Console.ReadKey();
        }
        //params可以指定不确定的参数数量
        static int sumNums(params int[] nums)
        {
            int sum = 0;
            for (int j = 0; j <= nums.GetUpperBound(0); j++)
            {
                //Console.Write("{0},",nums [i]);
                sum+=nums[j];
            }
            return sum;
        } 
        //插入
       void push(object item)
        {            
            list.Add(item );
            p_index++;
        }
        //移除
       public object pop()
        {
        object obj=list[p_index];
        list.RemoveAt(p_index);
        p_index--;
        return obj;
        }     
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值