题目:将英文句子中的单词顺序进行翻转,但是单词本身不需要翻转.
//Solution 1
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define WORD_TOTAL_NUM 100
#define WORD_TOTAL_LEN 1024
//目前先假定10个单词
int g_Word_End_Positon[WORD_TOTAL_NUM] = {0};
int g_Word_Total_Num = 0;
char g_Final_Result[WORD_TOTAL_LEN] = {0};
//判断词的数量
int count_Word(char * input);
void get_SpecWord(char * input , int word_num, char * p_out_wordN);
void reverse_Two_String(char * str_Frist,char * str_Second);
int count_Word(char * input)
{
int i=0;
int count=0;
if(input == NULL || strlen(input)==0)
return 0;
for(i=0;i<strlen(input);i++)
{
if(*(input+i)==' ')
{
g_Word_End_Positon[count] = i;
count++;
}
if(*(input+i)=='\0')
{
g_Word_End_Positon[count] = i;
count++;
g_Word_Total_Num = count;
return g_Word_Total_Num
}
}
return 0;
}
void get_SpecWord(char * input , int word_num, char * p_out_wordN)
{
if(input == NULL || strlen(input)==0||p_out_wordN == NULL || word_num > g_Word_Total_Num)
{
p_out_wordN = NULL;
return;
}
if(word_num == 1)
memcpy(p_out_wordN,input,g_Word_End_Positon[word_num-1]);
else
memcpy(p_out_wordN,input+g_Word_End_Positon[word_num-2],g_Word_End_Positon[word_num-1]-g_Word_End_Positon[word_num-2]);
}
void reverse_Two_String(char * str_First,char * str_Second)
{
strcat(g_Final_Result,str_Second);
strcat(g_Final_Result," ");
strcat(g_Final_Result,str_First);
}
int main()
{
char input[WORD_TOTAL_LEN];
int i =0;
fget(input);
count_Word(input);
for(i=0;i<g_Word_Total_Num;i++)
{
get_SpecWord(input,i,);
get_SpecWord(input,i,)
}
}
//++++++++++++++++++++++++++Solution 2++++++++++++++++++++++++++++++++
void str_Swap(char * input,int start_index,int end_index)
{
char tmp_char;
int start = 0;
int end = end_i;
for(;start<len;start++,end--)
{
tmp_char = input[start];
input[start] = input[end];
input[end] = tmp_char;
}
}
void main()
{
int i=0;
int j=0;
int k=0;
str_Swap(total_input,0,strlen(total_input));
//
for(i=0;i<strlen(total_input);i++)
{
k=i;
if(*(total_input+i)==' ' || (*(total_input+i)=='\0'))
{
j=i;
str_Swap(total_input,k,j);
}
}
}
题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
//++++++++++++++++++++++++++Solution 1++++++++++++++++++++++++++++++++
void insertTheNode(P_NODE cur)
{
int i=0;
int j=0;
P_NODE tmp_spec;
P_NODE tmp_mid;
P_NODE tmp_before_spec;
do{
//find the 2*i+1
for(;tmp_spec.p_next != NULL && j<2*i+1;)
{
tmp_spec = (cur.p_next)->p_next;
tmp_before_spec = (cur.p_next);
j++;
}
tmp_mid = cur.p_next;
//insert
cur.p_next = tmp_spec;
tmp_spec.p_next = tmp_mid;
//delete
tmp_before_spec.p_next = tmp_spec.p_next;
cur = tmp_spec;
i++;
}while(cur.p_next != NULL)
}
//++++++++++++++++++++++++++Solution 2++++++++++++++++++++++++++++++++
//We should update like this
p_before_node2;
p_node2;
p_before_odd_node;
p_after_odd_node;
count =0 ;
while(start!=NULL){
count++;
if(count ==1)
p_before_node2 = start;
if(count ==2)
p_node2 = start;
if(count%2 == 0)
p_before_odd_node = start;
else
{
p_after_odd_node = start;
//disconnect
p_before_odd_node->p_next = p_after_odd_node;
//insert
p_before_node2->p_next = start;
start->p_next = p_node2;
//after insert, should update p_before_node2
p_before_node2 = start;
}
start = start->p_next;
}
题目:将单向链表从中间断开,分成两部分
//++++++++++++++++++++++++++Solution 1++++++++++++++++++++++++++++++++
int getLinkCount(P_NODE * start)
{
int count=0;
while(start != NULL)
{
count++;
start = start->p_next;
}
}
void outputLink(P_NODE * start)
{
int count =0;
int i=0;
P_NODE part1;
P_NODE part2;
count = getLinkCount(start);
part1 = (P_NODE)(* start);
for(;i<(count/2);)
{
start = start->p_next;
i++;
}
//update part2
part2 = (P_NODE)(*(start->p_next));
//disconnect
start->p_next = NULL;
}
//++++++++++++++++++++++++++Solution 2++++++++++++++++++++++++++++++++
/ 优化后 /
//get mid element
P_NODE p_mid;
P_NODE p__tmp;
while(start != NULL){
count++;
if(count%2==1) {
//add linked node each 2 steps
p_tmp = start;
p_mid = p_tmp;
p_tmp = p_tmp->p_next;
}
// search all the link node
start= start->p_next;
}
题目: Please write a C function to reverse an integrer.For example, 123 ==> 321
//++++++++++++++++++++++++++Solution 1++++++++++++++++++++++++++++++++
/// 采用了一个投机的方法,理论上用迭代会比较合适 ///
int reverseNum(int num)
{
char input[16]={0};
int i_len =0;
int start =0;
char tmp;
int result;
input = itoa(num);
i_len = strlen(input);
for(;start < i_len-1;start++,i_len--)
{
tmp = input[start];
input[start] = input[len-1];
input[len-1] = tmp;
}
}
优化后 /
/++++++++++++++++++++++++++Solution 2++++++++++++++++++++++++++++++++
#include "stdio.h"
#include "stdlib.h
"int output=0;
int revNum(int shang,int yushu)
{
output = (output + yushu)*10;
if(shang<10)
output += shang;
else
revNum(shang/10,shang%10);
}
int main()
{
int param=0;
printf("Input correct number:\r\n");
scanf("%d",¶m);
printf("=========================\r\n");
printf("%d => ",param);
revNum(param/10 , param%10);
printf("%d\r\n", output);
getchar();
return 0;
}