、
随机打印“hi, April~”十次!
static可保留上次函数调用返回的值
include <stdio.h>
long long func(int n); // 函数声明
int main() {
int n;
long long f;
while (scanf("%d", &n) != EOF) {
f = func(n); // 函数调用
printf("n = %d, f = %lld\n", n, f);
}
return 0;
}
long long func(int n){
int i,sum,m;
sum=0;
m=0;
for(i=1;i<=n;i++){
m=2*m+1;
}
return m;
}
迭代法
include <stdio.h>
int func(int n, int m);
int main() {
int n, m;
while(scanf("%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){
int x,y,k;
if(m==0){
k=1;
}
else {
k=0;
for(x=n;x>=(n-m+2);x--){
x=x(x-1);
}
for(y=m;y>=2;y--){
y=y(y-1);
}
k=x/y;
}
return k;
}
递归法
include <stdio.h>
int func(int n, int m);
int main() {
int n, m;
while(scanf("%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){
int k;
if(m==0){
k=1;
}
else if(n<m){
k=0;
}
else{
k=func(n-1,m)+func(n-1,m-1);
}
return k;
}
include <stdio.h>
include<math.h>
void hanoi(unsigned int n,char from,char temp,char to);
void moveplate(unsigned int n,char from,char to);
int main()
{
unsigned int n;
while(scanf("%u",&n)!=EOF)
{
int m;
hanoi(n,'A','B','C');
m=pow(2,n)-1;
printf("\n一共移动了%d次.\n\n",m);
}
return 0;
}
void hanoi(unsigned int n,char from,char temp,char to)
{
if(n==1)
{
moveplate(n,from,to);
}
else
{
hanoi(n-1,from,to,temp);
moveplate(n,from,to);
hanoi(n-1,temp,from,to);
}
}
void moveplate(unsigned int n,char from,char to)
{
printf("%u:%c --> %c\n",n,from,to);
}
include <stdio.h>
include <math.h>
long func(long s);// 函数声明
int main() {
long s, t;
printf("Enter a number: ");
while (scanf("%ld", &s) != EOF) {
t = func(s); // 函数调用
printf("new number is: %ld\n\n", t);
printf("Enter a number: ");
}
return 0;
}
long func(long s)
{
long n,x,y;
x=0,y=0;
do
{
n=s%10;
if(n%2==1)
x=x10+n;
s=s/10;
}while(s>0);
do{
n=x%10;
y=y10+n;
x=x/10;
}while(x>0);
return y;
}