今天去面试了,就给我两个题:
1.逆序一个字符串
2.计算一个整型变量中二进制“1”的个数。

解决方案 »

  1.   

    是不是面试题可以带回家做啊。第一个问题非常简单,使用逆序循环:
    String a = "今天去面试了,就给我两个题:";
    StringBuilder b = new StringBuilder();
    for (int i = a.Length - 1; i >= 0; i--)
    {
        b.Append(a[i]);
    }
    String c = b.ToString();//这就是反序字符串啦。第二个问题也很简单啊,只要不停地那那个数除以2,统计总共不能被整除的次数,就是那个“1”的个数了。
      

  2.   

    提供一种思路,但效率不高:            string str = "ojlovecd";
                char[] cArray = str.ToCharArray();
                Array.Reverse(cArray);
                string result = new string(cArray);
                Console.WriteLine(result);
                int i = 12345678;
                str = Convert.ToString(i, 2);
                int count = 0;
                foreach (char c in str)
                    if (c == '1')
                        count++;
                Console.WriteLine(count);
      

  3.   

     public static string Reverse(string str)
            {
                byte[] byteStr = System.Text.Encoding.ASCII.GetBytes(str);
                Stack<byte> stack = new Stack<byte>();
                for (int i = 0; i < byteStr.Length; i++)
                {
                    stack.Push(byteStr[i]);
                }
                byteStr = stack.ToArray();
                return System.Text.Encoding.ASCII.GetString(byteStr);
            }string str= "";
         char[] arr = str.ToCharArray();
         Array.Reverse(arr);
         str= new string(arr);int c = 0;
    for (int i = 0; i < str.Length; i++)
    {
        if (str[i] == '1')
        {
            c++;
        }
    }
    c = str.Length - str.Replace("1", String.Empty).Length;c = str.Split(new char[] { '1' }).Length - 1;
      

  4.   

    我的想法和一楼的差不多,只不过我的是需要你输入你要求反转的字符串,算是一个简单的交互!
                Console.WriteLine("请输入你要反转的字符串");
                String str = Console.ReadLine();
                StringBuilder str1 = new StringBuilder();
                for (int i = str.Length - 1; i >= 0; i--)
                {
                    str1.Append(str[i]);
                }
                String str2 = str1.ToString();
                Console.WriteLine(str2);   LZ可以参考下!
      

  5.   

    2.计算一个整型变量中二进制“1”的个数。int a = 整型变量;
    int count = 0;
    while(a > 0){
       if(a & 1 = 1)count ++;
       a >>= 1;
    }
      

  6.   

    也可以这样:
                string str = "I Love You!";
                char[] cArray = str.ToCharArray();
                Array.Reverse(cArray, 2, 4);//此处是实现从索引为2(即第三个字符)开始,反转四个字符!
                string result = new string(cArray);
                Console.WriteLine(result);           输出的结果为:I evoL You!
      

  7.   

    高歌的一个解法 public static string Reverse(string str)
            {
                if (string.IsNullOrEmpty(str))
                {
                    throw new ArgumentException("参数不合法");
                }
                char[] chars = str.ToCharArray();
                int begin = 0;
                int end = chars.Length - 1;
                char tempChar;
                while (begin < end)
                {
                    tempChar = chars[begin];
                    chars[begin] = chars[end];
                    chars[end] = tempChar;
                    begin++;
                    end--;
                }
                string strResult = new string(chars);
                return strResult;
            }http://blog.csdn.net/amandag/archive/2009/06/08/4252723.aspx
      

  8.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class NET_Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string ss = ReverseStr();
            Response.Write(ss);
            
        }
        //1.逆序一个字符串
        public string ReverseStr()
        {
            string s1 = "";
            string Str = "ABCDEFG";
            for (int i = Str.Length; 0 < i; i--)
            {
                s1 += Str.Substring(i - 1, 1);
            }  
            return s1;
        }
    }
      

  9.   


       string s == "xxxxx";
                char[] chars = s.ToCharArray();
                chars.Reverse();int i = xxx;
    string s = Convert.ToString(i, 2);            int i = 234;
                string s = Convert.ToString(i, 2);
                int count = (from ss in s
                            where ss == '1'
                            select ss).Count();
      

  10.   

    贴错了这int i = xxx;
    string s = Convert.ToString(i, 2);
    没有的
      

  11.   

    public void  ReverseStr(string a)
    {
         stringBuild sb = new stringBuilder();
         for(int i=a.length-1; i.=0; i--)
         { sb.append(a[i]);}
         string aa= sb.tostring();
      

  12.   

    int i = 12345678;
                str = Convert.ToString(i, 2);
                int count = 0;
                foreach (char c in str)
                    if (c == '1')
                        count++;
                Console.WriteLine(count);
      

  13.   

    我有点不明白13楼的chars.Reverse();有这个方法吗
    int count = (from ss in s where ss == '1' select ss).Count(); 这个是sql语句吧 放这里会报错的吧