1.
#include<stdio.h>
long fun(long s);
int main(){
long s,t;
printf("enter a number:");
while(scanf_s("%ld",&s)!=EOF){
t=fun(s);
printf("new number is:%ld\n\n",t);
printf("enter a number:");
}
return 0;
}
long fun(long s)
{
int n=1,m,t=0;
while(s>0)
{
m=s%10;
if(m%2!=0)
{
t=t+n*m;
n=n*10;
}
s=s/10;
}
return t;
}
2.
#include <stdio.h>
long long fac(int n); // 函数声明
int main() {
int i, n;
printf("Enter n: ");
scanf_s("%d", &n);
for (i = 1; i <= n; ++i)
printf("%d! = %lld\n", i, fac(i));
return 0;
}
// 函数定义
long long fac(int n) {
static long long p = 1;
printf("p = %lld\n", p);
p = p * n;
return p;
}
#include <stdio.h>
int func(int, int); // 函数声明
int main() {
int k = 4, m = 1, p1, p2;
p1 = func(k, m); // 函数调用
p2 = func(k, m); // 函数调用
printf("%d, %d\n", p1, p2);
return 0;
}
// 函数定义
int func(int a, int b) {
static int m = 0, i = 2;
i += m + 1;
m = i + a + b;
return m;
}
3. #include <stdio.h>
long long func(int n); // 函数声明
int main() {
int n;
long long f;
while (scanf_s("%d", &n) != EOF) {
f = func(n); // 函数调用
printf("n = %d, f = %lld\n", n, f);
}
return 0;
}
// 函数定义
// 待补足。。。
long long func(int n) {
if (n == 1)
return 1;
else
return 2 * func(n - 1) + 1;;
}
4. #include <stdio.h>
int func(int n, int m);
int main() {
int n, m;
while (scanf_s("%d%d", &n, &m) != EOF)
printf("n = %d, m = %d, ans = %d\n", n, m, func(n, m));
return 0;
}
// 函数定义
// 待补足。。
int func(int n, int m) {
if (n == m)
return 1;
else if (m == 1)
return n;
else if (m == 0)
return 1;
else if (n < m)
return 0;
else
return func(n - 1, m) + func(n - 1, m - 1);
}
5. #include <stdio.h>
#include<stdlib.h>
int mul(int n, int m);
int main() {
int n, m;
while (scanf_s("%d%d", &n, &m) != EOF)
printf("%d * %d = %d\n", n, m, mul(n, m));
return 0;
}
int mul(int n, int m) {
if (n == 0 || m == 0)
return 0;
else if (n == 1)
return m;
else
return mul(n - 1, m) + m;
}
6. #include<stdio.h>
int a = 0;void hanoi(int i, char one, char two, char three);
int main() {int n;
printf("请输入盘子数:");
while(scanf_s("%d", &n)!=EOF)
{
hanoi(n, 'A', 'B', 'C');
printf("共需要%d步\n", a);
printf("请输入盘子数:");
}
return 0;
}
void hanoi(int i, char one, char two, char three) {
void move(int x, int y);
if (i == 1) {
move(one, three);
}
else {
hanoi(i - 1, one, three, two);
move(one, three);
hanoi(i - 1, two, one, three);
}
}
void move(int x, int y) {
a++;
printf("%c-->%c\n", x, y);
}
7. #include<stdio.h>
#include<stdlib.h>
int isprime(int n);
int main() {
int i, n;
for (i = 2; i <= 20; i = i + 2)
{
for (n = 2; n <=i/2; n++)
{
if (isprime(n) && isprime(i - n))
{
printf("%d=%d+%d\n", i, n, i - n);
break;
}
}
}
return 0;
}
int isprime(int n) {
int i = 2;
if (n < 2)
return 0;
for (i; i <=n/2; i++)
{
if (n % i == 0)
return 0;
}
}
8. #include<stdio.h>
long fun(long s);
int main(){
long s,t;
printf("enter a number:");
while(scanf_s("%ld",&s)!=EOF){
t=fun(s);
printf("new number is:%ld\n\n",t);
printf("enter a number:");
}
return 0;
}
long fun(long s)
{
int n=1,m,t=0;
while(s>0)
{
m=s%10;
if(m%2!=0)
{
t=t+n*m;
n=n*10;
}
s=s/10;
}
return t;
}
标签:return,int,long,实验,func,printf,include From: https://www.cnblogs.com/wcf8/p/16871350.html