当前位置:网站首页>Branch and loop statements (including goto statements) -part2
Branch and loop statements (including goto statements) -part2
2022-06-23 01:42:00 【'Dream_】
If you will It's better to start than to give up . Roman.
May we all have our own goals and are making unremitting efforts for them .
--------------------------------------------------------------------------- Exercises
1. Calculation n The factorial .2. Calculation 1!+2!+3!+……+10!3. Find a specific number in an ordered array n.( Explain two-point search )4. Write code , Demonstrate the movement of multiple characters from both ends , Converging in the middle .5. Write code to achieve , Simulate user login scenarios , And can only log in three times .( Only three passwords are allowed , If the password is correct, you will be prompted to log in as , If all three inputs are wrong , Then exit the program .
//1. Calculation n The factorial .
#include<stdio.h>
int main()
{
int n = 0;
printf(" Please enter any one n:\n");
scanf("%d", &n);
if (n >= 0)
{
printf("%d The factorial result of is :\n", n);
int i = 0;
int ret = 1;
for (i = 1; i <= n; i++)
{
ret *= i;
}
printf("%d\n", ret);
}
return 0;
}//2. Calculation 1!+ 2!+ 3!+ …… + 10!
#include<stdio.h>
int main()
{
int i = 0;
int sum = 0;
// Method 1 : Two layers of circulation
/*for (i = 1; i <= 10; i++)
{
int j = 0;
int ret = 1;
for (j = 1; j <= i; j++)
{
ret *= j;
}
sum += ret;
}*/
// Method 2 : Monolayer cycle
int ret = 1;// Keep it outside
for (i = 1; i <= 10; i++)
{
ret *= i;
sum += ret;
}
printf("1!+ 2!+ 3!+ …… + 10! The result is :\n");
printf("%d\n", sum);
return 0;
}//3. Find a specific number in an ordered array n.( Explain two-point search )
#include<stdio.h>
#include<string.h>
int main()
{
//char ch1[] = "0,1,2,3,4,5,6,7,8,9";// But notice that there is another one behind this kind of writing \0 character !!
//printf("ch=%d ch1=%d", sizeof(ch), sizeof(ch1));
//sizeof() Is to find the length of the string , Unit is byte !
// Note that the stem condition is an ordered array
int ch[10] = { 0,1,2,3,4,5,6,7,8,9 };
int left = 0;// Left subscript
int right = (sizeof(ch) / sizeof(ch[0]))-1;// Right subscript : Method 1 :{ } Initialize the array with sizeof
//int right=strlen(ch)-1;// Right subscript : Method 2 : Using library functions , This method is applicable to " " Initialize array
//printf("right=%d\n", right);
int n = 0;
printf(" Please enter the number you want to find :\n");
scanf("%d", &n);
while (left <= right)
{
//int mid = (left + right) / 2;// In the middle : Method 1
int mid=left+((right-left)/2);// In the middle : Method 2 : Optimize
if (ch[mid] < n)
{
left++;
continue;
}
else if (ch[mid] > n)
{
right--;
continue;
}
else
{
printf(" eureka !\n");
break;
}
}
if (left > right)
{
printf(" Can't find !\n");
}
return 0;
}//4. Write code , Demonstrate the movement of multiple characters from both ends , Converging in the middle .
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
int main()
{
char ch[] = "hello welcome to my world!";
char ch1[] = "**************************";
int left = 0;
int right = strlen(ch) - 1;
while (left <= right)
{
ch1[left] = ch[left];
ch1[right] = ch[right];
Sleep(1000);// Delayed printing , In milliseconds , Need header file :<windows.h>
// The goal is : Put all the processes on the same line , So the introduction of system("cls"), Need header file <stdlib.h>
system("cls");
printf("%s\n", ch1);
left++;
right--;
}
// If there is no printing at last , Add a print here , as follows
//printf("%s\n",ch);
return 0;
}
//5. Write code to achieve , Simulate user login scenarios , And can only log in three times .
//( Only three passwords are allowed , If the password is correct, the login is successful , If all three inputs are wrong , Then exit the program .
#include<stdio.h>
#include<string.h>
int main()
{
char cor[] = "Xianmeng 123";// Correct password setting
char password[20] = { 0 };
int i = 0;
for (i = 0; i < 3; i++)
{
printf(" Please enter your password :\n");
// error !
/*while (getchar() != '\n')
{
password[20] = (char) getchar();
}*/
// correct : Input string with space
// Method 1 :
//scanf("%[^\n]]", password);
// Method 2 :
gets(password);
if (strcmp(password, cor) == 0)
{
printf(" Landing successful !\n");
break;
}
else
{
if (i < 2)
{
printf(" Input error , Please re-enter :\n");
}
else
{
printf(" Three input errors , Exit procedure !\n");
}
}
}
return 0;
}---------------------------------------------------------- Be careful :
- scanf("%s") Only read the string before the space , however printf("%s") Can print completely
- Delay print function : Sleep( Numbers ); Company : millisecond , The header file is included in <windows.h> in
- Functions that print centrally on one line :system("cls"); The header file is included in <stdlib.h> in
- The comparison of two strings cannot be ==, You should use library functions strcmp( str1 , str2 ) ( You need to use a header file #include<string.h>) str1<str2 Output <0 str1==str2 Output ==0 str1>str2 Output >0
- Notice two ways to enter a string with spaces :scanf("%[^\n]], Array name ) perhaps gets( Array name )
------------------ All the anger of a person comes from the pain of his own incompetence .-------------------
************************ Shut down the program and guess the number of games see the next blog
边栏推荐
- Vector 3 (static member)
- 总体标准差和样本标准差
- [hdu] p1466 calculate the number of intersections of straight lines
- Cmake passing related macros to source code
- Wechat mobile terminal development - account login authorization
- Do you know the memory components of MySQL InnoDB?
- Day367: valid complete square
- JS prototype and prototype chain Paramecium can understand
- C language student achievement ranking system
- Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
猜你喜欢

E-R图

SFOD:无源域适配升级优化,让检测模型更容易适应新数据

Development status of full color LED display

JMeter associated login 302 type interface

Module 8 job

Component development

SQL programming task03 job - more complex query

Installing MySQL for Linux

SQL programming task05 job -sql advanced processing

Express framework installation and start service
随机推荐
Installing MySQL for Linux
9. class and object practice and initialization list
魔王冷饭||#099 魔王说西游;老板的本质;再答中年危机;专业选择
Template specialization template <>
[template] KMP
Use of higher order functions
[hdu] p2087 cut cloth strip
[learning notes] roll back Mo team
Vector 3 (static member)
3D printing microstructure
Day500: keyboard line
Steps to implement a container global component
LeetCode 206. 反转链表(迭代+递归)
Google benchmark user manual and examples
4. functions and inline functions with default values for formal parameters
6. const usage, combination of first and second level pointers
Pat class A - 1015 reversible primes
Philosopher's walk gym divide and conquer + fractal
HDU - 7072 double ended queue + opposite top
MySQL -- how to access the database of a computer in the same LAN (prenatal education level teaching)