[Take you through the questions by hand]-Introduction to C language programming (2)

Hello everyone, I am deep fish~

Table of contents

Foreword:

1. Judging letters

2. Character Christmas tree

3. ASCII code

4. Date of birth input and output

 5.2 Calculation of the nth power

 6. Follow format input and exchange output

7. Character to ASCII code

 8. Calculate the value of the expression

9. Calculate division with remainder

 10. Calculate body mass index

Conclusion: There is still glory in the other side, and young people are not afraid of the long years


Foreword:

This part is the initial part of the C language topic, a total of 130 questions, from easy to difficult, suitable for beginners to learn, non-beginners do not read the
topic link: programming language beginners training camp Guest Network (nowcoder.com)

 

1. Judging letters

Topic Link: Judging Letters

topic:

 Solution 1 : Note here:

<1> When using ch=getchar, be sure to use getchar() to process\n

<2> ch should be defined as int type , because the return value of getchar is int type, not char type

#include <stdio.h>

int main()
{
	int ch = 0;
    //多组输入
	while ((ch = getchar()) != EOF)
	{

		getchar();//处理\n
         //判断并输出
		if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
		  printf("YES\n");
	    else
		   printf("NO\n");
	}
	return 0;
}

Solution 2 : The isalpha function is used here . When the character is a letter, it returns a value other than 0. When it is not a letter, it returns 0.

#include <stdio.h>
#include<ctype.h>   //isalpha函数的头文件
int main()
{
	int ch = 0;
	while ((ch = getchar()) != EOF)
	{
		getchar();
		if (isalpha(ch))
		  printf("YES\n");
	    else
		   printf("NO\n");
	}
	return 0;
}

2. Character Christmas tree

Topic Link: Character Christmas Tree

Subject :

 Solution : This question mainly examines loop nesting, the outer loop controls the number of lines, the inner loop controls the printing of characters and spaces, and finally finds the relationship between the inner loop variable and the outer loop variable to get the answer (note the outer loop Every time it is performed, a line break is required )

Question: Why is int j=0 not written at the top of the code

Answer: The C language stipulates that variables are defined at the top of the current code block . The scope of the variable j is within the second brace, so it is fine to define it directly in front of the second brace. If it is written in the second brace Outside the brackets, j will be accumulated every cycle, defined inside the second curly braces can make j return to 0 every cycle

#include <stdio.h>

int main()
 {
    //输入一个字符
    char ch=0;
    ch=getchar();
    //输出圣诞树
    int i=0;
    //每循环一次,打印一行
    //每一行有2部分组成,空格和字符
    for(i=0;i<5;i++)
    {
       int j=0;
       //空格
       for(j=0;j<4-i;j++)
        {
              printf(" ");     //4 3 2 1 0 
        }
        //字符
        for(j=0;j<=i;j++)
        {
             printf("%c ",ch);  //1 2 3 4 5 
        }

        printf("\n");
    }
    return 0;
}

3. ASCII code

Topic link: ASCII code

topic:

 Solution 1 : For this question, you need to learn how to calculate the number of array elements sz=sizeof (arr)/sizeof (arr[0])  (divide the size of the entire array by the size of the first element of the array)

#include <stdio.h>

int main()
 {
    //这里定义要用char类型,因为打印的是字符
    char arr[] = { 73,32,99,97,110,32,100,111,32,105,116,33 };
    //arr是数组,数组是用下标来访问的
    int i=0;
    //计算数组元素的个数
    int sz=sizeof(arr)/sizeof(arr[0]);
    for ( i = 0; i < sz; i++)
    {
        printf("%c", arr[i]);
    }
    return 0;
}

Solution 2 : Print directly as a string, but it should be noted that 0 or '\0' must be added at the end of the array , otherwise there will be garbled characters behind, and the following result will appear ( the end mark of the string: \0 )

int main()
{
	char ch[] = { 73, 32, 99, 97, 110, 32, 100, 111, 32, 105, 116 , 33 ,'\0'};
	printf("%s", ch);
	return 0;
}

4. Date of birth input and output

Topic link: date of birth input and output

topic:

Solution: For this question, you need to learn how to input %md according to the format, which is the input data with m field width , and %0md is the output data with m field width, and automatically fill in the front with 0 (if the data does not reach the field width length)

#include <stdio.h>

int main() //简化版
{
	int year=0;
	int month=0;//变量尽量初始化
	int date = 0;
	//按格式输入
	scanf("%4d%2d%2d", &year, &month, &date);
	//输出
	printf("year=%4d\n", year);
	printf("month=%02d\n", month);
	printf("date=%02d\n", date);
	return 0;
}

 5.2 Calculation of the nth power

Topic link: Calculation of 2 to the nth power

topic:

Solution: For this question, you need to learn how to use the shift operator (the shift is a binary bit, such as 1 in binary is 01, 1<<1 [shift one bit to the left as a whole, and then automatically fill in 0 on the right] binary is 10, decimal That is 2)

Conclusion: Shifting one bit to the left is equivalent to *2, and shifting one bit to the right is equivalent to /2

There is also how to shape multiple sets of input : while((scanf("%d,&a"))!=EOF)

The return value of scanf is the number of input variables, which is also an integer

#include <stdio.h>

int main()
{
	int n = 0;
	while((scanf("%d", &n))!=EOF)
	{
      printf("%d\n",1<<n);
	}
	
	return 0;
}

 6. Follow format input and exchange output

Topic link: input and exchange output according to format

topic:

 Solution 1 : Idea: To exchange a bottle of soy sauce and a bottle of vinegar for a bottle, you first need a third bottle, put the soy sauce into the third bottle, then pour the vinegar into the second bottle, and then pour the soy sauce into the third bottle A bottle, this completes the exchange

#include <stdio.h>

int main()
 {
    int a,b,flag=0;
    scanf("a=%d,b=%d",&a,&b);
    //交换
    flag=a;
    a=b;
    b=flag;
    //输出
    printf("a=%d,b=%d\n",a,b);
    
    return 0;
}

Add a written test question: How to realize the exchange of two numbers without creating a third variable?

Solution 2 : Addition and subtraction : This method has a disadvantage, if a, b is too large, it will overflow

#include <stdio.h>

int main()
 {
    int a,b,flag=0;
    scanf("a=%d,b=%d",&a,&b);
    //交换
     a=a+b;
     b=a-b;
     a=a-b;
    //输出
    printf("a=%d,b=%d\n",a,b);
    
    return 0;
}

Solution 3 : XOR method : This method is recommended

^Exclusive OR operator: the operand must be an integer (int), the effect is a binary bit, the same position is 0, and the difference is 1

For example: the binary digit of 3 is 011, the binary digit of 5 is 101, then the result of XOR of 3 and 5 is 110, which corresponds to 6 in decimal, then the result of XOR of 3 and 5 is 6

#include <stdio.h>

int main()
 {
    int a,b,flag=0;
    scanf("a=%d,b=%d",&a,&b);
    //交换
    a=a^b;
    b=a^b;
    a=a^b;
    //输出
    printf("a=%d,b=%d\n",a,b);
    
    return 0;
}

7. Character to ASCII code

Topic link: Character to ASCII code

topic:

Solution : This question is relatively simple, but you can remember that the ASCII value of a is 97, and that of A is 65

#include <stdio.h>

int main() 
{
	char ch = 0;
	scanf("%c", &ch);
	//也可以这样写
	//ch=getchar();
	printf("%d", ch);

    return 0;
}

 8. Calculate the value of the expression

Topic link: Calculate the value of an expression

topic:

Solution: This question is very simple, but pay attention to the multiplication sign in C language is *

#include<stdio.h>

int  main()
{
    int a=40;
    int c=212;
    printf("%d\n",(-8+22)*a-10+c/2);
    return 0;
}

9. Calculate division with remainder

Topic link: Calculation with remainder division

topic:

Solution : This question can be made by knowing the / and % operator

#include <stdio.h>

int main() 
{
    int a=0;
    int b=0;
    scanf("%d %d",&a,&b);
    // / 除法操作符,得到的是商
    // % 取余(取模)操作符,得到的是余数
    printf("%d %d",a/b,a%b);
    return 0;
}

 10. Calculate body mass index

Topic Link: Calculating Body Mass Index

topic:

 Solution to the question: Pay attention to two points in this question: <1> The unit of height is cm

<2> high cannot be directly divided by 100 , so that the obtained integer is not a decimal, which will be very different from the correct result

#include <stdio.h>

int main() 
{
    //输入
    int weight = 0;
    int high = 0;
    double BMI = 0;
    scanf("%d %d", &weight, &high);
    //计算BMI
    BMI = (weight / ((high / 100.0) * (high / 100.0)));
    //输出
    printf("%.2lf\n", BMI);
}

Conclusion: There is still glory in the other side, and young people are not afraid of the long years

Please, let's have a three-in-a-row. 

 

Guess you like

Origin blog.csdn.net/qq_73017178/article/details/131674913