✨✨✨ [C language] take you to brush questions in the shortest time (with problem-solving ideas and specific codes) and constantly update them ✨✨✨

Keywords: C C++ Algorithm visualstudio

🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷

🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷🐷

💚 BC41. Are you a genius

Description:

It is said that people with an IQ above 140 are called geniuses, KiKi If you want to know if he is a genius, please help him judge by programming. Enter an integer to indicate a person's IQ. If it is greater than or equal to 140, it indicates that he is a genius and outputs“ Genius"

Enter Description:

Multiple sets of inputs, each line of input includes an IQ represented by an integer

Output Description:

For each line of input, output“ Genius"

Example:

Input:
160

Output:
Genius

code:

//Direct! = EOF
#include <stdio.h>
int main()
{
	int a = 0;
	while ((scanf("%d", &a)) != EOF)
	{
		if (a >= 140)
		{
			printf("Genius\n");
		}
	}
	return 0;
}

//Bitwise inversion
#include <stdio.h>
int main()
{
	int a = 0;
	//~Bitwise negation of operators in C language
	while (~scanf("%d", &a))
	{
		if (a >= 140)
		{
			printf("Genius\n");
		}
	}
	return 0;
}

result:

Extension:

  • Multi group input: be sure to deal with the input of many groups of data, and then consider how to end the loop
  • There are two ways to realize multi group input

🧡 BC42. Perfect results

Description:

KiKi If you want to know whether his test score is perfect, please help him judge. Input an integer from the keyboard to judge whether the score is 90~100 Between, if yes, output“ Perfect"

Enter Description:

Multiple sets of inputs, each line of input includes an integer representing the score (90~100)

Output Description:

For each line of input, output“ Perfect"

Example:

Input:
98

Output:
Perfect

code:

//Direct! = EOF
#include <stdio.h>
int main()
{
	int score = 0;
	while ((scanf("%d", &score)) != EOF)
	{
		if (score >= 90 && score <= 100)
		{
			printf("Perfect\n");
		}
	}
	return 0;
}

Bitwise inversion
#include <stdio.h>
int main()
{
	int score = 0;
	while (~scanf("%d", &score))
	{
		if (score >= 90 && score <= 100)
		{
			printf("Perfect\n");
		}
	}

	return 0;
}

result:

Extension:

  • Multi group input
  • Don't write 90 < = score < = 100 in C language, there will be bug s

💛 BC43. Passing score

Description:

KiKi If you want to know whether his test score is passed, please help him judge. Input a score represented by an integer arbitrarily from the keyboard and judge whether the score is within the pass range by programming. If he passes, that is, the score is greater than or equal to 60, it is output“ Pass",Otherwise, output“ Fail"

Enter Description:

Multiple sets of inputs, each line of input includes a fraction (0) represented by an integer~100)

Output Description:

For each line of input, output“ Pass"Or“ Fail"

Example:

Input:
94

Output:
Pass

Input:
44

Output:
Fail

code:

#include <stdio.h>
int main()
{
	int score = 0;
	while (scanf("%d", &score) != EOF)
	{
		if (score >= 0 && score < 60)
		{
			printf("Fail\n");
		}
		else
		{
			printf("Pass\n");
		}
	}

	return 0;
}

result:

💙 BC44. Judge the parity of integers

Description:

KiKi If you want to know the parity of an integer, please help him judge. Input an integer (range) arbitrarily from the keyboard-2^31~2^31-1),Program to judge its parity

Enter Description:

Multiple sets of inputs, each line of input includes an integer

Output Description:

For each line of input, the output is odd( Odd)Or even( Even)

Example:

Input:
4
7

Output:
Even
Odd

code:

#include <stdio.h>
int main()
{
	int a = 0;
	while (~scanf("%d", &a))
	{
		if (a % 2 == 0)
		{
			printf("Even\n");
		}
		else
		{
			printf("Odd\n");
		}
	}

	return 0;
}

result:

💜 BC45, highest score

Description:

KiKi After taking the Chinese, math and foreign language exams, please help him judge the highest score in the three subjects. Enter the scores represented by three integers arbitrarily from the keyboard and judge the highest score by programming

Enter Description:

Multiple sets of inputs, each line of input includes the fraction (0) represented by three integers~100),Separated by spaces

Output Description:

For each line of input, the output is one line, that is, the highest score of the three scores

Example:

Input:
94 98 99
100 88 60

Output:
99
100

code:

//Create 3 variables
#include <stdio.h>
int main()
{
	int chinese = 0;
	int math = 0;
	int english = 0;
	while (~scanf("%d%d%d", &chinese, &math, &english))
	{
		int max = chinese > math ? chinese : math;
		max = max > english ? max : english;
		printf("%d\n", max);
	}

	return 0;
}

//Create an array and traverse the array
#include <stdio.h>
int main()
{
	int score[3] = { 0 };
	while (~scanf("%d%d%d", &score[0], &score[1], &score[2]))
	{
		int i = 0;
		int max = 0;
		for (i = 0; i < 3; i++)
		{
			if (max <= score[i])
			{
				max = score[i];
			}
		}
		printf("%d\n", max);
	}

	return 0;
}

result:

Extension:

  • Multiple sets of input (multiple data input in one line)
  • Suppose you create variables in an array, and the initial value of max is 0 (the score is not negative)

🤎 BC46. Judging vowels or consonants

Description:

KiKi Start learning English letters, BoBo The teacher told him that there were five letters A(a), E(e), I(i), O(o),U(u)It is called a vowel, and all other letters are called consonants. Please help him write a program to judge whether the input letter is a vowel( Vowel)Or consonants( Consonant)

Enter Description:

Multiple sets of input, one letter per line

Output Description:

For each group of inputs, the output is one line. If the input letter is vowel (including case), the output is“ Vowel",If the input letter is a non vowel, the output“ Consonant"

Example:

Input:
A
b

Output:
Vowel
Consonant

code:

//Method 1
#include <stdio.h>
int main()
{
	char ch = 0;
	//An array of vowels
	char vowel[] = "AaEeIiOoUu";
	while ((ch = getchar()) != EOF)
	{
		getchar();//Process \ n (wrap)
		//judge
		int i = 0;
		for (i = 0; i < 10; i++)
		{
			if (ch == vowel[i])
			{
				printf("Vowel\n");
				break;//Jump out of for loop
			}
		}
		//Or it's already vowels jumping here
		//Either traverse the entire array, where i = 10, and print consonants
		if (i == 10)
		{
			printf("Consonant\n");
		}
	}
	return 0;
}

//Method 2
//Use the library function strchr (find characters in a string)
//If an address appears, it is returned in the string
//Returns null (null pointer) if no error occurs
#include <stdio.h>
#include <string.h>
int main()
{
	char ch = 0;
	//An array of vowels
	char vowel[] = "AaEeIiOoUu";
	while ((ch = getchar()) != EOF)
	{
		getchar();//Process \ n (wrap)
		//judge
		if (strchr(vowel, ch))
		{
			printf("vowel\n");
		}
		else
		{
			printf("Consonant\n");
		}
	}
	return 0;
}

//Method 3
#include <stdio.h>
#include <string.h>
int main()
{
	char ch = 0;
	//An array of vowels
	char vowel[] = "AaEeIiOoUu";
	//Adding a space before% c will digest all previous white space characters and read the last character
	while (scanf(" %c",&ch) != EOF)
	{
		//judge
		if (strchr(vowel, ch))
		{
			printf("vowel\n");
		}
		else
		{
			printf("Consonant\n");
		}
	}
	return 0;
}

result:

Extension:

  • strchr function to find characters in a string. If found, it returns the address in the string. If not found, it returns null (null pointer).
  • About how to handle (line feed) when reading characters:
  1. Read with getchar() \ n
  2. When using scanf, adding a space before% c will digest all the previous white space characters and read one character, scanf ('% c', &ch)

🖤 BC47. Judge whether it is a letter

Description:

KiKi If you want to judge whether the input character is a letter, please program it for him

Enter Description:

Multiple sets of input, one character per line

Output Description:

For each group of input, the output occupies a separate line to judge whether the input character is a letter. See the output example for the output content

Example:

Input:
A
6

Output:
A is an alphabet.
6 is not an alphabet.

code:

//Method 1
#include <stdio.h>
int main()
{
	int ch = 0;
	while (scanf(" %c", &ch) != EOF)
	{
		if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
		{
			printf("%c is an alphabet.\n",ch);
		}
		else
		{
			printf("%c is not an alphabet.\n",ch);
		}
	}

	return 0;
}

//Method 2
#include <stdio.h>
#include <ctype.h>
int main()
{
	int ch = 0;
	while (scanf(" %c", &ch) != EOF)
	{
		if (isalpha(ch))
		{
			printf("%c is an alphabet.\n",ch);
		}
		else
		{
			printf("%c is not an alphabet.\n",ch);
		}
	}

	return 0;
}

result:

Extension:

The isalpha function determines whether a character is a letter. If it is a letter, it returns a value other than 0. If it is not a letter, it returns 0.

💚 BC48. Letter case conversion

Description:

KiKi To complete the case conversion of letters, if there is a character, judge whether it is an uppercase letter. If so, convert it to lowercase letters; otherwise, convert it to uppercase letters

Enter Description:

Multiple sets of input, one letter per line

Output Description:

For each group of inputs, the output occupies a separate line and outputs the corresponding form of letters

Example:

Input:
a
A
Z

Output:
A
a
z

code:

//Method 1
#include <stdio.h>
int main()
{
	int ch = 0;
	while (scanf(" %c", &ch) != EOF)
	{
		if (ch >= 'A' && ch <= 'Z')
		{
			ch = ch + 32;
			printf("%c\n", ch);
		}
		else if(ch>='a'&&ch<='z')
		{
			ch = ch - 32;
			printf("%c\n", ch);
		}
	}

	return 0;
}

//Method 2: using library function
#include <stdio.h>
#include <ctype.h>
int main()
{
	int ch = 0;
	while (scanf(" %c", &ch) != EOF)
	{
		if (isupper(ch))//Determine whether it is a capital letter
		{
			printf("%c\n", tolower(ch));//Capital to lowercase
		}
		else if(islower(ch))//Determine whether it is lowercase
		{
			printf("%c\n", toupper(ch));//Lowercase to uppercase
		}
	}

	return 0;
}

result:

Extension:

isupper determines whether it is a capital letter

Tower uppercase to lowercase

islower determines whether it is a lowercase letter

toupper converts lowercase letters to uppercase letters

🧡 BC50. Calculation unit step function

Description:

KiKi Recently, I learned the signal and system course. In this course, there is a very interesting function, unit step function, one of which is defined as:



Now try to find the unit impulse function in time domain t Value on

θ ( t ) = { 1 t > 0 1 / 2 t = 0 0 t < 0 \theta(t)=\begin{cases} 1 & t>0\\ 1/2 & t = 0 \\ 0 & t<0 \end{cases} θ(t)=⎩⎪⎨⎪⎧​11/20​t>0t=0t<0​

Enter Description:

The title has multiple groups of input data, one for each line t(-1000<t<1000)Represents the time domain of the function t

Output Description:

Output the value of the function and wrap

Example:

Input:
11
0
-11

Output:
1
0.5
0

code:

#include <stdio.h>
int main()
{
	int t = 0;
	while (~scanf("%d", &t)!=EOF)
	{
		if (t > 0)
		{
			printf("1\n");
		}
		else if (t < 0)
		{
			printf("0\n");
		}
		else
		{
			printf("0.5\n");
		}
	}

	return 0;
}

result:

💛 BC51. Triangle judgment

Description:

KiKi Want to know the three sides that have been given a,b,c Whether it can form a triangle. If it can form a triangle, judge the type of triangle (equilateral triangle, isosceles triangle or ordinary triangle)

Enter Description:

There are multiple groups of input data for the title, and three data are input in each line a,b,c(0<a,b,c<1000),The three sides of the triangle are separated by spaces

Output Description:

For each group of input data, the output occupies one line. If it can form a triangle and an equilateral triangle, it will be output“ Equilateral triangle!",Isosceles triangle is output“ Isosceles triangle!",The remaining triangles are output“ Ordinary triangle!",Conversely output“ Not a triangle!"

Example:

Input:
2 3 2
3 3 3

Output:
Isosceles triangle!
Equilateral triangle!

code:

#include <stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	while (~scanf("%d%d%d", &a, &b, &c))
	{
		if ((a + b > c) && (a + c > b) && (a + c) > b)
		{
			if (a == b && b == c)
			{
				printf("Equilateral triangle!\n");
			}
			else if (((a == b) && a != c) || ((b == c) && b != a) || ((a == c) && a != b))
			{
				printf("Isosceles triangle!\n");
			}
			else
			{
				printf("Ordinary triangle!\n");
			}
		}

		else
		{
			printf("Not a triangle!\n");
		}
	}
	return 0;
}

result:

Don't forget before you leave 👍 follow 💡 Collection 💖
I hope this article can help you ~!


🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 Share past articles 🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀

[C language] take you to brush questions in the shortest time (with problem-solving ideas and specific codes) and constantly update (I)

[C language] take you to brush questions in the shortest time (with problem-solving ideas and specific codes) and constantly update (II)

[C language] take you to brush questions in the shortest time (with problem-solving ideas and specific codes) and constantly update (III)

[C language] take you to brush questions in the shortest time (with problem-solving ideas and specific codes) and constantly update (IV)

Posted by Garcia on Mon, 20 Sep 2021 21:16:22 -0700