【C语言Coding】第十天
原创
©著作权归作者所有:来自51CTO博客作者小样小响的原创作品,请联系作者获取转载授权,否则将追究法律责任
char * defangIPaddr(char * address){
int len = strlen(address);
char* p = (char*)malloc(sizeof(char)*len + 6 + 1);
int i,j;
for(i=0,j=0;i<len;i++){
if(address[i]=='.'){
j +=sprintf(p+j,"%s","[.]");
//返回值:打印字符的个数
// p[j++] = '[';
// p[j++] = '.';
// p[j++] = ']';
}else{
p[j++] = address[i];
}
}
p[len+6] = '\0';
return p;
}
char* pathEncryption(char* path) {
int len = strlen(path);
char* p = (char*)malloc(sizeof(char)*(len+1));
for(int i=0;i<len+1;i++){
if(path[i] == '.') p[i] = ' ';
else p[i] = path[i];
}
p[len] = '\0';
return p;
}
int cmp (const void* p1,const void* p2){
return *(int*)p1 - *(int*)p2;
}
int maxProductDifference(int* nums, int numsSize){
qsort(nums,numsSize,sizeof(int),cmp);
return nums[numsSize-1]*nums[numsSize-2] - nums[0]*nums[1];
}
int cmp(const int* p1,const int* p2){
return *p2-*p1;
}
int largestPerimeter(int* nums, int numsSize){
qsort(nums,numsSize,sizeof(int),cmp);
for(int i=0;i<numsSize-2;i++){
if(nums[i]<nums[i+1]+nums[i+2]) return nums[i]+nums[i+1]+nums[i+2];
}
return 0;
}
int cmp(const int* p1,const int* p2) {
return *p1-*p2;
}
int arrayPairSum(int* nums, int numsSize){
qsort(nums,numsSize,sizeof(int),cmp);
int sum = 0;
for(int i = 0;i<numsSize;i+=2){
sum+=nums[i];
}
return sum;
}