refactor
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
#include "stdio.h"
|
||||
|
||||
char *delete_occurence_character(char *src , char target){
|
||||
char *front = src;
|
||||
char *rear = src;
|
||||
while(*front != '\0'){
|
||||
if (*front != target){
|
||||
*rear = *front;
|
||||
rear++;
|
||||
}
|
||||
front++;
|
||||
}
|
||||
*rear = '\0';
|
||||
return src;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char test[] = "abcdeccba";
|
||||
printf("%s\n", delete_occurence_character(test,'c'));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
|
||||
int factorial(int n){
|
||||
|
||||
int ret=0;
|
||||
(n==0) || (ret=n+factorial(n-1));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int n=10;
|
||||
printf("factorial(10)= %d\n",factorial(10));
|
||||
printf("factorial(5)= %d\n",factorial(5));
|
||||
printf("factorial(1)= %d\n",factorial(1));
|
||||
printf("factorial(0)= %d\n",factorial(0));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
|
||||
#include "stdio.h"
|
||||
#include "time.h"
|
||||
|
||||
|
||||
|
||||
int fibonacci(int n){
|
||||
|
||||
int result[2] = {0,1};
|
||||
if (n<2)
|
||||
{
|
||||
return result[n];
|
||||
}
|
||||
|
||||
return fibonacci(n-1)+fibonacci(n-2);
|
||||
}
|
||||
|
||||
|
||||
// 循环
|
||||
int fibonacci2(int n){
|
||||
|
||||
int result[2] = {0,1};
|
||||
|
||||
if ( n<2 )
|
||||
{
|
||||
return result[n];
|
||||
}
|
||||
|
||||
int fibOne=0;
|
||||
int fibTwo=1;
|
||||
int fibN;
|
||||
for (int i = 2; i < n; ++i)
|
||||
{
|
||||
fibN = fibOne + fibTwo;
|
||||
fibOne = fibTwo;
|
||||
fibTwo = fibN;
|
||||
|
||||
}
|
||||
|
||||
return fibN;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int start,end;
|
||||
long result;
|
||||
|
||||
start = clock();
|
||||
result=fibonacci2(40);
|
||||
end = clock();
|
||||
printf("%ld %.6fs",result,(double)(end-start)/CLOCKS_PER_SEC);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#include "stdio.h"
|
||||
|
||||
int longest_continuious_number(const char *input,char *output){
|
||||
int max = 0;
|
||||
char *start= input;
|
||||
char *mid = input;
|
||||
char *end = input;
|
||||
int tmp = 0;
|
||||
if (input == NULL || output == NULL) return 0;
|
||||
|
||||
while (*end != '\0'){
|
||||
if (*end < '0' || *end > '9'){//字母
|
||||
if(tmp > max){
|
||||
max = tmp;
|
||||
start = mid;
|
||||
}
|
||||
tmp = 0;
|
||||
}else{//数字
|
||||
if (tmp == 0){//发现数字
|
||||
mid = end;
|
||||
}
|
||||
tmp++;
|
||||
}
|
||||
end++;
|
||||
}
|
||||
|
||||
//修改已数字结尾的bug
|
||||
if(tmp > max){
|
||||
max = tmp;
|
||||
start = mid;
|
||||
}
|
||||
|
||||
//copy
|
||||
int i=0;
|
||||
while(i<max){
|
||||
*(output+i) = *(start+i);
|
||||
i++;
|
||||
}
|
||||
*(output+i)='\0';
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
const char test[] = "abcd12345ed125ss123456789";
|
||||
char output[100] = {};
|
||||
int max = longest_continuious_number(test,output);
|
||||
printf("max=%d,number=%s\n", max,output);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
//打印和为n的2个值
|
||||
void print_continuous_sequence_sum(int n){// n=1,2
|
||||
|
||||
int small=1;
|
||||
int big=2;
|
||||
int sum = small+big;
|
||||
while(small<big && big<=n/2+1){
|
||||
|
||||
if (sum==n)
|
||||
{
|
||||
printf("%d ,%d\n",small,big);
|
||||
|
||||
}
|
||||
|
||||
while(sum>n){
|
||||
sum-=small;
|
||||
small++;
|
||||
if (sum==n)
|
||||
{
|
||||
printf("%d, %d\n",small,big);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
big++;
|
||||
sum+=big;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
|
||||
print_continuous_sequence_sum(115);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user