一:
编写函数fun,其功能是:输入一个正整数,将其每一位上为偶数的数取出重新构成一个新数并输出。主函数负责输入输出,如输入87653142,则输出8642。(main函数->fun函数)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int fun1(int n)
{int last = 0;//最后一位int sl = 1;//位int ret = 0;while (n > 0){last = n % 10;//取最后一位数字。if (last % 2 == 0){ret = last * sl + ret;sl *= 10;}n /= 10;//去掉最后一位}return ret;
}int main()
{int input = 0;scanf("%d", &input);printf("%d ",fun1(input));
}
二:
编写函数fun,其功能是:找出100至x (x<=999) 之间能被3整除且不能被5整除的数输出,并将符合条件的整数个数作为函数值返回。主函数负责输入及输出满足条件的整数个数。(main函数àfun函数)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int fun2(int x)
{int ret = 0;for (int i = 100; i <= x; i++){if (i % 3 == 0 && i % 5 != 0){printf("%d ", i);ret++;}}printf("\n");return ret;
}int main()
{int input = 0;scanf("%d", &input);printf("共有%d个 ",fun2(input));
}
三:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int prime(int x)//prime函数放在sum上边
{if (x < 2){return 0;}for (int i = 2; i <= x / i; i++){if (x % i == 0){return 0;}}return 1;}
int sum(int n)
{int ret = 0;for (int i = 1; i <= n; i++){if (prime(i)){ret += i;}}return ret;
}int main()
{int input = 0;scanf("%d", &input);printf("%d ",sum(input));
}
四:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>int fun4(int n)//fib//递归方法
{if (n <= 2){return 1;}else{return fun4(n - 1) + fun4(n - 2);}
}int main()
{int input = 0;scanf("%d", &input);printf("%d ",fun4(input));return 0;
}
五:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>void fun5(char* s) {int len = strlen(s);//取字符串长度//需要包头文件<string.h>if (len == 1){printf("%c", *s);}else{fun5(s + 1);printf("%c", *s);}
}int main()
{char str[20] = "\0";//不能输入超过20个字符scanf("%s", &str);fun5(str);return 0;
}