当前位置:网站首页>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 .
-- ------------------------------------------------------------------------- answer
//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

原网站

版权声明
本文为['Dream_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514206901.html