当前位置:网站首页>MOOC Weng Kai C language week 5: 1. cycle control 2. multiple cycles 3. cycle application
MOOC Weng Kai C language week 5: 1. cycle control 2. multiple cycles 3. cycle application
2022-07-28 07:03:00 【W.934】
One 、 Cycle control
prime number : Can only be 1 And the number divided by itself , barring 1.(2、3、5、7、11、13、17、19...)
Write a program : Read in a number entered by the user x, Then judge whether this number is a prime number .
Ideas : take except 1 And numbers other than it Except it , If it can be counted by those to be divisible by , Then he's not prime , conversely , Then prime . Because it is increasing , So use for loop (for Cycle fit : The number is definite or the range is definite ).
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
int i;
int isPrime=1;//x Prime number ?
for(i=2;i<x;i++)
{
if(x%i==0)
{
isPrime==0;
break;
}
}
if(isPrime==1)
{
printf(" Prime number \n");
}
else
{
printf(" Not primes \n");
}
return 0;
}break vs continue
break: Out of the loop
continue: Skip this round of the loop and the rest of the statements go to the next round .
Two 、 Multiple cycles
1. Nested loops : Inside the loop is still a loop .
How to write program output 100 Prime number within ?
Ideas : You need to construct a loop , Loop can make x from 2 Go to the 100. Use the method of the previous question , Let it enter the circulation body , To determine whether it is a prime number , Then the output . In the circulatory system , If it's a prime , Then the prime number is output , If not, you don't need to output this number .
#include<stdio.h>
int main()
{
int x;
for(x=2;x<100;x++)
{
int i;
int isPrime=1;//x Prime number ?
for(i=2;i<x;i++)
{
if(x%i==0)
{
isPrime==0;
break;
}
}
if(isPrime==1)
{
printf("%d",x);
}
}
printf("\n");
return 0;
}Nested loops : Inside the loop is still a loop .
Before output 50 Prime number ?
Ideas : Can not use for loop ( Because there is no exact range and number of cycles ).
You need a counter :int cnt=0; Find one prime at a time , The counter needs to be incremented once :cnt++; .
Then the cycle condition becomes : As long as the counter is less than 50, Then continue to cycle :while(cnt<50) { } ( Is equivalent to for Cycle to while loop , Change a condition )x The initial value should be 2, And there are x++; .
use for The cycle can also :for(x=2;cnt<50;x++) { }
#include<stdio.h>
int main()
{
int x;
x=2;
int cnt=0;
//for(x=2;x<100;x++)
while(cnt<50)
{
int i;
int isPrime=1;//x Prime number ?
for(i=2;i<x;i++)
{
if(x%i==0)
{
isPrime==0;
break;
}
}
if(isPrime==1)
{
printf("%d",x);
cnt ++;
}
x++;
}
printf("\n");
return 0;
}use for The cycle can also :for(x=2;cnt<50;x++) { }
#include<stdio.h>
int main()
{
int x;
x=2;
int cnt=0;
//for(x=2;x<100;x++)
//while(cnt<50)
for(x=2;x<100;x++)
{
int i;
int isPrime=1;//x Prime number ?
for(i=2;i<x;i++)
{
if(x%i==0)
{
isPrime==0;
break;
}
}
if(isPrime==1)
{
printf("%d",x);
cnt ++;
}
//x++;
}
printf("\n");
return 0;
}2. Jump out of a nested loop :break You can only do it for the layer of the loop where it is .
Collect coins :
How to use 1 horn 、2 Angular sum 5 A dime makes up 10 The amount below yuan ?
The procedure is as follows :
#include<stdio.h>
int main()
{
int x;
int one,two,five;
//scanf("%d",&x);
x=2;
for(one=1;one<x*10;one++){
for(two=1;two<x*10/2;two++){
for(five=1;five<x*10/5;five++){
if(one+two*2+five*5==x*10){
printf(" It can be used %d individual 1 Angular plus % individual 2 Corner plus 5 Jiao gets yuan \n",one,two,five,x);
}//if
}//five
}//two
}//one
return 0;
}If you want to find the first group that can make up the amount, quit
The idea of the procedure is as follows :
Wrong situation ①: You can't just add one after a loop statement break, because break You can only do it for the layer of the loop where it is .
Wrong situation ②: You can't simply put break, Then no matter what the situation is, leave this for loop , So this break Always jump out of the floor for loop . Should be , Under certain conditions , When this break occurs , Below break Will happen , Below break Will happen .
So how to do , Under certain conditions , Complete this series of things ?( use if).
You need to use a variable : ①int exit=0; ② At the first break Before you do it :break=1; ③ second 、 Three break The conditions are :if(exit==1)break;( namely : If exit==1 Words , then break).
This method is : The relay break(①: You need a variable to express this thing .②: When we want to break When , Let this variable equal 1.③: Judge whether the variable is equal to 1, To relay break.④: Relay again break, Make it jump out of the loop .)
break and continue: Can only be done on the layer where it is .
Wrong situation ①: You can't just add one after a loop statement break, because break You can only do it for the layer of the loop where it is .
#include<stdio.h>
int main()
{
int x;
int one,two,five;
//scanf("%d",&x);
x=2;
for(one=1;one<x*10;one++){
for(two=1;two<x*10/2;two++){
for(five=1;five<x*10/5;five++){
if(one+two*2+five*5==x*10){
printf(" It can be used %d individual 1 Angular plus % individual 2 Corner plus 5 Jiao gets yuan \n",one,two,five,x);
break;//( You can't just add one here break, because break You can only do it for the layer of the loop where it is .)
}//if
}//five
}//two
}//one
return 0;
}Wrong situation ②: You can't simply put break, Then no matter what the situation is, leave this for loop , So this break Always jump out of the floor for loop . Should be , Under certain conditions , When this break occurs , Below break Will happen , Below break Will happen .
#include<stdio.h>
int main()
{
int x;
int one,two,five;
int exit=0;//*
//scanf("%d",&x);
x=2;
for(one=1;one<x*10;one++){
for(two=1;two<x*10/2;two++){
for(five=1;five<x*10/5;five++){
if(one+two*2+five*5==x*10){
printf(" It can be used %d individual 1 Angular plus % individual 2 Corner plus 5 Jiao gets yuan \n",one,two,five,x);
break;//( You can't simply add break;)
}//if
}//five
break;//( You can't simply add break;)
}//two
break;//( You can't simply add break;)
}//one
return 0;
}The right way :
So how to do , Under certain conditions , Complete this series of things ?( use if).
You need to use a variable : ①int exit=0; ② At the first break Before you do it :break=1; ③ second 、 Three break The conditions are :if(exit==1)break;( namely : If exit==1 Words , then break).
This method is : The relay break(①: You need a variable to express this thing .②: When we want to break When , Let this variable equal 1.③: Judge whether the variable is equal to 1, To relay break.④: Relay again break, Make it jump out of the loop .)
#include<stdio.h>
int main()
{
int x;
int one,two,five;
int exit=0;//*
//scanf("%d",&x);
x=2;
for(one=1;one<x*10;one++){
for(two=1;two<x*10/2;two++){
for(five=1;five<x*10/5;five++){
if(one+two*2+five*5==x*10){
printf(" It can be used %d individual 1 Angular plus % individual 2 Corner plus 5 Jiao gets yuan \n",one,two,five,x);
//( You can't just add one here break, because break You can only do it for the layer of the loop where it is .)
//( You can't simply add break;)
exit=1;//*
break;
}//if
}//five
//( You can't simply add break;)
if(exit==1)break;//*
}//two
//( You can't simply add break;)
if(exit==1)break;//*
}//one
return 0;
}Method 2:
Of course , You can also use goto The way
goto out; out:
Where you need to leave the loop , Put in goto sentence .、
goto A label is required after the statement , This label is written in the program itself ,( Such as goto out; out: )goto To jump to out Go to the place indicated .
In need of multi-layer circulation 、 Inner layer of nested loop , When you jump out , have access to goto.
#include<stdio.h>
int main()
{
int x;
int one,two,five;
scanf("%d",&x);
for(one=1;one<x*10;one++){
for(two=1;two<x*10/2;two++){
for(five=1;five<x*10/5;five++){
if(one+two*2+five*5==x*10){
printf(" It can be used %d individual 1 Angular plus % individual 2 Corner plus 5 Jiao gets yuan \n",one,two,five,x);
goto out;//*
}//if
}//five
}//two
}//one
out://*
return 0;
}3、 ... and 、 Recycling
1. Sum up
/1: seek f(n)=1+½+⅓+¼+...+1/n
Ideas : use for loop ( Because the starting point 1, End n, The scope is clear ) Note floating point numbers .
#include<stdio.h>
int main()
{
int n;
int i;
double sum=0.0;// 1 Divide i The value of has a decimal point
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum+=1.0/i;// 1 Divide i,i Certain ratio 1 Big , There is a floating point number on both sides of the division sign , Then the other will be floating point numbers , The result will be the operation result of floating-point numbers .
}
printf("f(%d)=%f\n",n,sum);
return 0;
}/2: seek f(n)=1-½+⅓-¼+...+1/n
Ideas : It's the same idea as the previous one , use for loop ( Because the starting point 1, End n, The scope is clear ) Note floating point numbers . Just one more sign change , You can set another variable , To make changes in symbols .
#include<stdio.h>
int main()
{
int n;
int i;
double sum=0.0;
int sign=1;// Method 2:double sign=1.0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum+=sign*1.0/i;// Method 2:sum+=sign/i;
sign=-sign;// Method 2:sign=-sign; It is equivalent to using molecules to change positive and negative .
}
printf("f(%d)=%f\n",n,sum);
return 0;
}2. Find the greatest common divisor
Enter two numbers a and b. Output their greatest common divisor : Input 12 18; Output :6.
Ideas :1. Enumeration ( The efficiency is not high ) 2. division
1) Enumeration
int a,b;
int min;
scanf("%d %d",&a,&b);
if(a<b)
{min=0;}
else
{min=b;}
int ret=0;
int i;
for(i=1;i<min;i++)
{if(a%i==0){
if(b%i==0){
ret=i;
}
}
}
printf("%d and %d The greatest common divisor of %d.\n",a,b,ret);2) division
1) If b be equal to 0, End of calculation ,a Is the greatest common divisor ;
2) otherwise , Calculation a Divide b The remainder of , Give Way a be equal to b, and b Equal to the remainder ;
3) Back to step one
If you ask 12 And 18 Maximum common divisor of : Input 12 18; Output :6.
a b t( remainder )
12 18 12
18 12 6
12 6 0
6 0
int a,b;
int t;
scanf("%d %d",&a,&b);
while(b!=0)
{
t=a%b;
a=b;
b=t;
}
printf("gcd=%d\n",a);3. Integer decomposition
Positive order factorization of integers
Enter a non negative integer , Positive sequence output every digit of it . Input :13425; Output :1 3 4 2 5.
int x;
scanf("%d",&x);
int mask=1;
int t=x;
while(t>9){
t/=10;
mask*=10;
}
printf("x=%d,mask=%d\n",x,mask);
do{
int d=x/mask;
printf("%d",d);
if(mask>9){
printf(" ");
}
x%=mask;
mask/=10;
}while(mask>0);
printf("\n");( This is a bit of a detour for me T^T)
边栏推荐
猜你喜欢

Results fill in the blank. How many days of national day are Sundays (solved by pure Excel)

Applets: lifecycle

Shell script - sort, uniq, TR, array sort, cut, Eval command configuration

Iptables firewall

DNS forward resolution experiment

JSON notes

bond模式配置

DNS域名解析服务

Servlet

Firewall - iptables firewall (four tables and five links, firewall configuration method, detailed explanation of matching rules)
随机推荐
ES6 add -- > object
Technology sharing | how to do Assertion Verification in interface automated testing?
Technology sharing | do you know the functions of the server interface automated testing and requests library?
JS four operations are repackaged to solve the problem of precision loss
Applets: lifecycle
Applets: WSX scripts
Wechat applet custom compilation mode
Hdu-1159-commonsubsequence (LCS longest common subsequence)
2022 Tanabata gift recommendation! Nice, cheap and practical gift recommendation
Result fill in the blank (dfs*c language)
Upload and download files from Ubuntu server
DHCP service
Applet navigator cannot jump (debug)
RAID disk array
Network - transport layer (detailed version)
三层交换和VRRP
MySQL master-slave
Method of designing test cases
Teach you three steps to complete the construction of the test monitoring system hand in hand
修复故障扇区