两道C程序笔试题,大家讨论下

ply6669747 2005-04-14 05:07:35
1.行程压缩。将连续重复出现的对象表示为对象和它出现的次数,
即将如AAAAAHHHHHH压缩为A5H6;
333333355555555压缩为3758;
在设计压缩程序时要考虑解压缩,并设计该解压缩程序。
///////:我觉得关键是设计的压缩程序能够解压缩。

2.数据转换。将一个八位的二进制数转换为16进制数。两位16进制数转换为对应的二进制数。
...全文
798 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanhechen 2005-04-15
  • 打赏
  • 举报
回复
奇偶位置来确定不行啊
如果有连续11个3那么则是311,奇偶出错
heskyII 2005-04-15
  • 打赏
  • 举报
回复
建议参考LZSS
狂放之歌 2005-04-15
  • 打赏
  • 举报
回复
2.数据转换。将一个八位的二进制数转换为16进制数。两位16进制数转换为对应的二进制数。

用位移做
狂放之歌 2005-04-15
  • 打赏
  • 举报
回复
void yasuo()//压缩
{
char a[256]="aaaaaaaaabbbbbbbbcccccccccddddddddd";
char c[256];
int count=0;
int i,j;
for(i=0;i<256;i++)
{
if(a[i]=='\0')
break;
c[count++]=a[i];

for(j=i;j<256;j++)
if(a[j]!=a[i])
break;
c[count++]='0'+j-i;
i=j-1;
}
c[count]='\0';
printf("%s\n",c);
}
///////////////////////////////////////////////////
void Jya()//解压缩
{

char a[256]="a3b4n5";
char c[256];
int count=0;
int i,j;
for(i=0;i<256;i+=2)
{
if(a[i]=='\0')break;
c[count]=a[i];
for(j=1;j<a[i+1]-'0';j++)
c[count+j]=a[i];
count+=j;
}
c[count]='\0';
printf("%s",c);
}
//////////////////////////////
算法还可以优化
yinweihong 2005-04-15
  • 打赏
  • 举报
回复
第一个就是行程长度编码,第二个是进制转换题
ply6669747 2005-04-15
  • 打赏
  • 举报
回复
感谢大家的支持,但是压缩的目的就是为了节省空间,楼上的做法没有达到这个目的。
同时要求用C语言写出算法就行了,不用具体实现,也不能调用库函数。
请大家再考虑下,我是没有办法了。
magicknight 2005-04-15
  • 打赏
  • 举报
回复
我半天也没想出来

晕~~~
hikuers 2005-04-15
  • 打赏
  • 举报
回复
mark
xioxu 2005-04-15
  • 打赏
  • 举报
回复
感觉得先转换为字符串,然后一个一个字符的往另外一个字符串数组或其他什么读,当当前跟前者相同时,删除当前字符,把当前位加一。不同的时候保持当前位不变,而且如果数组奇数位为0的时候,删除掉它的前一个字符和这个奇数位。
第二题就比较简单了感觉,虽然好像麻烦,数据结构上有说。
yxin1322 2005-04-15
  • 打赏
  • 举报
回复
同意 WingForce说的方法
blackt 2005-04-15
  • 打赏
  • 举报
回复
int yasuo(char *pDes, const char *pSource)
{
char chNew = '\0';
int total = 0;
unsigned count = 1;
bool bInitial = true;//开始计数
for (int i = 0; pSource[i] != '\0'; ++i)
{
if (chNew != pSource[i])
{
if (!bInitial)
{
char ch = count > 9 ? ('A' + count - 10) : ('0' + count);
pDes[total++] = ch;//几位重复?
count = 1;
}
else
{
bInitial = false;
}
pDes[total++] = chNew = pSource[i];//保存值
}
else
{
++count;
if (count == 15)//最多15位重复
{
pDes[total++] = 'F';
chNew = '\0';
bInitial = true;
count = 1;
}
}
}
char ch = count > 9 ? ('A' + count - 10) : ('0' + count);
pDes[total++] = ch;
return total;
}

int JieYa(char *pDes, const char *pSource)
{
int count = 0;
for (int i = 0; pSource[i] != 0; ++i)
{
pDes[count++] = pSource[i];

char ch = pSource[i+1];
if (ch >= 'A' && ch <= 'F')
ch = ch - 'A' + 10;
else if (ch > '0' && ch <= '9')
ch -= '0';
else
return -1;

int n = (int)ch - 1;

printf("\nchar=%c ; count=%d\n", pSource[i], n);

for (int j = 0; j < n; ++j)
{
pDes[count++] = pSource[i];
}
++i;
}
if (((i+1) % 2) != 0)
return -1;//必须为偶数
return count;
}
一时手痒,写个通用的!
JoyerHuang_悦 2005-04-15
  • 打赏
  • 举报
回复
CSDN发言时不能注明是代码块吗??
格式都乱了。。。
JoyerHuang_悦 2005-04-15
  • 打赏
  • 举报
回复
前几天帮同学写了一个Hufman编码的,你可以看一下:

/*
Huffman compressing
A not so applicable software

Author : Joyer Huang
*/

#include<algorithm>
#include<iterator>
#include<map>
#include<vector>
#include<iostream>
#include<cstdio>
#include<string>
#include<cassert>
#if 0
#include<boost/lambda/lambda.hpp>
using namespace boost::lambda;
#endif

using namespace std;

//Supporting Structure
struct DictPair{ //Dictionary Pair
unsigned char c;
int vol;
};
struct hfHead{
int size; //size of header
int streamSize; //size of the decompressed file
int dictionarySize; //size of dictionary
DictPair dictpair[256];
};
union HuffmanFileHead {
hfHead head;
int binArray[300]; //For easier dumping.
};

struct HufTree{
HufTree *l, *r;
unsigned char c;
int vol;
HufTree():l(0),r(0),c(0),vol(0)
{};
HufTree(unsigned char cin, int volin):l(0),r(0),c(cin),vol(volin)
{}; //Constructor
};

struct LessSecond{
bool operator ()(const pair<int,int> pin ,const pair<int,int> pin2){
return pin.second<pin2.second;
}

};

struct GreaterVol{
bool operator ()(const HufTree* lhs,const HufTree* rhs){
return lhs->vol > rhs->vol;
}
};
template<typename tl ,typename tr>
void printpair(pair<tl,tr> pin) {
cout<<pin.first<<" "<<pin.second<<endl;
}

struct AssignTree{
HufTree* operator ()(const pair<unsigned char,int> pin){
return new HufTree(pin.first,pin.second);
}
};
//Supporting Structure

void printtree(HufTree*,int);
void usage();
void makehufDict(HufTree*,map<unsigned char,string> &,string );

int main(int argc , char ** argv) {
const int CharPerRead = 500; //Read 500 Charactors per input
unsigned char blk[CharPerRead];
map<unsigned char,int> dist;
dist.clear();
int rc,totalrc = 0; //Actual chars Readed

if (argc != 3) usage();

char* InFileName = argv[1];
char* OtFileName = argv[2];
FILE* InFile = fopen(InFileName,"r");
FILE* OtFile = fopen(OtFileName,"w");
//ASCII statistic
while( !feof(InFile) ){
rc = fread(blk, 1, CharPerRead, InFile);
totalrc+=rc;
for (int idx=0; idx<rc; ++idx){
++ dist[ blk[idx] ];
}
//fwrite(blk, 1, rc, OtFile);//output
}
//Build Huffman Tree
vector<pair<unsigned char,int> > vdist(dist.begin() ,dist.end());
stable_sort(vdist.begin(),vdist.end(),LessSecond() );
#if 0
for_each( vdist.rbegin(),vdist.rend(),printpair<unsigned char, int>); //Show the result of statistic
#endif
vector<HufTree*> vtree(vdist.size() );
transform(vdist.rbegin(), vdist.rend(), vtree.begin(), AssignTree());
while(vtree.size()>1){
HufTree* newTree = new HufTree();
newTree->vol += vtree.back()->vol;
newTree->l = vtree.back();
vtree.pop_back();
newTree->vol += vtree.back()->vol;
newTree->r = vtree.back() ;
vtree.pop_back();
vtree.push_back(newTree);
stable_sort(vtree.begin(), vtree.end(), GreaterVol());
} //Here ,The Huffman tree is built.in vtree[0];
#if 0
printtree(vtree[0],0); //Print the built huffman tree
#endif

map<unsigned char,string> hufDict;
string encode;
makehufDict(vtree[0],hufDict,encode);
#if 0 //Print the encoding result
for_each(hufDict.begin(),hufDict.end(), printpair<unsigned char,string>);
#endif

//Write the Header to the compressed file
#if 1
HuffmanFileHead header; memset(&header,0,sizeof(header));
header.head.streamSize = totalrc;
map<unsigned char,int>::iterator im = dist.begin(); //Save the distrubition
int i=0; //index of output file dist //instead of Dictionary
while(im!=dist.end() ){
header.head.dictpair[i].c = im->first;
header.head.dictpair[i].vol = im->second;
++i;++im;
}
header.head.dictionarySize = i;
header.head.size = 3*sizeof(int) + i*sizeof(DictPair);
fwrite(&header,3*sizeof(int),1,OtFile);
for(int i=0;i<header.head.dictionarySize;++i){
fwrite(&(header.head.dictpair[i]),sizeof(DictPair),1,OtFile);
}
#endif
// The compression output as follow..
#if 1
rewind(InFile);
unsigned long bitPosition=0;
unsigned char bitnode = 0;
while( !feof(InFile) ){
rc = fread(blk, 1, CharPerRead, InFile);
totalrc+=rc;
for (int idx=0; idx<rc; ++idx){
string code(hufDict[ blk[idx] ]);
for(int bit=0,size=code.size();bit<size;++bit){
bitnode = bitnode | (code[bit]=='1') << (bitPosition % 8);
++bitPosition;
if (bitPosition%8==0) {
fputc( bitnode, OtFile );
bitnode = 0;
}
}
}
}
if (bitPosition%8!=0) fputc( bitnode, OtFile );
#endif
// The compression output as above..

fclose(InFile);
fclose(OtFile);
system("pause"); //pause for while.
}

void makehufDict(HufTree* t,map<unsigned char,string> &hufDict,string encode){
if (!t->l && !t->r) hufDict[t->c] = encode;
if (t->l) makehufDict(t->l, hufDict , encode + "1");
if (t->r) makehufDict(t->r, hufDict , encode + "0");
}
void printtree(HufTree* t,int level){
if (t->l) printtree(t->l,level+1);
for(int i=0 ;i<level ;++i)
cout<<"->";
cout<<(int)(t->c)<<' '<<t->vol<<endl;
if (t->r) printtree(t->r,level+1);
}

void usage(){
cerr<<" Usage: \n"
<<" PROG inputfile outputfile";
exit(1);
}
WingForce 2005-04-15
  • 打赏
  • 举报
回复
就这破题也有讨论的价值吗?
---------------
可以从小处着手
勿以恶小而为之
勿以善小而不为
^_^
caoker2000 2005-04-15
  • 打赏
  • 举报
回复
就这破题也有讨论的价值吗?
FallenOrc 2005-04-15
  • 打赏
  • 举报
回复
对象是什么意思啊?
"abcabcabc"也可以看作“abc"这个对象重复3次,
WingForce 2005-04-15
  • 打赏
  • 举报
回复
奇偶位置来确定不行啊
如果有连续11个3那么则是311,奇偶出错
超过10就开始第二组
11个3就是
3031
CJLL0218 2005-04-14
  • 打赏
  • 举报
回复
不知道这样行不行

#include<iostream>
#include<string>
#include<vector>

using namespace std;

void main()
{
string yy;
int Q;
vector<string>box;
for(string i='!';getch()!='!';)
{
string *p;
p=new string;
box.pop_back (p);
delete p;
}
for(vector<string>::iterator x=box.begin ()-1;x!=box.end();x++)
{
for(vector<string>::iterator x=box.begin ();x!=box.end();x++)
{
if(&x==&[x-1])
{
yy=&x;
Q++;
}
else yy+=Q;
}

}
}
WonderOne 2005-04-14
  • 打赏
  • 举报
回复
前些天看了一个算法
333345666
转化成这样34-24563
方法是:如果遇到连续的重复的数字,譬如3333,就压缩为34
如果连续的数字不重复,则看有几个连续的数字不重复,假设n个不重复,就记为-n,接着是这n个不连续的数字,譬如45记为-245.
这样解码就没问题了,不知道是不是画蛇添足?!
daidodo 2005-04-14
  • 打赏
  • 举报
回复
这个并不难,自己试试嘛
加载更多回复(6)

70,036

社区成员

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

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