今天在推荐帖上看到一个面试算法题,感觉很好想拿来学习一下,可是按照帖子楼主的方法总是出错,所以请各位大侠帮忙看看问题出在哪里。题意大概是:
对字符串进行排序,用任意一种编程语言来实现,不能使用现有的类,在排序中,字符串“Bc”,“Ad”,“aC”,“Hello”,“X man”,“little”,“During”,“day”能够排序成 “Ad”,"aC",“Bc”,“During”,“day”,“Hello”,“little”,“X man”,也就是说,在排序的过程并不是传统的按照字符串排序,在排序中还需要将小写字母一并排序,也就是说a字符串要在B或b之前。源程序:
using System;class Sort08
{
public static void Main()
{
string[] str = { "dad", "bood", "bada", "Admin", "Good", "aete", "cc", "Ko", "Beta", "Could" };
Console.WriteLine("Original string is :");
for (int i = 0; i < str.Length; i++)
{
Console.Write(str[i] + " ");
}
Console.WriteLine(); Sort(str); //报错为“非静态的字段、方法或属性“Sort08.Sort(str)”要求对象引用”
Console.WriteLine("After Sort it gives:" + str); Console.ReadLine();
} private void Sort(string[] s)
{
for (int i = 0; i < s.Length; i++)
{
for (int j = 0; j < s.Length - i - 1; j++)
{
if (Compare(s[j], s[j + 1]) > 0)
{
string tem = s[j];
s[j] = s[j + 1];
s[j + 1] = tem;
}
}
}
} private int Compare(string str1, string str2)
{
int x = 0;
for (int i = 0, j = 0; (i < str1.Length) && (j < str2.Length); i++, j++)
{
int s1 = (int)str1[i];
int s2 = (int)str2[j]; //insert
if (s1 >= 97)
{
s1 -= 32;
} if (s2 >= 97)
{
s2 -= 32;
}
//end if (s1 > s2)
{
x = 1;
break;
}
else if (s1 < s2)
{
x = 0;
break;
}
else if (s1 == s2)
{
if ((int)str1[i] > (int)str2[j])
{
x = 1;
break;
}
else
{
x = 0;
break;
}
}
}
return x;
}
}错误如上红色部分标注的那样,但我若是把下面两个方法都改成Static静态后,却并不能显示出正确的排序结果。请大家帮忙看看问题究竟出在哪里吧!谢谢了先!
对字符串进行排序,用任意一种编程语言来实现,不能使用现有的类,在排序中,字符串“Bc”,“Ad”,“aC”,“Hello”,“X man”,“little”,“During”,“day”能够排序成 “Ad”,"aC",“Bc”,“During”,“day”,“Hello”,“little”,“X man”,也就是说,在排序的过程并不是传统的按照字符串排序,在排序中还需要将小写字母一并排序,也就是说a字符串要在B或b之前。源程序:
using System;class Sort08
{
public static void Main()
{
string[] str = { "dad", "bood", "bada", "Admin", "Good", "aete", "cc", "Ko", "Beta", "Could" };
Console.WriteLine("Original string is :");
for (int i = 0; i < str.Length; i++)
{
Console.Write(str[i] + " ");
}
Console.WriteLine(); Sort(str); //报错为“非静态的字段、方法或属性“Sort08.Sort(str)”要求对象引用”
Console.WriteLine("After Sort it gives:" + str); Console.ReadLine();
} private void Sort(string[] s)
{
for (int i = 0; i < s.Length; i++)
{
for (int j = 0; j < s.Length - i - 1; j++)
{
if (Compare(s[j], s[j + 1]) > 0)
{
string tem = s[j];
s[j] = s[j + 1];
s[j + 1] = tem;
}
}
}
} private int Compare(string str1, string str2)
{
int x = 0;
for (int i = 0, j = 0; (i < str1.Length) && (j < str2.Length); i++, j++)
{
int s1 = (int)str1[i];
int s2 = (int)str2[j]; //insert
if (s1 >= 97)
{
s1 -= 32;
} if (s2 >= 97)
{
s2 -= 32;
}
//end if (s1 > s2)
{
x = 1;
break;
}
else if (s1 < s2)
{
x = 0;
break;
}
else if (s1 == s2)
{
if ((int)str1[i] > (int)str2[j])
{
x = 1;
break;
}
else
{
x = 0;
break;
}
}
}
return x;
}
}错误如上红色部分标注的那样,但我若是把下面两个方法都改成Static静态后,却并不能显示出正确的排序结果。请大家帮忙看看问题究竟出在哪里吧!谢谢了先!
解决方案 »
- Effective C#: 50 Specific Ways to Improve Your C# 第二版 分享 + 求资料
- ORM开发
- DataGrid 中checkbox 勾选去掉的问题
- IEwebcontrols的treeview如果在客户端判定用户选中(checked)的节点?
- 关于printPreviewControl1 打印预览 高手请进
- 急,如何获得Image控件的handle?
- 请问C#中,拖动Panel的滚动条,松开后,又回到原先的位置,为什么??????????
- string是引用类型的疑问?
- 在水晶报表里面如何画一条斜线(谢谢各位帮帮忙)
- 怎样在C#中使用消息处理?
- 100分求解一个XML的问题,马上给分,急!
- asp.net 中 aspx和CS中的函数相互调用
private static void Sort(string[] s)
方法修饰符public去掉
或者方法修饰符全部改成public
s.Sort(str);
程序运行结果如下:
Original string is :
dad,bood,bada,Admin,Good,aete,cc,Ko,Beta,Could
After Sort it gives:System。string[]上面标注的红色就不对了,应该显示的是重新排序后的结果,但是现在只显示了System。string[]这样的形式,所以还请各位帮忙看看问题究竟出在哪里。
s.sort(str);这样就可以了在静态方法中调用不是静态的方法,就要实例对像
Original string is :
dad,bood,bada,Admin,Good,aete,cc,Ko,Beta,Could
After Sort it gives:System.string[] 上面标注的红色就不对了,应该显示的是重新排序后的结果,但是现在只显示了System.string[]这样的形式,现在主要的问题卡在这儿了。所以还请各位帮忙看看这到底是什么原因。
public static void Main()
{
string[] str = { "dad", "bood", "bada", "Admin", "Good", "aete", "cc", "Ko", "Beta", "Could" };
Console.WriteLine("Original string is :");
for (int i = 0; i < str.Length; i++)
{
Console.Write(str[i] + " ");
}
Console.WriteLine();
Sort08 s=new sort08();
str.lear()
s.sort(str);
//Sort(str); //报错为“非静态的字段、方法或属性“Sort08.Sort(str)”要求对象引用”
//Console.WriteLine("After Sort it gives:" + str);
for (int i = 0; i < str.Length; i++)
{
Console.Write(str[i] + " ");
}
Console.ReadLine();
}
class Sort08
{
public static void Main()
{
string[] str = { "dad", "bood", "bada", "Admin", "Good", "aete", "cc", "Ko", "Beta", "Could" };
Console.WriteLine("Original string is :");
for (int i = 0; i < str.Length; i++)
{
Console.Write(str[i] + " ");
}
Console.WriteLine();
Sort08 a = new Sort08();
a.Sort(str); //报错为“非静态的字段、方法或属性“Sort08.Sort(str)”要求对象引用”
Console.WriteLine("After Sort it gives:" + str);
for (int i = 0; i < str.Length; i++)
{
Console.Write(str[i] + " ");
} Console.ReadLine();
} public void Sort(string[] s)
{
for (int i = 0; i < s.Length; i++)
{
for (int j = 0; j < s.Length - i - 1; j++)
{
if (Compare(s[j], s[j + 1]) > 0)
{
string tem = s[j];
s[j] = s[j + 1];
s[j + 1] = tem;
}
}
}
} private int Compare(string str1, string str2)
{
int x = 0;
for (int i = 0, j = 0; (i < str1.Length) && (j < str2.Length); i++, j++)
{
int s1 = (int)str1[i];
int s2 = (int)str2[j]; //insert
if (s1 >= 97)
{
s1 -= 32;
} if (s2 >= 97)
{
s2 -= 32;
}
//end if (s1 > s2)
{
x = 1;
break;
}
else if (s1 < s2)
{
x = 0;
break;
}
else if (s1 == s2)
{
if ((int)str1[i] > (int)str2[j])
{
x = 1;
break;
}
else
{
x = 0;
break;
}
}
}
return x;
}
}