黑马程序员技术交流社区

标题: 吕老师视频面试的题,可惜不会做,求救 [打印本页]

作者: wanghuailin1030    时间: 2013-6-21 21:02
标题: 吕老师视频面试的题,可惜不会做,求救
本帖最后由 wanghuailin1030 于 2013-6-26 11:39 编辑

把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
61.54.231.245
61.54.231.9
61.54.231.246
61.54.231.48
61.53.231.249

参考大家的意见,这是最后的源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
namespace temp
{
    class Program
    {
        //把一下IP地址存入一个TXT文件,编写程序把这些IP按数值大小,从小到大排序并打印出来IP地址排序文件操作
        //61.52.231.245
        //60.54.231.9
        //61.54.234.246
        //61.54.231.48
        //60.53.231.249
        static void Main(string[] args)
        {
            FileStream fs = new FileStream(@"text.txt", FileMode.Open, FileAccess.Read);//把文件方法放在debug里
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            ArrayList strIP = new ArrayList();
            while (sr.Peek() >= 0)
            {  //读到最后一行            
                strIP.Add(sr.ReadLine());
            }
            sr.Close();
            double[] temp = new double[5];
            for (int i = 0; i < strIP.Count; i++)
            {
                string[] p = strIP.ToString().Split('.');
                for (int j = 0; j < p.Length; j++)
                {
                    temp += (double.Parse(p[j])) * Math.Pow((double)256,(double)(p.Length - 1 - j));
                }
            }
            for (int i = 0; i < strIP.Count - 1; i++)
            {
                for (int j = 0; j < strIP.Count - 1; j++)
                {
                    if (temp[j] > temp[j + 1])
                    {
                        double tempd = temp[j + 1];
                        temp[j + 1] = temp[j];
                        temp[j] = tempd;
                        string strBu = (string)strIP[j];
                        strIP[j] = strIP[j + 1];
                        strIP[j + 1] = strBu;
                    }
                }
            }
            for (int i = 0; i < strIP.Count; i++)
            {
                //Console.WriteLine(temp);
                Console.WriteLine(strIP);
            }
            Console.ReadKey();
        }
    }
}

yemp.jpg (53.96 KB, 下载次数: 0)

QQ上问题的截图

QQ上问题的截图

作者: 王靖远    时间: 2013-6-21 21:15
用一个读取流获取这些IP,存入一个set集合中,给集合自定义一个比较器,然后遍历集合就可以了。
作者: 逆_水_寒    时间: 2013-6-21 21:37
这是一个TXT文本,并且是一行一行写的,用  Line 能读出 “/r/n”,一行一行读出来放入数组再遍历吧。
第九天基础视频里的那一题与这个类似,看懂了那个,这个也就好做了。
作者: 曾大鹏    时间: 2013-6-21 21:52
文件操作就不说了 简单的
说一下排序把。。ip的 里面的数字范围是0~255 你可以把他看成一个256进制数  然后把256进制转换成十进制 在排序就OK了
作者: ﹏Lifeヽ    时间: 2013-6-21 22:35
private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog()==DialogResult.Cancel)
            {
                return;
            }
            textBox1.Text = ofd.FileName;
            string[] strtxt = File.ReadLines(textBox1.Text, Encoding.Default).ToArray();
            
            string[] str1 = strtxt[0].Split('.');
            string a = str1[3];
            string[] str2 = strtxt[1].Split('.');
            string b = str2[3];
            string[] str3 = strtxt[2].Split('.');
            string c = str3[3];
            string[] str4 = strtxt[3].Split('.');
            string d= str4[3];
            string[] str5 = strtxt[4].Split('.');
            string f= str5[3];
                //MessageBox.Show(d.ToString());
            

        }
获取 每个IP 最后一个值 a,b,c,d,f   把获取的值放到一个数组里
比较最后一个数的大小就好了,用最简单的冒泡排序就ok了
然后在输出



这个我想的最笨的方法!!!   哈哈     希望能帮助你!



作者: 274997322    时间: 2013-6-21 22:47
本帖最后由 274997322 于 2013-6-22 22:33 编辑

     FileStream fs = new FileStream(@"text.txt", FileMode.Open, FileAccess.Read);//把txt文件放到Debug目录下
            StreamReader sr = new StreamReader(fs, Encoding.Default);

            ArrayList strIP = new ArrayList();

            while (sr.Peek()>=0)   //读到最后一行
            {
                strIP.Add(sr.ReadLine());
            }

            //冒泡排序
            for (int i = 0; i < strIP.Count-1; i++)
            {
                for (int j = 0; j < strIP.Count-1; j++)
                {
                    string[] p = strIP[j].ToString().Split('.');      //用'.'拆分IP
                    string[] p2 = strIP[j + 1].ToString().Split('.'); //
                    for (int k = 0; k < p.Length; k++)              //再加一个循环,比较每一段
                    {
                        if (int.Parse(p[k]) > int.Parse(p2[k]))           //依次比较每一段的大小
                        {
                            string strBu = (string)strIP[j];
                            strIP[j] = strIP[j + 1];
                            strIP[j + 1] = strBu;
                            break;
                        }
                    }

                }
            }

            for (int i = 0; i < strIP.Count; i++)
            {
                Console.WriteLine(strIP);
            }

            Console.ReadKey();

的确是我想的太简单了,修改了一下,请大家帮忙看还有没有什么低级错误没

作者: wanghuailin1030    时间: 2013-6-21 22:59
274997322 发表于 2013-6-21 22:47
FileStream fs = new FileStream(@"text.txt", FileMode.Open, FileAccess.Read);//把txt文件放到Debug ...

能解,我仔细看看
作者: 陈行    时间: 2013-6-21 23:04
和你一样的遭遇
作者: 王靖远    时间: 2013-6-21 23:09
这需要给建一个Ip类比较合适的。我来写试试。
作者: 吴承烨    时间: 2013-6-22 00:30
文件+冒泡+字符串
作者: 吴承烨    时间: 2013-6-22 00:46
淡.。 发表于 2013-6-21 23:04
和你一样的遭遇

你的也是一样的题吗?
作者: 王靖远    时间: 2013-6-22 02:12
  1. /*
  2. * 需求:把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
  3. 61.54.231.245
  4. 61.54.231.9
  5. 61.54.231.246
  6. 61.54.231.48
  7. 61.53.231.249
  8. 思路:
  9. 1.将ip封装成对象,并自定义排序方法。
  10. 2.将ip写入到一个txt文件中。
  11. 3.将ip打印出来。

  12. * */
  13. package com.debug;
  14. import java.io.*;
  15. import java.util.*;
  16. class IpComparable implements Comparator<Ip>//自定义的比较器
  17. {
  18. public int compare(Ip ip1,Ip ip2)
  19. {
  20. String[] s1 = ip1.toString().split("\\.");
  21. int []i1 = new int[s1.length];
  22. for(int x=0;x<i1.length;x++)
  23. {
  24. i1[x]=new Integer(s1[x]);

  25. }

  26. String[] s2 =ip2.toString().split("\\.");
  27. int []i2 = new int[s2.length];
  28. for(int x=0;x<i2.length;x++)
  29. {
  30. i2[x]=new Integer(s2[x]);
  31. }
  32. int num0 = i1[0]-i2[0];
  33. int num1 = i1[1]-i2[1];
  34. int num2 = i1[2]-i2[2];
  35. int num3 = i1[3]-i2[3];

  36. if(num0==0 && num1==0 && num2==0)
  37. return num3;
  38. else if(num0==0 && num1==0)
  39. return num2;
  40. else if(num0==0)
  41. return num1;
  42. else
  43. return 0;
  44. }

  45. }
  46. class Ip //封装的Ip类
  47. {
  48. private String ip;
  49. Ip(String ip)
  50. {
  51. this.ip = ip;
  52. }

  53. public String getIp()
  54. {
  55. return ip;
  56. }
  57. public void setIp(String ip)
  58. {
  59. this.ip = ip;
  60. }
  61. public String toString()
  62. {
  63. return this.getIp();
  64. }
  65. public int hashCode()
  66. {
  67. String[] s = ip.toString().split("\\.");
  68. int[] i = new int[s.length];
  69. for(int x=0;x<i.length;x++)
  70. {
  71. i[x]=new Integer(s[x]);
  72. }
  73. int num = 0;
  74. for(int x=0;x<i.length;x++)
  75. {
  76. num+=i[x]*33;
  77. }
  78. return num;
  79. }

  80. }

  81. class ipSort
  82. {
  83. public static void main(String [] args)
  84. {
  85. Set<Ip> set = new TreeSet<Ip>(new IpComparable());
  86. set.add(new Ip("61.54.231.245"));
  87. set.add(new Ip("61.54.231.9"));
  88. set.add(new Ip("61.54.231.246"));
  89. set.add(new Ip("61.54.231.48"));
  90. set.add(new Ip("61.53.231.249"));
  91. Iterator<Ip>it = set.iterator();
  92. while(it.hasNext())
  93. {
  94. System.out.println(it.next().toString());
  95. }
  96. BufferedWriter bufw =null;
  97. try
  98. {
  99. Iterator<Ip>it1 = set.iterator();
  100. bufw = new BufferedWriter(new FileWriter("ip.txt"));
  101. while(it1.hasNext())
  102. {
  103. bufw.write(it1.next().toString());
  104. bufw.newLine();
  105. }

  106. }
  107. catch(IOException e)
  108. {
  109. throw new RuntimeException("写入失败'");
  110. }
  111. finally
  112. {
  113. try
  114. {
  115. if(bufw!=null)
  116. bufw.close();
  117. }
  118. catch(IOException e)
  119. {
  120. throw new RuntimeException("写入关闭失败");
  121. }
  122. }


  123. }
  124. }
复制代码
不知道符合不符合要求。有需求再说
作者: 高腾    时间: 2013-6-22 09:29
  1.             string[] lines = System.IO.File.ReadAllLines(@"F:\1.txt", Encoding.Default);//读取文件

  2.             for (int i = 0; i < lines.Length; i++)  //冒泡排序
  3.             {
  4.                 for (int j =i+1; j < lines.Length; j++)
  5.                 {
  6.                     string[] line = lines[i].Split('.');    //取出前一行
  7.                     string lastIP = line[line.Length - 1];  //得到前一行的IP地址最后一个值
  8.                     string[] line2 = lines[j].Split('.');   //取出下行
  9.                     string lastIP2 = line2[line2.Length - 1];   //得到下行IP地址最后一个值
  10.                     if (Convert.ToInt32(lastIP2) < Convert.ToInt32(lastIP)) //比较,如果后面的小于前面的,则交换
  11.                     {
  12.                         string temp = lines[j];
  13.                         lines[j] = lines[i];
  14.                         lines[i] = temp;
  15.                     }
  16.                 }
  17.             }
  18.             //遍历lines数组
  19.             foreach (string str in lines)
  20.             {
  21.                 Console.WriteLine(str);
  22.             }

  23.             Console.ReadKey();
复制代码

作者: 关关雎鸠    时间: 2013-6-22 09:45
不知道就只有这些IP地址,还是另外随意写?如果说只有这些,那只判断最后一个数然后比较大小就可以以了。。
作者: 关关雎鸠    时间: 2013-6-22 11:30
本帖最后由 关关雎鸠 于 2013-6-22 11:49 编辑

我写了一个,这个题目还是有点意思的。。

效果图:
  1. //读取文本的IP地址
  2. using(FileStream fileStream = new FileStream("IPList.txt", FileMode.Open, FileAccess.Read)) {
  3. using(StreamReader readerStream = new StreamReader(fileStream)) {
  4.        string subIP = string.Empty; //子IP地址
  5.        List<int> list = new List<int>(); //存放IP地址的最后一个数
  6.        while(readerStream.Peek() > -1) { //循环读取
  7.             string IP = readerStream.ReadLine(); //读取一行
  8.             subIP = IP.Substring(0, IP.LastIndexOf('.') + 1); //截取IP地址
  9.             //最后一个数
  10.             int lastNum = int.Parse(IP.Substring(IP.LastIndexOf('.') + 1));
  11.             list.Add(lastNum); //添加到List集合中
  12.        }
  13.        //用于拼接IP地址
  14.        StringBuilder sb = new StringBuilder(list.Count);
  15.        //冒牌排序比较大小
  16.        for(int i = 0; i < list.Count - 1; i++) {
  17.             for(int j = 0; j < list.Count - 1 - i; j++) {
  18.                 if(list[j] < list[j + 1]) {
  19.                     int temp = list[j];
  20.                     list[j] = list[j + 1];
  21.                     list[j + 1] = temp;
  22.             }
  23.        }
  24.        //每次比较后最小的数排在后面,然后拼接IP地址
  25.        sb.Append(subIP + list[list.Count - 1 - i] + "\n");
  26.       }
  27.       //上面排序完之后,最后一个数没有被拼接,在这里拼接最后一个数。
  28.       sb.Append(subIP + list[0]);
  29.       Console.WriteLine(sb.ToString());
  30.      }
  31. }
复制代码

QQ拼音截图未命名.png (10.29 KB, 下载次数: 0)

QQ拼音截图未命名.png

作者: 庞登升    时间: 2013-6-22 13:40
10分钟还真的蛮有难度的
作者: 吴承烨    时间: 2013-6-22 16:25
你确定是
61.54.231.48
61.53.231.249
61.53????????
作者: 吴承烨    时间: 2013-6-22 21:31
这样做只能比较后面的.61.54.231.48和61.53.231.249这个比较不了.
作者: 吴承烨    时间: 2013-6-22 21:31
这样做只能比较后面的.61.54.231.48和61.53.231.249这个比较不了.
作者: 吴承烨    时间: 2013-6-22 21:31
这样做只能比较后面的.61.54.231.48和61.53.231.249这个比较不了.
作者: yangaidongcumt    时间: 2013-6-22 22:01
274997322 发表于 2013-6-21 22:47
FileStream fs = new FileStream(@"text.txt", FileMode.Open, FileAccess.Read);//把txt文件放到Debug ...

这个肯定不行吧!你没发现最后一个ip的前面和前几个的不一样么
作者: yangaidongcumt    时间: 2013-6-22 22:04

貌似不行,最后一个ip和前面的形式不一样~~~~~
作者: 姚团结    时间: 2013-6-23 10:18
看看学习一下
作者: wanghuailin1030    时间: 2013-6-23 10:37
关关雎鸠 发表于 2013-6-22 11:30
我写了一个,这个题目还是有点意思的。。

效果图:

不成啊,这个把文件的原意都给改了,你看看截图,是61.53和61.54比较
作者: 曾大鹏    时间: 2013-6-23 16:35
{:soso_e140:}楼上的想法都不好。。都只能解决当前的问题。。给你任意的IP 让你排序 你们的方法都不行。
正确的想法:Ip地址格式为:a.b.c.d
每个数字范围在0~255之间 那么我们可以把它们看成一个四位的256进制数
然后转换成十进制 =a*256^3+b*256^2+c*256^1+d*256^0
然后根据对应的十进制大小排序就OK了。。


作者: wanghuailin1030    时间: 2013-6-24 09:17
曾大鹏 发表于 2013-6-23 16:35
楼上的想法都不好。。都只能解决当前的问题。。给你任意的IP 让你排序 你们的方法都不行。
正 ...

a*256^3+b*256^2+c*256^1+d*256^0
这个看不太懂,能再解释一下么?^是开方的意思么?
还有6楼的方法能解决任意IP排序的问题
作者: 曾大鹏    时间: 2013-6-24 10:41
wanghuailin1030 发表于 2013-6-24 09:17
a*256^3+b*256^2+c*256^1+d*256^0
这个看不太懂,能再解释一下么?^是开方的意思么?
还有6楼的方法能解 ...

多少次放的意思。。
举个例子 比如说二进制100装10进制
=1*2^2+0*2^1+0*2^0=4

作者: aa26963    时间: 2013-6-24 11:05
哥们,面试怎么样啊,我后天面试,能给我介绍一下么,我qq
940969973
作者: wanghuailin1030    时间: 2013-6-24 12:02
曾大鹏 发表于 2013-6-24 10:41
多少次放的意思。。
举个例子 比如说二进制100装10进制
=1*2^2+0*2^1+0*2^0=4

Ok,我明白了,若果用十进制就更好理解了,开了几次方就分别是个十百千位。
至于怎么实现,我在实践一下
作者: wanghuailin1030    时间: 2013-6-25 10:03
本帖最后由 wanghuailin1030 于 2013-6-25 10:06 编辑
曾大鹏 发表于 2013-6-23 16:35
楼上的想法都不好。。都只能解决当前的问题。。给你任意的IP 让你排序 你们的方法都不行。
正 ...


你看看我实现的源码,不知道什么原因,转换进制时算出来的数和实际的数差个一两百
namespace temp
{
    class Program
    {
        //把一下IP地址存入一个TXT文件,编写程序把这些IP按数值大小,从小到大排序并打印出来IP地址排序文件操作
//61.52.231.245
//60.54.231.9
//61.54.234.246
//61.54.231.48
//60.53.231.249
        static void Main(string[] args)
        {
            FileStream fs = new FileStream(@"text.txt", FileMode.Open, FileAccess.Read);//把文件方法放在debug里
            StreamReader sr = new StreamReader(fs, Encoding.Default);

            ArrayList strIP = new ArrayList();

            while (sr.Peek() >= 0)
            {  //读到最后一行            
                strIP.Add(sr.ReadLine());
            }
            sr.Close();
           int[] temp = new int[5];
            for (int i = 0; i < strIP.Count; i++)
            {
                string[] p = strIP.ToString().Split('.');
                for(int j=0;j<p.Length;j++)
                {
                    //Console.WriteLine(60 * 256 ^ 3 + 53 * 256 ^ 2 + 231 * 256 ^ 1 + 249 * 256 ^ 0);
                    temp += (int.Parse(p[j])) * (256 ^ (p.Length - 1 - j));
                }
            }

            for (int i = 0; i < strIP.Count; i++)
            {
                Console.WriteLine(temp);
                Console.WriteLine(strIP);
            }
            Console.WriteLine();
            for (int i = 0; i < strIP.Count-1; i++)
            {
                for (int j = 0; j < strIP.Count-1; j++)
                {
                    if (temp[j] > temp[j+1])
                    {
                        int tempd = temp[j+1];
                        temp[j+1] = temp[j];
                        temp[j] = tempd;

                        string strBu = (string)strIP[j];
                        strIP[j] = strIP[j + 1];
                        strIP[j + 1] = strBu;

                    }
                }
            }

            for (int i = 0; i < strIP.Count; i++)
            {
                Console.WriteLine(temp);
                Console.WriteLine(strIP);
            }
            Console.ReadKey();

        }

    }
}


作者: ◇゛ 仅此而以    时间: 2013-6-25 12:36
^   这个东西在程序里是按位异或的意思 。
作者: 曾大鹏    时间: 2013-6-25 14:29
本帖最后由 曾大鹏 于 2013-6-25 14:31 编辑
wanghuailin1030 发表于 2013-6-25 10:03
你看看我实现的源码,不知道什么原因,转换进制时算出来的数和实际的数差个一两百
namespace temp
{

temp += (int.Parse(p[j])) * (256 ^ (p.Length - 1 - j));
这里错了。。
^ 在C#中表示异或,不是次方
计算次方 可以用Math.Pow()函数
改成这样
  1. temp[i]=0;
  2. for(int j=0;j<p.Length;j++)
  3. {
  4.   temp[i]=temp[i]*256+int.Parse(p[j]);
  5. }
复制代码

作者: 风雪再现    时间: 2013-6-25 16:33
string path = @"E:\IP地址.txt";
            string[] strs = System.IO.File.ReadAllLines(path,Encoding.Default);
            List<int> numIP = new List<int>();
            string[] strQ = strs[0].Split('.');
            string s = strQ[0] +"."+ strQ[1] +"."+ strQ[2]+".";//取字符串中的相同部分
            for (int i = 0; i < strs.Length; i++)
            {
                numIP.Add(Convert .ToInt32 ( strs[i].Split ('.')[3]));
            }
            for (int i = 0; i < numIP.Count; i++)//比较最后一位的大小
            {
                for (int j = i; j < numIP.Count; j++)
                {
                    if (numIP[i] > numIP[j])
                    {
                        int temp = numIP[i];
                        numIP[i] = numIP[j];
                        numIP[j] = temp;
                    }
                }
                Console.WriteLine(s + numIP[i].ToString());//输出结果
            }
            

QQ图片20130625163213.jpg (11.33 KB, 下载次数: 0)

QQ图片20130625163213.jpg

作者: 曾大鹏    时间: 2013-6-26 21:57
本帖最后由 曾大鹏 于 2013-6-26 22:17 编辑
  1. static void Main(string[] args)
  2.         {
  3.             string[] str = File.ReadAllLines("IP地址.txt", Encoding.Default);
  4.             for (int i = 0; i < str.Length - 1; i++)
  5.             {
  6.                 for (int j = 0; j < str.Length - 1; j++)
  7.                 {
  8.                     if (ToNumber(str[j]) > ToNumber(str[j + 1]))
  9.                     {
  10.                         string strBu = str[j];
  11.                         str[j] = str[j + 1];
  12.                         str[j + 1] = strBu;
  13.                     }
  14.                 }
  15.             }
  16.             for (int i = 0; i < str.Length; i++)
  17.             {
  18.                 Console.WriteLine(str[i]);
  19.             }
  20.             Console.ReadKey();
  21.         }
  22.         /*Ip地址格式为:a.b.c.d
  23.         每个数字范围在0~255之间 那么我们可以把它们看成一个四位的256进制数
  24.         然后转换成十进制 =a*256^3+b*256^2+c*256^1+d*256^0
  25.         然后根据对应的十进制大小排序就OK了。。*/
  26.         private static int ToNumber(string str)
  27.         {
  28.             string[] p = str.Split('.');
  29.             int sum = 0;
  30.             for (int i = 0; i < p.Length; i++)
  31.             {
  32.                 sum = sum * 256 + int.Parse(p[i]);
  33.             }
  34.             return sum;
  35.         }
复制代码

作者: 蔡志涛    时间: 2013-7-3 16:07
逆_水_寒 发表于 2013-6-21 21:37
这是一个TXT文本,并且是一行一行写的,用  Line 能读出 “/r/n”,一行一行读出来放入数组再遍历吧。
第九 ...

哪个第九天的基础视频啊,能再说细一些吗,这道题不会做。。。
作者: 蔡志涛    时间: 2013-7-3 16:35

你的想法很精妙,不知道你是怎么得来的这个思路。另外,你的第一步冒泡排序的内层循环可以再优化下,可以这么写for (int j = 0; j < str.Length - 1- i ; j++)
作者: 曾大鹏    时间: 2013-7-3 16:49
蔡志涛 发表于 2013-7-3 16:35
你的想法很精妙,不知道你是怎么得来的这个思路。另外,你的第一步冒泡排序的内层循环可以再优化下,可以 ...

差别也不大。
作者: 成都—陈超    时间: 2013-7-4 18:49

排序好了的  可以不用再比较了
作者: 宋兴征    时间: 2013-7-4 23:05

网上看到的一种写法:用移位操作,将ip地址转换为long型整数,排序后,再将long整数转为string。
不过这里移位后累加,最后得出一个long整数的原理不太明白,还想请教一下。
代码:
  1. #region IP地址转换为long型整数
  2.         public static long ip2long(string strIP)
  3.         {
  4.             IPAddress ip = IPAddress.Parse(strIP);
  5.             int x = 3;
  6.             long o = 0;
  7.             foreach (byte f in ip.GetAddressBytes())
  8.             {
  9.                 o += (long)f << 8 * x--;
  10.             }
  11.             return o;
  12.         }
  13.         #endregion

  14.         #region long型整数转换为IP地址
  15.         public static string long2ip(long l)
  16.         {
  17.             var b = new Byte[4];
  18.             for (int i = 0; i < 4; i++)
  19.             {
  20.                 b[3 - i] = (byte)(l >> 8 * i & 255);
  21.             }
  22.             return new IPAddress(b).ToString();
  23.         }
  24.         #endregion
复制代码

作者: sym544135698    时间: 2013-7-5 12:48

你的string[] str = File.ReadAllLines("IP地址.txt", Encoding.Default);最后一个Encoding.Default是什么意思啊
作者: sym544135698    时间: 2013-7-5 13:28
本帖最后由 sym544135698 于 2013-7-5 13:33 编辑


你的这段代码是不行的,总是说字符串有问题,需要trycatch
  1. try
  2. {
  3. sum = sum + int.Parse(p[i]) * 256 ^ (i);
  4. }
  5. catch
  6. { }
复制代码
sum那里也不是的

作者: sym544135698    时间: 2013-7-5 13:32
好有难度,被打击了,
作者: 盘晟    时间: 2013-7-5 15:24
面试不是基础题么?怎么这么难的!
作者: zhangcheng5468    时间: 2013-7-9 14:08
这些IP是手动拷贝到txt文件还是通过程序写入txt文件?
作者: pm324    时间: 2013-7-9 14:38
  1. static void Main(string[] args)
  2.         {
  3.             List<string> s = File.ReadLines(@"ip.txt", Encoding.Default).ToList<string>();

  4.             for (int i = 0; i < s.Count - 1; i++)
  5.             {
  6.                 for (int j = 0; j < s.Count - 1 - i; j++)
  7.                 {
  8.                     if (ToNumber(s[j]) > ToNumber(s[j + 1]))
  9.                     {
  10.                         string temp = s[j];
  11.                         s[j] = s[j + 1];
  12.                         s[j + 1] = temp;
  13.                     }
  14.                 }
  15.             }
  16.             foreach (string str in s)
  17.             {
  18.                 Console.WriteLine(str);
  19.             }
  20.             Console.ReadKey();
  21.         }
  22.         private static long ToNumber(string s)
  23.         {
  24.             string resault = "";
  25.             for (int i = 0; i < 4; i++)
  26.             {
  27.                 resault += s.Split('.')[i].PadLeft(3, '0');
  28.             }
  29.             return long.Parse(resault);
  30.         }
复制代码

作者: wanghuailin1030    时间: 2013-7-9 20:47
zhangcheng5468 发表于 2013-7-9 14:08
这些IP是手动拷贝到txt文件还是通过程序写入txt文件?

手动写,是给出来的
作者: wanghuailin1030    时间: 2013-7-9 21:00

此法更妙
作者: 大漠孤烟    时间: 2014-5-13 23:48
这个考试题,看了搞了好久才有些思路,基础视频也看了,但涉及多了就搞混了,看来还是基础不牢。。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2