冰雹序列-C语言

C语言编写的冰雹序列


#include "stdio.h"
#include "stdlib.h"</code>

int getSubValue(const int &amp;tmpvalue)
{
    int newvalue = 0;
    if(tmpvalue &lt;= 1)
    {
        newvalue = 1;
    }
    else if( tmpvalue % 2 == 0 )
    {
        newvalue = tmpvalue/2;
    }
    else
    {
        newvalue = tmpvalue*3 + 1;
    }
    return newvalue;
}

void hailstone(const int &amp;tmpvalue)
{
    int count = 0;
    int newvalue = tmpvalue;
    printf("hailstone( %d ) = { ",tmpvalue);
    while( newvalue != 1 )
    {
        if( 0 != count )
        {
             printf( " , ");
        }
        count++;
        newvalue = getSubValue(newvalue);
        printf("%d",newvalue);
    }
    printf(" } \n");
}

int main(void)
{
    int num ;
    int count = 0;
    printf("please input the first value: ");
    scanf("%d",&amp;num);
    hailstone(num);
}

C语言获取当前时间

C语言获取当前时间:


#include "stdio.h"
#include "time.h"

#define LOG_INFO(format, ...)                                                                           \
{                                                                                                       \
time_t t = time(0);                                                                                 \
struct tm ttt = *localtime(&amp;t);                                                                     \
fprintf(stdout, "[INFO][%4d-%02d-%02d %02d:%02d:%02d] [%s:%s:%d] " format "",                     \
ttt.tm_year + 1900, ttt.tm_mon + 1, ttt.tm_mday, ttt.tm_hour,         \
ttt.tm_min, ttt.tm_sec, __FILE__,__FUNCTION__ , __LINE__, ##__VA_ARGS__);                            \
}

struct currentTime
{

int year;
int mounth;
int day;
int hour;
int minute;
int second;
int weekday;
int days;
int isdst;

}cTime;

void getCurrentTime(void)
{
time_t timep;
struct tm *p;
time(&amp;timep);
p = localtime(&amp;timep);

cTime.year = 1900+p-&gt;tm_year;
cTime.mounth = 1+p-&gt;tm_mon;
cTime.day = p-&gt;tm_mday;
cTime.hour = p-&gt;tm_hour;
cTime.minute = p-&gt;tm_min;
cTime.second = p-&gt;tm_sec;
cTime.weekday = p-&gt;tm_wday;
cTime.days = p-&gt;tm_yday;
cTime.isdst = p-&gt;tm_isdst; //是否是夏令时

}

int main(int argc, char **argv)
{
LOG_INFO("argc: %d\n",argc);
getCurrentTime();

LOG_INFO("cuurent time is: %d/%d/%d %d:%d:%d  weekday[%d]  days[%d]  isdst[%d]\n",
cTime.year , cTime.mounth,cTime.day,
cTime.hour,cTime.minute,cTime.second,
cTime.weekday,
cTime.days,
cTime.isdst);
}



C语言 延迟执行 某个命令

笔记:C语言延迟执行某条命令


/*
C语言 延迟执行 某个命令
*/
int delay_exec(char *command, int delaySeconds)
{
	int pid = 0;
	int status = 0;
	extern char **environ;

	if ( command == 0 )
		return 1;
	pid = fork();
	if ( pid == -1 )
		return -1;

	if ( pid == 0 ) {
		char *argv[4];
		argv[0] = "sh";
		argv[1] = "-c";
		argv[2] = command;
		argv[3] = 0;

		if(delaySeconds)
		{
			sleep(delaySeconds);
		}

		execve("/bin/sh", argv, environ);
		exit(127);
	}


	return status;
}

C指针操作-函数返回数组

最近开始在https://leetcode-cn.com里面练习算法。
首先来第一题简单的:

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
    你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
示例:
    给定 nums = [2, 7, 11, 15], target = 9
    因为 nums[0] + nums[1] = 2 + 7 = 9
    所以返回 [0, 1]

给定的函数定义为
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target) {
    ... ...
}

于是用C语言完成了该题:

#include "stdio.h"
#include "stdlib.h"

int* twoSum(int* nums, int numsSize, int target) {
    int index1,index2;
    int *result ;
    int *temp;
    
    result = (int*) malloc ( sizeof (int) *2 );
    
    for ( index1 = 0; index1 < numsSize; index1++ )
    {
        for( index2 = index1+1; index2 < numsSize ; index2++ )
        {
            if( target == nums[index1] + nums[index2] )
            {
                temp = result;
                *temp = index1 ;
                
                temp++ ;
                *temp = index2 ;
                
                return result;
            }
        }
    }
    return result;
}

int main(void)
{
	int *array;
	int array_length;
	int *temp;
	int array_index = 0;
	int *result;
	int target = 0;
	
	printf("please input target: ");
	scanf("%d",&target) ;
	
	printf("please input array length: ");
	scanf("%d",&array_length) ;
	
	array = (int *) malloc ( sizeof(int *) * array_length );
	temp = array;
	
	for(array_index = 0; array_index < array_length ; array_index++,temp++)
	{
		printf("\nplease input array[%d]:",array_index);
		scanf("%d",temp);		
	} 
	
	printf("\narray[%d]:",array_length);
	
	for(array_index = 0; array_index < array_length ; array_index++ ,temp++ )
	{
		printf(" %d :",*(array+array_index));	
		
	} 
	
	result = twoSum(array,array_length,target);
	
	if( *(array + *result) + *(array + *(result+1)) != target)
	{
		printf("\n not found");
	}
	else
	{
		printf("\nfind array[%d](%d) + array[%d](%d)  = %d ",
			*result, *(array + *result),
			*(result+1), *(array + *(result+1)),target);
	}
	free(array);
	free(result);
	return 0;
}

运行结果为

please input target: 15
please input array length: 6

please input array[0]:5

please input array[1]:1

please input array[2]:9

please input array[3]:6

please input array[4]:8

please input array[5]:20

array[6]: 5 : 1 : 9 : 6 : 8 : 20 :
find array[2](9) + array[3](6)  = 15 请按任意键继续. . .

考虑之后,觉得函数那样定义有问题:在函数体内malloc,在函数体外free,如果在外面调用之后,忘记free了,就很容易造成内存泄露。

于是进行了改版


#include "stdio.h"
#include "stdlib.h"

int twoSum_v2(int* result, int* nums, int numsSize, int target) 
{
    int index1,index2;
    int *temp;
    for ( index1 = 0; index1 < numsSize; index1++ )
    {
    	for( index2 = index1+1; index2 < numsSize ; index2++ )
        {
        	if( target == (nums[index1] + nums[index2]) )
            {
                temp = result;
                *temp = index1 ;
                
                temp++ ;
                *temp = index2 ;
                 
				return 1;
            }
        }
    }
    return 0;
}
 
int main(void)
{
	int *array;
	int array_length;
	int *temp;
	int array_index = 0;
	int *result;
	int target = 0;
	int found ;
	
	printf("please input target: ");
	scanf("%d",&target) ;
	
	printf("please input array length: ");
	scanf("%d",&array_length) ;
	
	result = (int *) malloc ( sizeof(int *) * 2 );
	array = (int *) malloc ( sizeof(int *) * array_length );
	temp = array;
	
	for(array_index = 0; array_index < array_length ; array_index++,temp++)
	{
		printf("\nplease input array[%d]:",array_index);
		scanf("%d",temp);		
	} 
	
	printf("\narray[%d]:",array_length);
	
	for(array_index = 0; array_index < array_length ; array_index++ ,temp++ )
	{
		printf(" %d ,",*(array+array_index));		
	} 
	
	found = twoSum_v2(result,array,array_length,target);
	if( 0 == found )
	{
		printf("\n not found");
	}	
	else
	{
		printf("\nfind array[%d](%d) + array[%d](%d)  = %d ",
			*result, *(array + *result),
			*(result+1), *(array + *(result+1)),target);		
	}
	
	free(array);
	free(result);
	return 0;
}

测试结果1:

please input target: 25
please input array length: 3

please input array[0]:0

please input array[1]:26

please input array[2]:4

array[3]: 0 , 26 , 4 ,
 not found请按任意键继续. . .

测试结果2:

please input target: 33
please input array length: 5

please input array[0]:20

please input array[1]:15

please input array[2]:23

please input array[3]:7

please input array[4]:10

array[5]: 20 , 15 , 23 , 7 , 10 ,
find array[2](23) + array[4](10)  = 33 请按任意键继续. . .

另外对于结果的输出可以换成下面的,打印结果是一样的

	/* printf("\nfind array[%d](%d) + array[%d](%d)  = %d ",
			*result, *(array + *result),
			*(result+1), *(array + *(result+1)),target);		
		*/
	printf("\nfind array[%d](%d) + array[%d](%d)  = %d ",
			result[0], array[result[0]],
			result[1], array[result[1]],target);

小结:
这个题目在面试用应该会经常用到,主要知识点是:
1. 函数返回的数组必须是一个已分配的地址
2. malloc与free要成对
3. 使用malloc动态分配的数组,可以用下标方式访问。