当前位置:网站首页>C language programming based on loop structure (PTA)
C language programming based on loop structure (PTA)
2022-04-23 21:45:00 【Hebuter-Rui】
One 、 The experiment purpose
1. Learn circular sentences for、while and do-while How to use statements .
2. Learn to use circular statements to implement various algorithms , For example, exhaustive method 、 Iterative method, etc .
Two 、 Experimental content
1 Find the minimum
This question requires the preparation of procedures , Find the smallest value in a given series of integers .
Input format : The input first gives a positive integer in a line n, And then n It's an integer , Separated by spaces .. Output format : In a row, press “min = minimum value ” Format output n The smallest of an integer .
2 Print the Jiu formula table
Here's a complete list of the bottom triangle and nine nines :
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
This question requires any given positive integer N, Output from 1*1 To N*N Part of the formula table
Output format : Input gives a positive integer on a line N(1≤N≤9)
Input format : Output the lower triangle N*N Part of the formula table , The number to the right of the equal sign is 4 position 、 Align left
3 Calculate factorial sum
For a given positive integer N, You need to calculate S=1!+2!+3!+...+N!.
Input format : The input gives no more than 10 The positive integer N.
Output format : Output in one line S Value .
4 Sum of integral segments
Given two integers A and B, Output from A To B All the integers of and the sum of these numbers .
Input format : Enter... On one line 2 It's an integer A and B, among −100≤A≤B≤100, Separated by spaces .
Output format : First, output sequentially from A To B All integers of , Every time 5 A line of numbers , Each number accounts for 5 Character width , Align right . Finally, in a line, press Sum = X Output the sum of all the numbers X.
5 Count the student's scores
This question requires writing a program to read in N A student's 100 point mark , Make statistics on the distribution of the scores on the five point system . The conversion rules from 100% to 5% :
• Greater than or equal to 90 It is divided into A;
• Less than 90 Greater than or equal to 80 by B;
• Less than 80 Greater than or equal to 70 by C;
• Less than 70 Greater than or equal to 60 by D;
• Less than 60 by E.
Input format : Input gives a positive integer in the first line N(≤1000), That is, the number of students ; Given in the second line N A student's 100 point mark , Separated by spaces . Output format : Output in one line A、B、C、D、E The distribution of the number of people corresponding to the five point system , Numbers are separated by spaces , There must be no extra space at the end of the line .
6 special a Summation of series of numbers (20 branch )
Given that neither is more than 9 The positive integer a and n, Ask to write a program a+aa+aaa++⋯+aa⋯a(n individual a) The sum of the .
Input format :
The input is given in one line no more than 9 The positive integer a and n.
Output format :
In a row, press “s = Corresponding sum ” Format output .
sample input :
2 3
sample output :
s = 246
7 Narcissistic number
The number of Narcissus refers to a N Positive integer (N≥3), Of the numbers in each of its bits N The sum of the powers is equal to itself . for example :153=1
3
+5
3
+3
3
. This question requires the preparation of procedures , Calculate all N Number of daffodils .
Input format :
Input gives a positive integer on a line N(3≤N≤7).
Output format :
Output all... In ascending order N Number of daffodils , One line for each number .
sample input :
3
sample output :
153
370
371
407
8 Print hourglass (20 branch )
This question requires you to write a program to print the given symbol into the shape of an hourglass . For example, given 17 individual “*”, It is required to print in the following format
*****
***
*
***
*****
So-called “ Hourglass shape ”, Refers to the output of odd symbols per line ; Align symbol centers ; Number difference between two adjacent lines of symbols 2; The number of symbols decreases from large to small to 1, Increase from small to large ; Equal number of first and last symbols .
Given arbitrary N Symbols , It doesn't have to be an hourglass . Require the printed hourglass to use as many symbols as possible .
Input format :
Type in on a line to give 1 A positive integer N(≤1000) And a symbol , Space between .
Output format :
First print out the largest hourglass shape made up of the given symbols , Finally, output the number of unused symbols in one line .
sample input :
19 *
sample output :
*****
***
*
***
*****
2
3、 ... and 、 Screenshot of experimental source program and results
1 Find the minimum
#include <stdio.h>
int main()
{
int n,i = 0,j,min;
do
{
scanf("%d ",&n);
}while(n <= 0); // control n The scope of the
for(i = 0;i < n;i++)
{
scanf("%d ",&j); // Input n It's an integer
if(i == 0)
{
min = j; // Make the first number equal to min
}
else
{
if(j < min) // If j Than min Small then handle j The value is assigned to min
{
min = j;
}
}
i++;
}
printf("min = %d",min); // Output min
return 0;
}
2 Print the Jiu formula table
#include <stdio.h>
int main()
{
int N,i,j;
do
{
scanf("%d",&N);
}while(N <= 0); // control N The scope of the
for(i = 1;i <= N;i++) // Control the number of lines printed
{
for(j = 1;j <= i;j++) // Control the of each line geshu
{
printf("%d*%d=%-4d",j,i,i*j);
}
printf("\n"); // Control line feed
}
return 0;
}
3 Calculate factorial sum
#include <stdio.h>
int main()
{
int N,sum = 0,S,i,j;
do
{
scanf("%d",&N);
}while(N <= 0 || N > 10); // control N The scope of the
for(i = 0;i < N;i++) // The outer loop controls the number of cumulative multipliers
{
for(j = 0;j <= i;j++) // The inner loop controls the end point of each multiplication
{
S = S * (j + 1);
}
sum = sum + S; // Sum of multiplications
S = 1; // initialization S
}
printf("%d",sum); // Output sum
return 0;
}
4 Sum of integral segments
#include <stdio.h>
int main()
{
int A,B,i,j = 1,sum = 0;
do
{
scanf("%d %d",&A,&B);
}while(A<-100||A>100 || B<-100||B>100||A>B); // control A and B The scope of the
for(i = A;i <= B;i++,j++) // from A To B All the numbers of
{
printf("%5d",i); // Output numbers and align them to the right within the width of five characters
sum = sum + i; // Sum up
if(j % 5 == 0)
printf("\n"); // Wrap every five numbers
}
if((j - 1) % 5 != 0) // If the last line is less than five, the output is wrapped
printf("\n");
printf("Sum = %d",sum);
return 0;
}
5 Count the student's scores
#include <stdio.h>
int main()
{
int N,i,j,a=0,b=0,c=0,d=0,e=0; // Define initial values at all levels
do
{
scanf("%d",&N);
}while(N > 1000 || N <= 0); // control N The scope of the
for(i = 0;i < N;i++)
{
scanf("%d",&j);
if(j >= 90)
{
a++; // Results are greater than 90 when a Add one
}
else if(j >= 80 && j < 90)
{
b++; // Results are greater than 80 Less than 90 when b Add one
}
else if(j >= 70 && j < 80)
{
c++; // Results are greater than 70 Less than 80 when b Add one
}
else if(j >= 60 && j < 70)
{
d++; // Results are greater than 70 Less than 60 when b Add one
}
else
{
e++; // The score is less than 60 when e Add one
}
}
printf("%d %d %d %d %d",a,b,c,d,e);
return 0;
} 
6 special a Summation of series of numbers
#include <stdio.h>
int main()
{
int a,n,i;
double s = 0.0,x = 0.0;
do
{
scanf("%d %d",&a,&n);
}while(a<=0 || a > 9 || n <= 0 || n > 9); // control a and n The scope of the
for(i = 0;i < n;i++)
{
x = x*10 + a; // Calculate each aa……a Value
s = s + x; // Sum up
}
printf("s = %.0lf",s); // Output integer
return 0;
} 
7 Narcissistic number
#include <stdio.h>
#include <math.h>
int main()
{
int N,i,j,k,s=1,sum=0,e,a;
scanf("%d",&N);
for(i=pow(10,N-1);i<pow(10,N);i++) // Find all N digit
{
e=i;
for(k=1;k<=N;k++)
{
a=i%10; // Find the single digit
i=i/10;
for(j=1;j<=N;j++)
{
s=s*a; // count a Of N Power
}
sum=sum+s;
s=1;
}
if(sum==e) // If you qualify
{
printf("%d\n",e);
}
sum=0;
i=e;
}
return 0;
}
8 Print hourglass
#include <stdio.h>
int main()
{
int N,i,j=0,m,n,sum=0;
char c;
scanf("%d %c",&N,&c);
for(i=1;1+2*(i*i-1)<=N;i++)
{
j++;
}// Count the number of rows of the largest upper half of the hourglass
for(m=1;m<=j;m++)// Print the upper half of the hourglass
{
for(n=1;n<=2*j-m;n++)
{
if(n<=m-1)// Each line prints a different number of spaces and characters
printf(" ");
else
{
printf("%c",c);
sum++;// Record how many characters are printed
}
}
printf("\n");
}
for(m=1;m<j;m++)// Print the bottom half of the hourglass
{
for(n=1;n<=j+m;n++)
{
if(n<=j-1-m)// Print different spaces and characters on each line
printf(" ");
else
{
printf("%c",c);
sum++;// Record how many characters are printed
}
}
printf("\n");// Line break
}
printf("%d",N-sum);// Output the number of characters remaining
return 0;
} 
Four 、 Analysis and thinking of experiment
1、 Find the minimum
The first number is equal to the minimum , Then compare the following numbers through a loop , If it is less than the minimum value, it is assigned to the minimum value .
2 Print the Jiu formula table
Double loops are used to control rows and columns respectively .
3 Calculate factorial sum
First use the loop to carry out factorial operation , Then sum it .
4 Sum of integral segments
Print the number between the two numbers , A number occupies five digits and records the number of prints , Whenever the number of prints is 5 When it's a multiple of , Wrap once , When the total number of printing times is a multiple of five, there is no line break at last .
5 Count the student's scores
According to the students' grades, compare with the conditions through cycles , If the conditions are met, the corresponding number shall be increased by one .
6 special a Summation of series of numbers
First use the loop to find aa...a Value , And then sum up
7 Narcissistic number
Count out every digit , Use the cycle to find their N Power , And then sum up , Judge whether the conditions are met .
8 Print hourglass
First calculate the maximum number of lines that can print the upper half of the hourglass , Then print the upper half of the hourglass according to the quantitative relationship , Then print the hourglass of the off-duty part according to the quantity relationship .
hypothesis N=5, The maximum number of lines in the upper half is j=3
Row number Space character sum So the number of lines is sum The relationship is sum=N+1- Row number
1 0 5 5 The relationship between the number of spaces and the number of lines is that the number of spaces is equal to the number of lines -1
2 1 3 4
3 2 1 3
Row number Space character sum So the number of lines is sum The relationship is sum= Row number +j
1 1 3 4 The relationship between the number of spaces and the number of lines is that the number of spaces is equal to j-1- Row number
2 0 5 5 As the number of spaces increases, add the number of lines , Gradually reduce the number of lines
版权声明
本文为[Hebuter-Rui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204200618536700.html
边栏推荐
- Tensorflow and pytorch middle note feature map size adjustment to achieve up sampling
- Plato Farm元宇宙IEO上线四大,链上交易颇高
- Opencv application -- jigsaw puzzle
- Prim、Kruskal
- Arm architecture assembly instructions, registers and some problems
- Use 3080ti to run tensorflow GPU = 1 X version of the source code
- C reads excel specific data into specific columns of DataGridView
- What if Jenkins forgot his password
- Tensorflow realizes gradient accumulation, and then returns
- [leetcode refers to offer 47. Maximum value of gift (medium)]
猜你喜欢

Reentrant function
![[leetcode refers to offer 27. Image of binary tree (simple)]](/img/65/85e63a8b7916af058d78d72d775530.png)
[leetcode refers to offer 27. Image of binary tree (simple)]

DW basic tutorial (I)

亚马逊和Epic将入驻,微软应用商城向第三方开放
![[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]](/img/f9/22a379f330c3ee21a2a386bbd4a98f.png)
[leetcode refers to offer 10 - I. Fibonacci sequence (simple)]

Problem brushing plan -- dynamic programming (IV)
![[leetcode refers to offer 22. The penultimate node in the linked list (simple)]](/img/f1/4d5a3552d1c09d2dfa81e0cfc1547e.png)
[leetcode refers to offer 22. The penultimate node in the linked list (simple)]

ROS learning notes - tutorial on the use of ROS

Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对

Plato Farm元宇宙IEO上线四大,链上交易颇高
随机推荐
Cancel the default open project setting of idea
[※ leetcode refers to offer 46. Translate numbers into strings (medium)]
A series of problems of C DataGridView binding list
Pytorch selects the first k maximum (minimum) values and their indexes in the data
管道和xargs
Pipes and xargs
Ali has another "against the sky" container framework! This kubernetes advanced manual is too complete
[leetcode refers to offer 18. Delete the node of the linked list (simple)]
[leetcode refers to offer 25. Merge two sorted linked lists (simple)]
Detectron2 usage model
airbase 初步分析
C# ftpHelper
2. Finishing huazi Mianjing -- 2
Pycharm Chinese plug-in
如何发挥测试策略的指导性作用
Is rust more suitable for less experienced programmers?
阿里云回应用户注册信息泄露事件
ROS学习笔记-----ROS的使用教程
JS prototype and prototype chain
Based on jsplumb JS to achieve multi list one to many connection effect