变态算法问题:反转语句(算法大虾们近来)

gyro 2005-07-29 03:44:50
输入:"I am a student",return "student a am I";

简单吧?变态的是:不允许用任何的内置方法,自己写可以,但是任何地方都不能用内置的方法。
外部的?更不行

哦,要求用c#
...全文
773 62 打赏 收藏 转发到动态 举报
写回复
用AI写文章
62 条回复
切换为时间正序
请发表友善的回复…
发表回复
真相重于对错 2005-09-14
  • 打赏
  • 举报
回复
C#把人都变懒了,这道题对于了解c的程序员,易如反掌
tsc2005 2005-09-14
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
Console.WriteLine("请输入一个句子:");
string str = Console.ReadLine();
char[] stack = new char[50];
int p = 0;

for (int i = str.Length-1; i >= 0; i--)
{
if (str[i] != ' ')
{
stack[p] = str[i];
p++;

if (i == 0 && p > 0)
{
while (p != 0)
{
Console.Write(stack[--p].ToString());
}
}
}
else
{
while (p != 0)
{
Console.Write(stack[--p].ToString());
}
Console.Write(' ');
}
}
sarcophile 2005-09-14
  • 打赏
  • 举报
回复
不就是写一个堆栈吗?搞得这么吓人
dragonfly001 2005-09-14
  • 打赏
  • 举报
回复
栈的方法拉解決了最好了 ,真的用C#真的會把人變傻的喔
mathsword 2005-09-13
  • 打赏
  • 举报
回复
还不结帖?
zmy0611 2005-09-13
  • 打赏
  • 举报
回复
借LZ的宝地问大家:
帮我看看这个问题
http://community.csdn.net/Expert/TopicView.asp?id=4264226
yzhou_swnu 2005-09-13
  • 打赏
  • 举报
回复
谈谈栈+状态机算法的原理。

其实说白了很简单。

状态为正,读非空格字符,状态为负,读空格。

一开始状态为正,直到遇到空格,状态反转,并将读到的字符压入栈。
状态为负时,读空格,直到遇到非空格字符,状态反转,并将读到的空格压入栈。
如此反复,直到字符串读完。

当然,利用栈,不用状态机也可以完成。那就是,遇到空格就把以前读的字符压入栈,再压一个空格进栈。但是这样的话,空格很多的时候就会频繁的压栈。而状态机与栈配合起来,就很优雅了。


同意! UP!UP!UP!UP!
hnldsfww 2005-09-13
  • 打赏
  • 举报
回复
我操,密密麻麻...
lovvver 2005-09-13
  • 打赏
  • 举报
回复
substring肯定是不可以的了。
我也做了一下,兄弟们帮忙看看这里有没有内部或外部函数啊。
[在控制台上已测试通过]
//这是转换的方法:
private static string Convert(string strIn)
{
string strOut = "";
string strWord = "";
for(int i = strIn.Length-1;i>=0;i--)
{
if(strIn[i] != ' ')
{
strWord = strIn[i]+strWord;
if(i == 0)
{
strOut += strWord;
break;
}
continue;
}
else
{
strOut += strWord+" ";
strWord = "";
continue;
}
}
return strOut;
}

//这是全部的程序:
using System;

namespace Sf1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Console.WriteLine("Please enter a sentence first.") ;
string _strRead = Console.ReadLine() ;
string _strWrite = null;
if(_strRead.Length >1)
{
_strWrite = Convert(_strRead);
}
Console.WriteLine(_strWrite) ;
}

private static string Convert(string strIn)
{
string strOut = "";
string strWord = "";

for(int i = strIn.Length-1;i>=0;i--)
{
if(strIn[i] != ' ')
{
strWord = strIn[i]+strWord;
if(i == 0)
{
strOut += strWord;
break;
}
continue;
}
else
{
strOut += strWord+" ";
strWord = "";
continue;
}

}
return strOut;
}
}
}
NickyName 2005-09-13
  • 打赏
  • 举报
回复
我觉得substring()应该可以用吧,因为C语言里也有啊(如果没记错的话),实在不让用可以自己写一个。
看了Ivony() 的解答,才想到可以用堆栈,很好的思路。我压根儿就没想到,惭愧,汗一个。
自己半天才调试出一个,也贴出来现一下。
private string Reverse2(string s)
{
string temp = "";
int len = s.Length;
int wordStartPosition = len-1;
//If we find balnk
bool searchBlank = true;
if (s[len - 1].ToString() == " ")
{
searchBlank = false;
}
for (int i = len-1; i > 0; i--)
{
string t = s[i].ToString();
if( (t==" " && searchBlank==true) || (t!=" " && searchBlank==false) )
{
temp += s.Substring(i+1, wordStartPosition - i);
wordStartPosition = i;
searchBlank = !searchBlank;
}
}
temp += s.Substring(0, wordStartPosition + 1);
return temp;
}
fan2c 2005-07-31
  • 打赏
  • 举报
回复
to f_lorelei(icehyp)
考的是解决问题的能力,而不是平台是否足够强大.
你能解决,u will be hired
你不能解决,u are fired!
lzyuqiang 2005-07-31
  • 打赏
  • 举报
回复
就是让用.split,对于这个问题也没有多大帮助
lzyuqiang 2005-07-31
  • 打赏
  • 举报
回复
还没有结呀,那我给出调试过的代码:
string strsp(string input)
{
const int MaxWords = 10;
string output="";
string[] inputs = new string[MaxWords];
string[] spaces = new string[MaxWords];
bool flag = false;
int i = 0;
foreach(char a in input)
{
if (a == ' ')
{spaces[i] += a;
flag = true; }
else
if flag {i++;flag = false;}
inputs[i] += a;
}
output = inputs[i--];

for (;i>=0;i--)
output += spaces[i] + inputs[i];
return output
}
sandsboy 2005-07-31
  • 打赏
  • 举报
回复
应该主要考查对数据结构"栈"的用途的理解
asuan 2005-07-31
  • 打赏
  • 举报
回复
如果Length也不用的话,请看下面:
private string test(string str)
{
int i = 0 ;
string outstr = "",tmp ="" ;
try
{
while(true)
{
if(str[i]!=(char)32)
{
tmp += str[i++] ;
continue ;
}
outstr =tmp +" "+ outstr ;
tmp = "" ;
i++ ;
}
return (tmp +" "+ outstr) ;
}
catch
{
return (tmp +" "+ outstr) ;
}
}
asuan 2005-07-31
  • 打赏
  • 举报
回复
private string test(string str)
{
int i = 0 ;
string outstr = "",tmp ="" ;
while(i<str.Length)
{
if(str[i]!=(char)32)
{
tmp += str[i] ;
i++ ;
continue ;
}
outstr =tmp +" "+ outstr ;
tmp = "" ;
i++ ;
}
return (tmp +" "+ outstr) ;
}
kissknife 2005-07-30
  • 打赏
  • 举报
回复
两个栈a、b,将字串入a栈,完毕之后依次弹栈,将弹出的字符入b栈,遇空格b弹栈到栈底,再稍做点处理应该就可以了吧。。不知道行不行。。。
zeusvenus 2005-07-30
  • 打赏
  • 举报
回复
其实是个考栈和内存管理的题。
tiaoci 2005-07-30
  • 打赏
  • 举报
回复
标准算法在此

using System;
public class Test
{
public static void Main(string [] args)
{
Console.WriteLine(Reverse("I am a student"));
}

public static string Reverse(string s)
{
char [] data = s.ToCharArray();
int i = 0; int n = data.Length;
Reverse(data, 0, n - 1);
while(i < n)
{
if(data[i] == ' ')
{
i++;
continue;
}
int j = i;
while(j + 1 < n && data[j + 1] != ' ') j++;
Reverse(data, i, j);
i = j + 1;
}
return new string(data);
}

private static void Reverse(char [] data, int n1, int n2)
{
while(n1 < n2)
{
char c = data[n1]; data[n1] = data[n2]; data[n2] = c;
n1++; n2--;
}
}
}
syeerzy 2005-07-30
  • 打赏
  • 举报
回复
public string func(string s)
{
string result = "";
string word = "";
foreach(char c in s)
{
if(c!='\0')
{
if (c != ' ')
{word += c;}
else
{
result = " " + word + result;
word = "";
}
}
}
if (word != "")
{result = word + result;}
return result;
}
加载更多回复(42)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧