当前位置:网站首页>Loop structure programming 2
Loop structure programming 2
2022-06-09 18:30:00 【Minghai step】
The first 1 Turn off :C loop - For the average score
Task description
Our mission : Write a program , Enter the number of students and each person's grade , Calculate average .
###### Be careful : When the number of students entered is less than or equal to 0 when , The average score is output 0 branch !
for example :




Related knowledge
In programming , We often encounter situations where we need to run a statement or a piece of code several times , If there is no circular statement , To be prepared item by item , It will lead to the complexity and redundancy of the program .
C Available in while、for、do-while And other commonly used loop functions to repeat statements or code blocks , So as to save a lot of repetitive work , Make the code simple and clear .
while sentence
Basic grammar : be based on while The basic syntax for implementing a loop is as follows :
while( Boolean value ){Statements or blocks of code that require a loop}
- If the Boolean value in brackets is true , This goes into the loop , Until the Boolean becomes false , Then we will launch the cycle , After the execution continues, the code .
- If the Boolean value in brackets is false , Then it will not enter the cycle directly , After continuing to execute the loop code block, the code .
Application example :
// Cyclic output is required 3 Time 6int n = 3;while(n--){printf("6");}
for sentence
Basic grammar : be based on for The basic syntax for implementing a loop is as follows :
for(1. Assignment statement or omit ;2. Judge or omit ;3. Execute a statement or omit ){Statements or code blocks that need to be looped}
- among
1Location , It's an assignment statement , In circulationStartDo it once before , Then do not execute . - among
2Position is a judgment statement , Every timeStart the cyclewhen ( Including the first time ) Will go through the judgment statement first- If this is true , Then enter the cycle , Execute loop statements or code blocks .
- If this is false , Then it does not enter the cycle , Code after execution .
- among 3 Position is an operation statement , Every time
After executionA loop , Will execute the code here once .
Application example :
// Output is also required 3 individual 6int i;for(i=0;i<3;i++){printf("6");}
Programming requirements
The programming task of this pass is to complete the code fragment on the right Begin to End The code in the middle , The specific requirements are as follows :
Programming to realize : Write a program , Enter the number of students and each person's grade , Calculate average .
###### Be careful : When the number of students entered is less than or equal to 0 when , The average score is output 0 branch !
for example :




-
The code framework of the code file involved in this level is as follows :
#include <stdio.h>// Definition main functionint main(){// Please add code here/********** Begin *********//********** End **********/return 0;}
Test instructions
Here is a sample test :
Input :3 90 70 80
Output :the number of students:the scores:average=80.00
Input :-1
Output :the number of students:the scores:average=0.00
Input :4 78.5 26 73.6 90.1
#include<stdio.h>
int main()
{
int n,i;
float score,sum=0,average;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%f",&score);
sum+=score;
}
average=(n<=0?0:sum/n);
printf("the number of students:the scores:average=%.2f",average);
}The first 2 Turn off :C loop - Find the product of your numbers
Task description
Our mission : Calculate a positive integer num The product of the numbers on .
for example :
Input :2583 after ----(2x5x8x3) Output :240
Input :102 after ----(1x0x2) Output :0
Input :136 after ----(1x3x6) Output :18
Related knowledge
In programming , We often encounter situations where we need to run a statement or a piece of code several times , If there is no circular statement , To be prepared item by item , It will lead to the complexity and redundancy of the program .
C Available in while、for、do-while And other commonly used loop functions to repeat statements or code blocks , So as to save a lot of repetitive work , Make the code simple and clear .
while sentence
Basic grammar : be based on while The basic syntax for implementing a loop is as follows :
while( Boolean value ){Statements or blocks of code that require a loop}
- If the Boolean value in brackets is true , This goes into the loop , Until the Boolean becomes false , Then we will launch the cycle , After the execution continues, the code .
- If the Boolean value in brackets is false , Then it will not enter the cycle directly , After continuing to execute the loop code block, the code .
Application example :
// Cyclic output is required 3 Time 6int n = 3;while(n--){printf("6");}
for sentence
Basic grammar : be based on for The basic syntax for implementing a loop is as follows :
for(1. Assignment statement or omit ;2. Judge or omit ;3. Execute a statement or omit ){Statements or code blocks that need to be looped}
- among
1Location , It's an assignment statement , In circulationStartDo it once before , Then do not execute . - among
2Position is a judgment statement , Every timeStart the cyclewhen ( Including the first time ) Will go through the judgment statement first- If this is true , Then enter the cycle , Execute loop statements or code blocks .
- If this is false , Then it does not enter the cycle , Code after execution .
- among
3Position is an operation statement , Every timeAfter executionA loop , Will execute the code here once .
Application example :
// Output is also required 3 individual 6int i;for(i=0;i<3;i++){printf("6");}
Programming requirements
The programming task of this pass is to complete the code fragment on the right Begin to End The code in the middle , The specific requirements are as follows :
Calculate a positive integer num The product of the numbers on .
for example :
Input :2583 after ----(258*3) Output :240
Input :102 after ----(102) Output :0
Input :136 after ----(136) Output :18
The code framework of the code file involved in this level is as follows :
#include <stdio.h>// Definition main functionint main(){// Please add code here/********** Begin *********//********** End **********/return 0;}
Test instructions
Here is a sample test :
Input :120
Output :0
Input :314
Output :12
Input :1952
Output :90
#include<stdio.h>
int main()
{
int i,a,b,c=1;
scanf("%d",&a);
if(a%10==0)
{
printf("0\n");
}
else
{
while(a%10!=0)
{i=(int)a/10;
b=a%10;
a=i;
c=c*b;}
printf("%d\n",c);
}
return 0;
}
The first 3 Turn off :C loop - Find the sum of factorials
Task description
Our mission : Write a program , Any input n, seek S=1!+2!+...+n!. Be careful :n! Express n The factorial .0 The factorial of is equal to 1, The factorial of a negative number is equal to 0.
* Tips :(n+1)!=n!(n+1)** for example :
Input :10
Output :4037913
Input :7
Output :5913
Input :-1
Output :0
Related knowledge
In programming , We often encounter situations where we need to run a statement or a piece of code several times , If there is no circular statement , To be prepared item by item , It will lead to the complexity and redundancy of the program .
C Available in while、for、do-while And other commonly used loop functions to repeat statements or code blocks , So as to save a lot of repetitive work , Make the code simple and clear .
while sentence
Basic grammar : be based on while The basic syntax for implementing a loop is as follows :
while( Boolean value ){Statements or blocks of code that require a loop}
- If the Boolean value in brackets is true , This goes into the loop , Until the Boolean becomes false , Then we will launch the cycle , After the execution continues, the code .
- If the Boolean value in brackets is false , Then it will not enter the cycle directly , After continuing to execute the loop code block, the code .
Application example :
// Cyclic output is required 3 Time 6int n = 3;while(n--){printf("6");}
for sentence
Basic grammar : be based on for The basic syntax for implementing a loop is as follows :
for(1. Assignment statement or omit ;2. Judge or omit ;3. Execute a statement or omit ){Statements or code blocks that need to be looped}
- among
1Location , It's an assignment statement , In circulationStartDo it once before , Then do not execute . - among
2Position is a judgment statement , Every timeStart the cyclewhen ( Including the first time ) Will go through the judgment statement first- If this is true , Then enter the cycle , Execute loop statements or code blocks .
- If this is false , Then it does not enter the cycle , Code after execution .
- among
3Position is an operation statement , Every timeAfter executionA loop , Will execute the code here once .
Application example :
// Output is also required 3 individual 6int i;for(i=0;i<3;i++){printf("6");}
Programming requirements
The programming task of this pass is to complete the code fragment on the right Begin to End The code in the middle , The specific requirements are as follows :
Programming to realize : Any input n, seek S=1!+2!+...+n!. ###### Be careful :n! Express n The factorial .0 The factorial of is equal to 1, The factorial of a negative number is equal to 0.
The code framework of the code file involved in this level is as follows :
#include <stdio.h>// Definition main functionint main(){// Please add code here/********** Begin *********//********** End **********/return 0;}
Test instructions
Here is a sample test :
Input :10
Output :4037913
Input :1
Output :1
Input :-5
Output :0
#include <stdio.h>
int main()
{ int sum,item,i,j,n;
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{item=1;
for(j=1;j<=i;j++)
item=item*j;
sum=sum+item;
}
printf("%d",sum);
return 0;
}The first 4 Turn off :C loop - Narcissistic number
Task description
Our mission : Find all the daffodils .
Tips : The so-called narcissus number refers to a three digit number , The cubic sum of its digits is equal to the number itself . such as 153 It's a narcissus number , because 153=1^3+5^3+3^3.
Be careful : There is no need to enter a sentence , Due to site restrictions, input and output examples are required , But students can ignore the input part .
for example :370 It's a narcissus number , because 370 = 3^3 +7^3 + 0^3
Related knowledge
In programming , We often encounter situations where we need to run a statement or a piece of code several times , If there is no circular statement , To be prepared item by item , It will lead to the complexity and redundancy of the program .
C Available in while、for、do-while And other commonly used loop functions to repeat statements or code blocks , So as to save a lot of repetitive work , Make the code simple and clear .
while sentence
Basic grammar : be based on while The basic syntax for implementing a loop is as follows :
while( Boolean value ){Statements or blocks of code that require a loop}
- If the Boolean value in brackets is true , This goes into the loop , Until the Boolean becomes false , Then we will launch the cycle , After the execution continues, the code .
- If the Boolean value in brackets is false , Then it will not enter the cycle directly , After continuing to execute the loop code block, the code .
Application example :
// Cyclic output is required 3 Time 6int n = 3;while(n--){printf("6");}
for sentence
Basic grammar : be based on for The basic syntax for implementing a loop is as follows :
for(1. Assignment statement or omit ;2. Judge or omit ;3. Execute a statement or omit ){Statements or code blocks that need to be looped}
- among
1Location , It's an assignment statement , In circulationStartDo it once before , Then do not execute . - among
2Position is a judgment statement , Every timeStart the cyclewhen ( Including the first time ) Will go through the judgment statement first- If this is true , Then enter the cycle , Execute loop statements or code blocks .
- If this is false , Then it does not enter the cycle , Code after execution .
- among
3Position is an operation statement , Every timeAfter executionA loop , Will execute the code here once .
Application example :
// Output is also required 3 individual 6int i;for(i=0;i<3;i++){printf("6");}
Programming requirements
The programming task of this pass is to complete the code fragment on the right Begin to End The code in the middle , The specific requirements are as follows :
Find all the daffodils .
Tips : The so-called narcissus number refers to a three digit number , The cubic sum of its digits is equal to the number itself . such as 153 It's a narcissus number , because 153=1^3+5^3+3^3.·
Be careful : There is no need to enter a sentence , Due to site restrictions, input and output examples are required , But students can ignore the input part .
for example :370 It's a narcissus number , because 370 = 3^3 +7^3 + 0^3
The code framework of the code file involved in this level is as follows :
#include <stdio.h>// Definition main functionint main(){// Please add code here/********** Begin *********//********** End **********/return 0;}
Test instructions
Here is a sample test :
Input : 1
#include <stdio.h>
int main()
{
int i,a,b,c;
for(i=100;i<1000;i++)
{
a=i/100;// Hundred bit
b=i%100/10;// ten Or you could write it as b=i/10%10;
c=i%10;// bits
if(a*a*a+b*b*b+c*c*c==i)
{
printf("%d ",i);
}
}
return 0;
}
The first 5 Turn off :C loop - Find the perfect number
Task description
Our mission : If a number is exactly equal to the sum of its factors , This number is called " Complete ". for example ,6 The factor is 1、2、3, and 6=1+2+3, therefore 6 yes " Complete ". Program to find out 1000 All the completions in .
Related knowledge ( A little )
Programming requirements
According to the prompt , On the right editor Begin-End Add the code , And complete the task as required .
Input 1000
Output
Program to find out 1000 All the completions in , Each of them takes up one line .
Test instructions
Input :1000
Output :628496
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int i, j;
for(i = 2; i <= 1000; i++) {
int sum = 0;
for(j = 1; j <= i / 2; j++)
if(i % j == 0) sum += j;
if(sum == i)
printf("%d\n", sum);
}
/*********End**********/
return 0;
}
The first 6 Turn off : Sum fractions
Task description
Our mission : Write programs to calculate 1 - 1/2 + 1/3 - 1/4 + ..... +1/99 - 1/100 Value , And show it ( Keep the result to three decimal places ).
Related knowledge ( A little )
Programming requirements
According to the prompt , On the right editor Begin-End Add the code , Write programs to calculate 1 - 1/2 + 1/3 - 1/4 + ..... +1/99 - 1/100 Value , And show it ( Keep the result to three decimal places ).
Test instructions
The platform will test your code , If it is the same as the expected output , Customs clearance .
Start your mission , wish you success !
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int s;
double sum,t,d;
sum=1.0;
d=2.0;
s=1;
while(d <= 100)
{
s=(-1)*s;
t=s*(1/d);
sum = sum + t;
d=d+1;
}
printf("%.3f\n",sum);
/*********End**********/
return 0;
}
边栏推荐
猜你喜欢

【高阶知识】用户态协议栈之Epoll实现原理

Explain MySQL index

Go 最细节篇|pprof 统计的内存总是偏小?

SQL题目整理

3 个注解,优雅的实现微服务鉴权

Details on how Tencent can reduce the live broadcast delay by more than 90%

聊聊 MQ 技术选型

空闲内存的管理

Zhichong launched net zero series energy storage and charging all-in-one machine to create a net zero future with BYD

For 11 years, the programmer gave suggestions to undergraduate, graduate students and students preparing to engage in background development to learn the way to advance
随机推荐
Golang Foundation (2)
Explain MySQL index
Golang基础(1)
C# 30. 字符串截取
Redis source code learning-02_ memory allocation
QUIC会成为互联网传输的颠覆者吗?
Welcome to the InfoQ writing platform!
智充推出NET ZERO SERIES储能充一体机,携手比亚迪共创净零未来
Goldfish rhca memoirs: do447 management list -- compile yaml list file
Development direction of hybrid cloud storage
Golang Foundation (1)
Transformer模型新SOTA--fully attentional networks (FANs) 学习笔记
Squeeze-and-Excitation Networks学习笔记
【高阶知识】用户态协议栈之Epoll实现原理
选择结构程序设计
Kubectl latest common command --v1.24 version
DM8查看SQL执行计划的5种方法(测试+调优用)
C# 32. 静态类实现下拉框选择网卡
Golang Foundation (3)
redis源码学习-05_字典、哈希表