这道面试题我错在哪了?

韩雷 2005-10-14 02:14:12
前一段时间我去了一家公司面试,他们给我出了道题:

用户输入若干字符串,如果输入的是0000
表示结尾,对用户输入的字符串排序并输出到屏幕。

我的答案是:
(不好意思,有点长)
#include "stdafx.h"
using namespace std;

// 字符链表结构
struct char_struct
{
char a;
char_struct* next;
};

// 字符串链表结构
struct str_struct
{
char* str;
str_struct* next;
};

//----------------------------------------------
// main
//----------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{

int i = 0;
int j = 0;
int testforPaiXu = 0;

bool temp = false;

str_struct *str_head, *str_p, *str_q, *str_p_shang;

str_head = (str_struct*)malloc(sizeof(struct str_struct));
str_p = (str_struct*)malloc(sizeof(struct str_struct));

str_p_shang = (str_struct*)malloc(sizeof(struct str_struct)); // 排序时链表中上一个结构的暂存

str_head->next = str_p;
str_head->str = NULL;
str_p->next = NULL;
str_p->str = NULL;


char_struct* head, *p, *q, *temp_end;

temp_end = (char_struct*)malloc(sizeof(struct char_struct));


head = p = (char_struct*)malloc(sizeof(struct char_struct));

// while 1 begin
while(1)
{

str_p = str_head->next;
str_p_shang = str_head;



cout << " into please: ";

p = head;

head->a = getchar();
head->next = NULL;


//如果用户输入的第一个字母不是回车
if(head->a != '\n')
{
//while 2 begin
while(1)
{
q = (char_struct*)malloc(sizeof(struct char_struct));


q->a = getchar();

j++;

p->next = q;
p=q;

if(q->a == '\n')
break;
}
// while 2end


p->next = NULL;
cout << endl;

//将char_struct字符串链表内容装入一个char*

str_q = (str_struct*)malloc(sizeof(struct str_struct));

//malloc一个与刚输入的字符序列大小一致的char*空间
str_q->str = (char*)malloc((j+1)*sizeof(char));

memset(str_q->str,'\0',(j+1));//memset用来对一段内存空间全部设置为某个字符。

//为测试是否为结尾
temp_end = head;

//倒入
for(int k=0;k<j;k++)
{
str_q->str[k] = head->a;
head = head->next;
}

// 排序
if(temp == false)
{
str_head->next = str_q;
str_q->next = NULL;
}

// while 3 begin
while(temp)
{
//如果两个字符串的第testforPaiXu位相同
while(
(str_p->str[testforPaiXu] != '\0')
&& (str_q->str[testforPaiXu] != '\0')
&& str_p->str[testforPaiXu] == str_q->str[testforPaiXu]
)
{
testforPaiXu++;
}

if( str_p->str[testforPaiXu] < str_q->str[testforPaiXu])
{
if(str_p->next != NULL)
{
str_p_shang = str_p;
str_p = str_p->next;
}
else
{
//将 str_q 加在最后
str_p->next = str_q;
str_q->next = NULL;
temp = false;
}
}
else
{
str_p_shang->next = str_q;
str_q->next = str_p;
temp = false;
}
}
// while 3 end


temp = true;
testforPaiXu = 0;
j = 0;


while(temp_end->a == '0')
{
++i;
temp_end = temp_end->next;

}

if( (temp_end->a =='\n')
&& (i==4) ) break; // 输入的字符串为 "0000"

i = 0;

}

}
// while 1 end



str_head = str_head->next;


//输出

while(str_head != NULL)
{

cout << endl;

cout << str_head->str << endl;

str_head = str_head->next;
}

cout << endl;

return 0;
}


面试的人没说什么,让我回去等,结果我就等啊等...最后没结果:(
我把代码贴出来,请大家费心看看,错在那里?
谢谢
...全文
860 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
韩雷 2005-10-16
  • 打赏
  • 举报
回复
我是这样理解的:
题目没有说明可以有多少个字符串及每个字符串有多少字符,开始我用C++的方法,
用容器vector,做完后他让我用C的方法实现,当然只是主要部分,像cin cout就不用修改
v41dugu 2005-10-16
  • 打赏
  • 举报
回复
呵呵 就是这样了
sky_apple 2005-10-15
  • 打赏
  • 举报
回复
关注一下,顶!!!!
gamelearner 2005-10-15
  • 打赏
  • 举报
回复
#pragma warning(disable: 4786)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

void main()
{
vector<string> SV;
string s;
while (cin >> s)
{
if (s == "0000") break;
SV.push_back(s);
}

sort(SV.begin(), SV.end());
ostream_iterator<string> OS(cout, "\n");
copy(SV.begin(), SV.end(), OS);
}
超级大笨狼 2005-10-15
  • 打赏
  • 举报
回复
有时候我倒希望我们的工作经常用一些学校里面学的东西,可是不是这样的,有些知识很想巩固一下。
超级大笨狼 2005-10-15
  • 打赏
  • 举报
回复
哦,你一个函数的确是太长了。
超级大笨狼 2005-10-15
  • 打赏
  • 举报
回复
面试没结果不一定就是你题目做得不好,有很多种原因的。
昨天我就招聘别人,准备了题目,不合适的人问了好多试题,合适的人说几句话就不用问试题了。
Robin 2005-10-15
  • 打赏
  • 举报
回复
haha
lindex 2005-10-15
  • 打赏
  • 举报
回复
为什么不用桶排序阿?建一个桶bucket[256],每出现一个字符c,则将bucket[c]增加1。
hao0228 2005-10-15
  • 打赏
  • 举报
回复
都是高手,我的好好学习学习。
Baku 2005-10-15
  • 打赏
  • 举报
回复
用STL,里面有排序的啊,写的好复杂,还有BUG 。。。
hualin007 2005-10-15
  • 打赏
  • 举报
回复
我前几天面试给出了个字符压缩的算法题,当时不会做,后来回来查资料求人,结果那个程序要老长老复杂才弄的出来,面试那一会怎么能写的出来。
zoulie 2005-10-15
  • 打赏
  • 举报
回复
这么长啊,看懂思路就可以了
www 2005-10-15
  • 打赏
  • 举报
回复
太长了,考官也不会看完的。
请问用了多长时间写完的。
2005-10-15
  • 打赏
  • 举报
回复
C++为什么不用泛型算法???
要写这么多?
copy sort
NuclearG 2005-10-15
  • 打赏
  • 举报
回复
这世上有的考官出这么简单的题是要看看你会不会写注释地...
mainwind 2005-10-14
  • 打赏
  • 举报
回复
楼主写的太长了,这个没有必要这样写的!!
karlfly 2005-10-14
  • 打赏
  • 举报
回复
楼主写的也太长了
jay0518 2005-10-14
  • 打赏
  • 举报
回复
我觉得楼主c学的很好啊
什么时候有时间能指教一下啊
csucdl 2005-10-14
  • 打赏
  • 举报
回复

void InterChange(string& x1, string& x2)
{
string x = x1;
x1 = x2;
x2 = x;
return;
}
int Partition(string A[], const int& left, const int& right)
{
int i = 0;
int k = 0;
string v ;
i = left;
k = right;
v = A[left];

while(true)
{
do
{
i++;
}while(A[i].compare(v) == -1 && i < right - 1);
do
{
k--;
}while(A[k].compare(v) == 1 && k > 0);
if(i < k)
{
InterChange(A[i], A[k]);
}
else
{
break;
}


}
A[left] = A[k];
A[k] = v;
return k;
}


void QuickSort(string A[], const int& left, const int& right)
{
int j = 0;
if(left < right)
{
j = right + 1;
j = Partition(A, left, j);
QuickSort(A, left, j - 1);
QuickSort(A, j + 1, right);
}
return;
}


int main()
{
vector<string> strings;
string sinput;
while(cin>>sinput)
{
if(sinput == "0000")
{
break;
}
else
{
strings.push_back(sinput);
}
}
QuickSort(strings.begin(), 0, strings.size() - 1);
for(string* cp = strings.begin(); cp != strings.end(); cp++)
{
cout<<*cp<<" ";
}
cout<<endl;
return 0;
}
加载更多回复(6)

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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