zoukankan      html  css  js  c++  java
  • C博客作业02--循环结构

    1.PTA总分

    • 单循环结构

    • 嵌套循环


    2.本章学习总结

    2.1学习内容总结

    for语句

    • 形式:for(表达式1;表达式2;表达式3)
      循环体语句
    • 适用于事先给定循环次数的情况,一目了然。

    while语句

    • 形式:while(表达式)
      循环体语句 ;
    • 适用于循环次数不明确且无需先进行循环体运算的情况。

    do-while语句

    • 形式:do{
      循环体语句
      }while(表达式);
    • 适用于循环次数不明确且需先进入循环体运算,再判断是否进入下一次循环的情况。

    嵌套循环(多重循环):注意分清内外循环以及循环初始化语句的位置。


    break语句和continue语句

    • break语句强制语句结束,不再执行循环体中位于其后的其他语句,且应和if语句配合使用,即条件满足时,才执行break跳出循环。
    • continue语句的作用是跳过循环体中continue后面的语句,继续下一次循环,一般也需要与if语句配合使用。
    • break结束循环,而continue只是跳过后面语句继续循环。break语句除了可以终止循环外,还用于switch语句,而continue只能用于循环。

    2.2本章学习体会

    2.2.1学习体会:很难培养所谓的循环思维,好多次知道要用什么循环语句,但不懂该如何用相关的变量表示出来。

    2.2.2代码量

    第七周 第八周
    代码量 350 335

    3.PTA实验作业

    3.1 7-8 查询水果价格

    3.1.1伪代码

    printf()    //五次输出,打出菜单
    while(最多查询5次)
    scanf();  //用户输入选择
    if (choice == 0)
    {
    结束循环
    }
    if (choice == 1)
    {
    输出苹果价格,次数加一
    }
    else if (choice == 2)
    {
    输出梨子价格,次数加一
    }
    else if (choice == 3)
    {
    输出桔子价格,次数加一
    }
    else if (choice == 4)
    {
    输出葡萄价格,次数加一
    }
    else
    {		
    输出价格为0.00,次数加一	
    }
    结束循环
    

    3.1.2代码截图


    3.1.3造测试数据

    输入 输出 说明
    3 -1 0 2 [1] apple [2] pear [3] orange [4] grape [0] exit price = 4.10 price = 0.00 sample
    1 2 3 3 4 4 5 6 7 8 [1] apple [2] pear [3] orange [4] grape [0] exit price = 3.00 price = 2.50 price = 4.10 price = 4.10 price = 10.20 sample
    1 0 [1] apple [2] pear [3] orange [4] grape [0] exit price = 3.00 第一个数据有效,第二次输入0时结束循环

    3.1.4 PTA提交列表及说明

    答案错误原因:菜单中的选项梨子pear错写成peaar。。


    3.2 7-7 换硬币

    3.2.1伪代码

    for(number5=19;number5>=1;number5--)    //对5分硬币的数量进行控制,数量为1到19之间
    { 
       for(number2=49;number2>=1;number2--)      //对2分硬币的数量进行控制,数量为1到49之间
    
     { 
           for(number1=99;number1>=1;number1--)     //对1分硬币的数量进行控制,数量为1到99之间
    
            { 
               
                if(number5*5+number2*2+number1==x)    //判断三种硬币的数量是否满足题目要求
                {
                printf("fen5:%d, fen2:%d, fen1:%d, total:%d
    ",number5,number2,number1,number5+number2+number1);     //若满足,输出相应硬币的数量
           
                count++;     //次数加一,用来统计换法个数
                }
            }
      }
    }
    结束循环
    

    3.2.2代码截图


    3.2.3造测试数据

    输入 输出 说明
    13 fen5:2, fen2:1, fen1:1, total:4 fen5:1, fen2:3, fen1:2, total:6 fen5:1, fen2:2, fen1:4, total:7 fen5:1, fen2:1, fen1:6, total:8 count = 4 sample

    3.2.4 PTA提交列表及说明

    错误代码:

    #include<stdio.h>
    int main()
    {
    
    
       int number5;
       int number2;
       int number1;
       int count;
       int x;
       scanf("%d",&x);
       count=0;
       for(number5=1;number5<=19;number5++)
       { 
           for(number2=1;number2<=49;number2++)
           { 
               for(number1=1;number1<=99;number1++)
               { 
               
               if(number5*5+number2*2+number1==x)
               {
               printf("fen5:%d, fen2:%d, fen1:%d, total:%d
    ",number5,number2,number1,number5+number2+number1);
           
               count++;
               }
               }
           }
       }
       printf("%d",count);
       return 0;
    }
    

    答案错误原因:没看清题目要求,题目要求按5分、2分和1分硬币的数量依次从大到小的顺序,输出各种换法,按照此代码的话会从小到大输出各种换法,不符合题目要求。


    3.3 7-2 梅森数

    3.3.1伪代码

    for (i = 1; i <= n; i++)    
    {
           number = pow(2, i) - 1;     //表示出梅森数的形式
       for (j = 2; j <= number; j++)   //进行number是否是素数的判断
       {
       	if (number % j == 0)
       	{
       		结束循环
       	}
       	if (j > number / 2 && number != 1)  
       	{
       		leap = 2;   //表示状态,说明number是梅森数
       		printf("%d
    ", number);
       		break;
       	}
       }					
    }	
    
    

    3.3.2代码截图


    3.3.3造测试数据

    输入 输出 说明
    6 3 7 31 sample
    1 None 输入最小的正整数,则不存在这样的梅森数

    3.3.4 PTA提交列表及说明

    说明:注意一下素数的判断,代码中j的取值范围可以改为2到number的一半或者2到number的算术平方根。


    3.代码互评

    7-8 查询水果价格

    同学代码1:

    #include<stdio.h>
    int main(void)
    {
       printf("[1] apple
    [2] pear
    [3] orange
    [4] grape
    [0] exit
    ");
    
       int a;
       int k;
    
       for (a = 0; a <= 4; a++)
       {
       	scanf("%d", &k);
       	if (k == 0)
       	{
       		break;
       	}
       	switch (k)
       	{
       	case 1:printf("price = 3.00
    ");
       		break;
       	case 2:printf("price = 2.50
    ");
       		break;
       	case 3:printf("price = 4.10
    ");
       		break;
       	case 4:printf("price = 10.20
    ");
       		break;
       	default:printf("price = 0.00
    ");
       		break;
       	}
       }
       return 0;
    }​
    
    

    同学代码2:

    #include<stdio.h>
    int main()
    {
       int num;
       int i;
       printf("[1] apple
    ");
       printf("[2] pear
    ");
       printf("[3] orange
    ");
       printf("[4] grape
    ");
       printf("[0] exit
    ");
       for (i = 1; i <= 5; i++)
       {
       	scanf("%d", &num);
       	if (num == 0)
       		break;
       	switch (num)
       	{
       	case 1:
       		printf("price = 3.00
    "); break;
       	case 2:
       		printf("price = 2.50
    "); break;
       	case 3:
       		printf("price = 4.10
    "); break;
       	case 4:
       		printf("price = 10.20
    "); break;
       	default:
       		printf("price = 0.00
    "); break;
       	}
       }
       return 0;
    }
    

    我的代码:

    #include<stdio.h>
    int main()
    {
       int choice;
       int count = 1;
       printf("[1] apple
    ");
       printf("[2] pear
    ");
       printf("[3] orange
    ");
       printf("[4] grape
    ");
       printf("[0] exit
    ");
       while (count <=5)
       {
       	scanf("%d", &choice);
       	if (choice == 0)
       	{
       		break;
       	}
       	if (choice == 1)
       	{
       		printf("price = 3.00
    ");
       		count++;
       	}
       	else if (choice == 2)
       	{
       		printf("price = 2.50
    ");
       		count++;
       	}
       	else if (choice == 3)
       	{
       		printf("price = 4.10
    ");
       		count++;
       	}
       	else if (choice == 4)
       	{
       		printf("price = 10.20
    ");
       		count++;
       	}
       	else
       	{
       		printf("price = 0.00
    ");
               count++;
       	}
       }
       return 0;
    }
    

    • 两个同学都用了for语句来实现循环结构,并且都用了switch语句来实现多分支结构。而我用while语句来实现循环结构,用else-if语句来实现多分支结构。
    • 代码1把菜单的输出用一句printf()语句完成,其中加上换行符。我的代码和代码2都用5句printf()语句来输出菜单。代码1for语句中初值为0,最大值为4;代码2for语句中初值为1,所以最大值为5;我的代码中查询次数的初值为1,所以执行while语句的条件为查询次数小于等于5.
  • 相关阅读:
    Python day43 :pymysql模块/查询,插入,删除操作/SQL注入完全问题/事务/模拟登录注册服务器/视图/函数/存储过程
    docker
    Linux 05
    Linux04
    Linux 03
    Linux 02
    go语言
    go语言
    go语言
    Linux
  • 原文地址:https://www.cnblogs.com/1234hj/p/11708272.html
Copyright © 2011-2022 走看看