|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
Autodesk programming face the questions together, we try playing Write a function to reduce a sentence, such as :
"MY name is chris" compression "Mynameischris", please use the smallest and occupied memory to achieve the maximum efficiency of the algorithm. |
I wrote one, please enlighten
#include "Stdio.h>
Char * compress (char * ptr)
{
Char 6-12, *tail;
Char c;
P=tail=ptr;
While (c=*p)
{
If (c!=32)
{
*tail++=c;
*p=32;
}
P++;
}
*tail= '\0';
Return ptr;
}
Int main ()
{
Char a[30]={ "MY name is")
Printf ( "%s" compress (a));
Printf ( "done\n");
Return 0
} |
/***
Write a function to reduce a sentence, such as :
"MY name is chris" compression "Mynameischris"
Please use the smallest and occupied memory to achieve the maximum efficiency of the algorithm.
****/
#include "Stdio.h>
#include "String.h>
#include "Malloc.h>
//both Passed on VC + + 6.0
//writen By [stdlib.h]
Compress1 char * (char * ptr)
{
Char 6-12, *tail;
Char c;
P=tail=ptr;
While (c=*p)
{
If (c!=32)
{
*tail++=c;
*p=32;
}
P++;
}
*tail= '\0';
Return ptr;
}
//writen By [choki]
4003rd Oh, I do not have the space to use
Char * compress2 (const char * ptr)
{
Char *result;
Int count;
Result = (char *) malloc (sizeof (char) *strlen (ptr));
If (result ====== NULL)
Return NULL;
Memset (result, 0x00, strlen (ptr));
For (count=0; count "strlen (ptr); count++)
{
If (ptr[count]> '')
Memcpy (result+strlen (result), ptr+count, 1);
}
Return result;
}
Int main ()
{
Char a[30]={ "MY name is chris")
Printf ( "%s\n" compress1 (a));
Printf ( "%s\n" compress2 (a));
Printf ( "done\n");
Return 0
} |
-->
*p=32; How I think this irrelevant? |
Memcpy (result+strlen (result), ptr+count, 1);
The total will be
*tail++=c;
*p=32;
High efficiency
Ha ha |
| |