Fibonacci数递归法,猴子吃桃难题

By admin in 4858.com on 2019年8月24日

[C语言]猴子吃桃难点

#include<stdio.h>
int main()
{
    int i=0;
    int sum=1;
    for(i=0;i<9;i++)
    {
        sum=2*sum+2;   
    }  
    printf("%d",sum);
    return 0;
}

4858.com 1

#includestdio.hint main(){
int i=0; int sum=1; for(i=0;i9;i++) { sum=2*sum+2; } printf(%d,sum);
return 0;}…

循环语句

巡回:反复实行某段代码。

[plain]
#include <stdio.h> 
 
int fibonacci(int num); 
 
int main() 

    int num; 
    int sum; 
 
    printf(“please enter a number:”); 
    scanf(“%d”, &num); 
Fibonacci数递归法,猴子吃桃难题。 
    sum=fibonacci(num); 
     
    printf(“The result is %d\n”, sum); 
     
    return 0; 

 
int fibonacci(int num) 

    int sum; 
 
    if(num==1 || num==2) 
    { 
       sum=1; 
    } 
    else 
    { 
       sum=fibonacci(num-1)+ fibonacci(num-2); 
    } 
 
    return sum; 

循环四因素:开端标准,循环条件,循环体,状态改动。
for(开头标准;循环条件;状态改换)
{
 循环体
}
break ——中断循环,跳出循环
continue——结束此次巡回,步向下一次循环

循环四成分:起首标准,循环条件,循环体,状态退换。
for(开端标准;循环条件;状态改动)
{
循环体
}

#include <stdio.h>

 

付给早先标准,先决断是还是不是知足循环条件,假若不满意条件则跳过for语句,借使满意则跻身for语句实行,for语句内的代码施行实现后,将鲁人持竿气象改动,改造变量,然后判定是还是不是否和循环条件,符合则继续施行for语句内的代码,直到变量i不相符循环条件则甘休循环,大概蒙受break命令,直接跳出当前的for循环。

int fibonacci(int num);

 //请输入二个正整数 求阶乘的和
            Console.Write(“请输入一个正整数”);
            int a = int.Parse(Console.ReadLine());
            int sum = 1;
            int sum1 = 0;
            for (int i = 1; i <= a; i++)
            {
                sum = sum + i;
            sum1 = sum * i;
           
            }

break ——中断循环,跳出前段时间的循环循环
continue——甘休此番巡回,步向后一次循环

int main()
{
    int num;
 int sum;

            Console.WriteLine(sum1);
            Console.ReadLine();

巡回(for)和分支语句(if  else等)一样能够互相嵌套

 printf(“please enter a number:”);
 scanf(“%d”, &num);

 

 

 sum=fibonacci(num);
   
 printf(“The result is %d\n”, sum);
   
 return 0;
}

第一种:

 

int fibonacci(int num)
{
    int sum;

            三个嬉戏,前20关是每一关作者的分数,
            21-30关每一关是10分
            31-40关,每一关是20分
            41-49关,每一关是30分
            50关,是100分
            输入你现在闯到的卡子数,求你今后具备的分数
            二种做法:if嵌套for    for嵌套if
            Console.Write(“请输入您今后闯到的关卡数:”);
            int guan = int.Parse(Console.ReadLine());
            if (guan >= 1 && guan <= 50)
            {
                int sum = 0;
                if (guan <= 20)
                {
                    for (int i = 1; i <= guan; i++)
                    {
                        sum += i;
                    }
                }
                else if (guan <= 30)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= guan; i++)
                    {
                        sum += 10;
                    }
                }
                else if (guan <= 40)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= guan; i++)
                    {
                        sum += 20;
                    }
                }
                else if (guan <= 49)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= 40; i++)
                    {
                        sum += 20;
                    }
                    for (int i = 41; i <= guan; i++)
                    {
                        sum += 30;
                    }
                }
                else//50关
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= 40; i++)
                    {
                        sum += 20;
                    }
                    for (int i = 41; i <= 49; i++)
                    {
                        sum += 30;
                    }
                    sum += 100;
                }
                Console.WriteLine(sum);
            }
            else
            {
                Console.WriteLine(“输入有误!”);
            }
4858.com,            Console.ReadLine();

死循环:

 if(num==1 || num==2)
 {
    sum=1;
 }
 else
 {
    sum=fibonacci(num-1)+ fibonacci(num-2);
 }

 

for (; ; )
{
 Console.WriteLine(“Hello”);
}

 return sum;
}

第二种

只顾:程序中差别意出现死循环!

 

 

应用

 4858.com 2
 

            Console.Write(“请输入您以后闯到的关卡数:”);
            int guan = int.Parse(Console.ReadLine());
            if (guan <= 50 && guan >= 1)
            {
                int sum = 0;
                for (int i = 1; i <= guan; i++)
                {
                    if (i <= 20)
                    {
                        sum += i;
                    }
                    else if (i <= 30)
                    {
                        sum += 10;
                    }
                    else if (i <= 40)
                    {
                        sum += 20;
                    }
                    else if (i <= 49)
                    {
                        sum += 30;
                    }
                    else
                    {
                        sum += 100;
                    }
                }
                Console.WriteLine(sum);
            }
            else
            {
                Console.WriteLine(“输入有误!”);
            }
            Console.ReadLine();

循环语比较麻烦,多加演练

 

 

1.输入一个大背头,总结从1加到那几个数的结果
Console.Write(“请输入一个正整数:”);
int a = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;//sum=sum+i;
}

] #include stdio.h int fibonacci(int
num); int main() { int num; int sum; printf(please enter a number:);
scanf(%d, num); sum=fibonacci(num); printf(The result is %d\n,
sum);…

 

Console.WriteLine(sum);
Console.ReadLine();

            //找寻100以内与7有关的数并打字与印刷、
            //(1).从1找到100
            //(2).搜索与7关于的数
            //    个位数是7 a%10==7
            //    12位数是7 a/10 ==7
            //    能被7整除 a%7 ==0
            for (int i = 1; i <= 100; i++)
            {
                if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
                {
                    Console.Write(i+”\t”);
                }
            }
            Console.ReadLine();

2.输入一个正整数,求阶乘
Console.Write(“请输入一个正整数:”);
int a = int.Parse(Console.ReadLine());
int sum = 1;
for (int i = 1; i <= a; i++)
{
sum *= i;//sum=sum*i;
}

 

Console.WriteLine(sum);
Console.ReadLine();

3.输入二个正整数,求阶乘的和1!+2!+…+n!
Console.Write(“请输入三个正整数:”);
int a = int.Parse(Console.ReadLine());
int sum = 1;
int sum1 = 0;
for (int i = 1; i <= a; i++)
{
sum *= i;//sum=sum*i;
if (i == 4)//不想要4的
{
continue;//终止本次巡回,继续下一次循环
}
sum1 += sum;
}

Console.WriteLine(sum1);
Console.ReadLine();

 

4.二个游戏,前20关是每一关自家的分数,
21-30关每一关是10分
31-40关,每一关是20分
41-49关,每一关是30分
50关,是100分
输入你今后闯到的卡子数,求您今后有着的分数
三种做法:if嵌套for for嵌套if
(1)Console.Write(“请输入您今后闯到的关卡数:”);

int guan = int.Parse(Console.ReadLine());
if (guan >= 1 && guan <= 50)
{
int sum = 0;
if (guan <= 20)
{
for (int i = 1; i <= guan; i++)
{
sum += i;
}
}
else if (guan <= 30)
{
for (int i = 1; i <= 20; i++)
{
sum += i;
}
for (int i = 21; i <= guan; i++)
{
sum += 10;
}
}
else if (guan <= 40)
{
for (int i = 1; i <= 20; i++)
{
sum += i;
}
for (int i = 21; i <= 30; i++)
{
sum += 10;
}
for (int i = 31; i <= guan; i++)
{
sum += 20;
}
}
else if (guan <= 49)
{
for (int i = 1; i <= 20; i++)
{
sum += i;
}
for (int i = 21; i <= 30; i++)
{
sum += 10;
}
for (int i = 31; i <= 40; i++)
{
sum += 20;
}
for (int i = 41; i <= guan; i++)
{
sum += 30;
}
}
else//50关
{
for (int i = 1; i <= 20; i++)
{
sum += i;
}
for (int i = 21; i <= 30; i++)
{
sum += 10;
}
for (int i = 31; i <= 40; i++)
{
sum += 20;
}
for (int i = 41; i <= 49; i++)
{
sum += 30;
}
sum += 100;
}
Console.WriteLine(sum);
}
else
{
Console.WriteLine(“输入有误!”);
}
Console.ReadLine();

 

 

(2)Console.Write(“请输入您未来闯到的关卡数:”);
int guan = int.Parse(Console.ReadLine());
if (guan <= 50 && guan >= 1)
{
int sum = 0;
for (int i = 1; i <= guan; i++)
{
if (i <= 20)
{
sum += i;
}
else if (i <= 30)
{
sum += 10;
}
else if (i <= 40)
{
sum += 20;
}
else if (i <= 49)
{
sum += 30;
}
else
{
sum += 100;
}
}
Console.WriteLine(sum);
}
else
{
Console.WriteLine(“输入有误!”);
}
Console.ReadLine();

 

5.寻找100以内与7关于的数并打字与印刷、
(1).从1找到100
(2).寻觅与7有关的数
个位数是7 a%10==7
九个人数是7 a/10 ==7
能被7整除 a%7 ==0
for (int i = 1; i <= 100; i++)
{
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
{
Console.Write(i+”\t”);
}
}
Console.ReadLine();

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有