Explanation of C language examples (middle)

Which of the following is not a keyword:

A.int
B.struct
C.define
D.continueAnswer
analysis:
C language keywords: C language identifiers defined by C language with specific meanings and specially used for special purposes, also known as reserved words
define is not a keyword, It is implemented by the compiler and is used to define preprocessing instructions for macros, not in the C language.
int, struct, and continue are all keywords included in the C language
Therefore: choose C

BC113-Xiaole Leding Alarm Clock

BC113-Xiaole Leding Alarm Clock

#include <stdio.h>
int main()
{
    
    
    int h = 0;
    int m = 0;
    int k = 0;
    scanf("%d:%d %d", &h, &m, &k);
    h = ((m+k)/60+h)%24;
    m = (m+k)%60;
    printf("%02d:%02d\n", h, m);
    return 0;
}

BC16-character to ASCII code

BC16-character to ASCII code

#include <stdio.h>
int main()
{
    
    
    char ch = 0;
    scanf("%c", &ch);
    printf("%d\n",ch);
    return 0;
}

The result of executing the following code is:

A.1 2 3 4 5 6 7 8 9 10
B.5 5 5 5 5 5 5 5 5 5
C. Printing of infinite loop 5
D.0 1 2 3 4 5 6 7 8 9
Answer analysis:

The original idea of ​​the above code should be: loop 10 times, and print the result of i if i==5 in each loop.

However, the expression == in the if statement is written as an assignment, which is equivalent to setting the value of i to 5 as much as possible in each loop, and 5 is true, so 5 will be printed every time.

After i is modified to 5 and printed every time, the value of i will never be equal to 10, thus causing an infinite loop

Therefore: the print 5 of the infinite loop

Therefore: choose C

The correct statement about the if statement is

A.if statement can only be followed by one statement
B.in the if statement, 0 means false and 1 means true
C.if statement is a branch statement, which can realize single branch or multi-branch
D.else statement is always with it The aligned if statement matches
A: error, multiple statements can be followed after the if, and {} should be used to enclose multiple statements

B: false, 0 for false, non-zero for true

C: correct

D: Not necessarily, it depends on the specific code, if the code is not standardized, it may not be aligned

What is the result of executing the following code

#include <stdio.h>
int main() {
    
    
	int x = 3;
	int y = 3;
	switch (x % 2) {
    
    
	case 1:
		switch (y)
		{
    
    
		case 0:
			printf("first");
		case 1:
			printf("second");
			break;
		default: printf("hello");
		}
	case 2:
		printf("third");
	}
	return 0;
}

A.secondthird
B.hello
C.firstsecond
D.hellothirdAnswer
analysis:

The switch statement is a multi-branch selection statement. If the result of the expression in the switch hits the case, the sub-item of the case will be executed. If the sub-item of the case is not followed by a break statement, the execution will continue.

For the analysis of this question, please see the following notes:

#include <stdio.h>
  int main() {
    
    
  	int x = 3;
  	int y = 3;
  	switch (x % 2) {
    
      // x%2的结果为1,因此执行case1
  	case 1:
  		switch (y)   // y是3,因此会执行case3,而case3不存在,那只能执行default
  		{
    
    
  		case 0:
  			printf("first");
  		case 1:
  			printf("second");
  			break;
  		default: printf("hello"); // 打印hello,打印完之后,内部switch结束,此时外部case1结束
  		}             // 因为外部case1之后没有添加break语句,所以继续执行case2
  	case 2:             // 打印third
  		printf("third");      // 外部switch结束
  	}
  	return 0;
  }

That is: first print hello in the default position of the internal switch, and then print third in the external case2

Therefore: choose D

The correct statement about pointers is:

A. The size of sizeof(char*) must be 1
B. A pointer variable is a variable used to store addresses
C. The size of a pointer variable is 4 bytes
D. A pointer is not a variable
Answer analysis: This question mainly examines the relevant characteristics of pointers

A: Error, the pointer is a compound data type, and the content of the pointer variable is an address, so a pointer can represent the entire address collection of the system,

Therefore, when compiling the code according to 32-bit, the pointer occupies 4 bytes; when compiling the code according to 64-bit, the pointer occupies 8 bytes (note: not 64-bit system must occupy 8 bytes, the key is to compile according to 64-bit mode)

B: correct

C: error, refer to option A for explanation

D: Error, the description in this article is rather ambiguous. The pointer can be considered as a data type, or as a defined pointer variable.

Therefore, choose B

The incorrect statement about switch is:

A. The default clause in the switch statement can be placed anywhere
B. The expression after the case in the switch statement can only be an integer constant expression
C. The case clause in the switch statement must be before the default clause
D. The switch statement The case expression does not require order
Answer analysis:

A: Correct, it can be placed anywhere, but it is generally recommended to put it at the end

B: Correct, the constant expression or enumeration type of the shaping result is usually placed after the case statement, and the enumeration type can also be regarded as a special constant

C: Error, there is no requirement that the case must be before the default, and the general case is best placed before the default

D: Correct, but generally follow the order

Therefore: choose C

print multiples of 3

Write a code to print all numbers that are multiples of 3 between 1-100

/*
解题思路:
1. 3的倍数一定能够被3整除,因此i%3==0表达式成立时,则i一定是3的倍数
2. 要输出1~100之间的3的倍数,那只需要从1~100循环100次即可,每次拿到i之后,用i%3==0检测
   如果成立:i是3的倍数,输出
   如果不成立:i不是3的倍数
*/
#include <stdio.h>
int main()
{
    
    
    int i = 0;
    for(i=1; i<=100; i++)
    {
    
    
        if(i%3==0)
        {
    
    
            printf("%d ", i);
        }
    }
    return 0;
}

Output from largest to smallest

Write code to output three integer numbers in descending order.

For example:

Input: 2 3 1

Output: 3 2 1

/*
思路:
该题比较简单,参考代码
*/
#include <stdio.h>
int main()
{
    
    
    int a = 2;
    int b = 3;
    int c = 1;
    scanf("%d%d%d",&a, &b,&c);
    if(a<b)
    {
    
    
        int tmp = a;
        a = b;
        b = tmp;
    }
    if(a<c)
    {
    
    
        int tmp = a;
        a = c;
        c = tmp;
    }
    if(b<c)
    {
    
    
        int tmp = b;
        b = c;
        c = tmp;
    }
    printf("a=%d b=%d c=%d\n", a, b, c);
    return 0;
}

print prime numbers

Write a code: print prime numbers between 100 and 200

/*
思路:
素数:即质数,除了1和自己之外,再没有其他的约数,则该数据为素数,具体方式如下
*/


//方法一:试除法
int main()
{
    
    
	int i = 0;
	int count = 0;


    // 外层循环用来获取100~200之间的所有数据,100肯定不是素数,因此i从101开始
	for(i=101; i<=200; i++)
	{
    
    
		//判断i是否为素数:用[2, i)之间的每个数据去被i除,只要有一个可以被整除,则不是素数
		int j = 0;
		for(j=2; j<i; j++)
		{
    
    
			if(i%j == 0)
			{
    
    
				break;
			}
		}
        
		// 上述循环结束之后,如果j和i相等,说明[2, i)之间的所有数据都不能被i整除,则i为素数
		if(j==i)
		{
    
    
			count++;
			printf("%d ", i);
		}
	}


	printf("\ncount = %d\n", count);
	return 0;
}


//上述方法的缺陷:超过i一半的数据,肯定不是i的倍数,上述进行了许多没有意义的运算,因此可以采用如下
// 方式进行优化
// 方法二:每拿到一个数据,只需要检测其:[2, i/2]区间内是否有元素可以被2i整除即可,可以说明i不是素数
int main()
{
    
    
	int i = 0;//
	int count = 0;


	for(i=101; i<=200; i++)
	{
    
    
		//判断i是否为素数
		//2->i-1
		int j = 0;
		for(j=2; j<=i/2; j++)
		{
    
    
			if(i%j == 0)
			{
    
    
				break;
			}
		}
		//...
		if(j>i/2)
		{
    
    
			count++;
			printf("%d ", i);
		}
	}


	printf("\ncount = %d\n", count);
	return 0;
}




/*
方法二还是包含了一些重复的数据,再优化:
如果i能够被[2, sqrt(i)]之间的任意数据整除,则i不是素数
原因:如果 m 能被 2 ~ m-1 之间任一整数整除,其二个因子必定有一个小于或等于sqrt(m),另一个大于或等于 sqrt(m)。
*/
int main()
{
    
    
	int i = 0;
	int count = 0;


	for(i=101; i<=200; i++)
	{
    
    
		//判断i是否为素数
		//2->i-1
		int j = 0;
		for(j=2; j<=sqrt(i); j++)
		{
    
    
			if(i%j == 0)
			{
    
    
				break;
			}
		}
		//...
		if(j>sqrt(i))
		{
    
    
			count++;
			printf("%d ", i);
		}
	}


	printf("\ncount = %d\n", count);
	return 0;
}


//方法4
/*
继续对方法三优化,只要i不被[2, sqrt(i)]之间的任何数据整除,则i是素数,但是实际在操作时i不用从101逐渐递增到200,因为出了2和3之外,不会有两个连续相邻的数据同时为素数
*/


int main()
{
    
    
	int i = 0;
	int count = 0;


	for(i=101; i<=200; i+=2)
	{
    
    
		//判断i是否为素数
		//2->i-1
		int j = 0;
		for(j=2; j<=sqrt(i); j++)
		{
    
    
			if(i%j == 0)
			{
    
    
				break;
			}
		}
		//...
		if(j>sqrt(i))
		{
    
    
			count++;
			printf("%d ", i);
		}
	}

	printf("\ncount = %d\n", count);
	return 0;
}

print leap year

Print leap years between 1000 and 2000

/*
思路:
要求1000年到2000年之间的闰年,只需要知道求解闰年的方法即可。
闰年的条件:如果N能够被4整除,并且不能被100整除,则是闰年
           或者:N能被400整除,也是闰年
     即:4年一润并且百年不润,每400年再润一次
*/


#include <stdio.h>
int main()
{
    
    
	int year = 0;
	for(year=1000; year<=2000; year++)
	{
    
    
		//判断year是否为闰年
		if(year%4==0)  // 如果year能够被4整除,year可能为闰年
		{
    
    
			if(year%100!=0) // 如果year不能内100整除,则一定是闰年
			{
    
    
				printf("%d ", year);
			}
		}
		if(year%400 == 0)  // 每400年再润一次
		{
    
    
			printf("%d ", year);
		}
	}


	return 0;
}


//
//介绍一下这种的简单写法
//
int main()
{
    
    
	int year = 0;
	for(year=1000; year<=2000; year++)
	{
    
    
		if(((year%4==0)&&(year%100!=0))||(year%400==0))
		{
    
    
			printf("%d ", year);
		}
	}

	return 0;
}

greatest common divisor

Given two numbers, find the greatest common divisor of the two numbers

For example:

Input: 20 40

Output: 20

/*
最大公约数:即两个数据中公共约数的最大者。
求解的方式比较多,暴力穷举、辗转相除法、更相减损法、Stein算法算法
此处主要介绍:辗转相除法


思路:
例子:18和24的最大公约数
第一次:a = 18  b = 24  c = a%b = 18%24 = 18
      循环中:a = 24   b=18
第二次:a = 24   b = 18  c = a%b = 24%18 = 6
      循环中:a = 18   b = 6


第三次:a = 18   b = 6   c=a%b = 18%6 = 0
  循环结束
  
此时b中的内容即为两个数中的最大公约数。
*/


int main()
{
    
    
	int a = 18;
	int b = 24;
	int c = 0;

	while(c=a%b)
	{
    
    
		a = b;
		b = c;
	}

	printf("%d\n", b);
	return 0;
}

Which of the following is a bitwise operator:

A.&
B.&&
C.||
D.!
Answer analysis:

A. & is a bitwise AND operator, correct

B. && is logical and, not bitwise and, wrong

C. || is logical or, wrong

D. ! is the logical inverse operator, error

The result of the following code is:

#include <stdio.h>
int main()
{
    
    
	int a, b, c;
	a = 5;
	c = ++a;
	b = ++c, c++, ++a, a++;
	b += a++ + c;
	printf("a = %d b = %d c = %d\n:", a, b, c);
	return 0;
}

A.a = 8 b = 23 c = 8
B.a = 9 b= 23 c = 8
C.a = 9 b = 25 c = 8
D.a = 9 b = 24 c = 8
答案解析:

++ operator: divided into pre ++ and post ++,

Preposition ++: first add 1, then use, that is, use the content in the variable first, and then add 1 to the result

Postfix ++: Use the content in the variable first, and add 1 to the variable when the entire expression ends

Comma expression, takes the value of the last expression.

#include <stdio.h>
int main()
{
    
    
	int a, b, c;
	a = 5;
	c = ++a;// ++a:加给a+1,结果为6,用加完之后的结果给c赋值,因此:a = 6  c = 6
	b = ++c, c++, ++a, a++;
   // 逗号表达式的优先级,最低,这里先算b=++c, b得到的是++c后的结果,b是7
   // b=++c 和后边的构成逗号表达式,依次从左向右计算的。
   // 表达式结束时,c++和,++a,a++会给a+2,给c加1,此时c:8,a:8,b:7
	b += a++ + c; // a先和c加,结果为16,在加上b的值7,比的结果为23,最后给a加1,a的值为9
	printf("a = %d b = %d c = %d\n:", a, b, c); // a:9, b:23, c:8
	return 0;
}

Therefore: choose B

Swap two variables (without creating a temporary variable)

It is not allowed to create a temporary variable that swaps the contents of two integers

#include <stdio.h>
int main()
{
    
    
	int a = 10;
    int b = 20;
    printf("交换前:a = %d b = %d\n", a,b);
    a = a^b;
    b = a^b;
    a = a^b;
    printf("交换后:a = %d b = %d\n", a,b);
	return 0;
}

Count the number of 1's in binary

Write a function that returns the number of 1s in the argument binary.

For example: 15 0000 1111 4
OJ links of 1Niuke.com

/*
方法一:
思路:
循环进行以下操作,直到n被缩减为0:
   1. 用该数据模2,检测其是否能够被2整除
   2. 可以:则该数据对应二进制比特位的最低位一定是0,否则是1,如果是1给计数加1
   3. 如果n不等于0时,继续1
*/
int count_one_bit(int n)
{
    
    
	int count = 0;
	while(n)
	{
    
    
		if(n%2==1)
			count++;
		n = n/2;
	}
	return count;
}


/*
上述方法缺陷:进行了大量的取模以及除法运算,取模和除法运算的效率本来就比较低。
方法二思路:
一个int类型的数据,对应的二进制一共有32个比特位,可以采用位运算的方式一位一位的检测,具体如下
*/
int count_one_bit(unsigned int n)
{
    
    
	int count = 0;
	int i = 0;
	for(i=0; i<32; i++)
	{
    
    
		if(((n>>i)&1) == 1)
			count++;
	}
	return count;
}


/*
方法二优点:用位操作代替取模和除法运算,效率稍微比较高
  缺陷:不论是什么数据,循环都要执行32次
  
方法三:
思路:采用相邻的两个数据进行按位与运算
举例:
9999:‭10 0111 0000 1111‬
第一次循环:n=9999   n=n&(n-1)=9999&9998= 9998
第二次循环:n=9998   n=n&(n-1)=9998&9997= 9996
第三次循环:n=9996   n=n&(n-1)=9996&9995= 9992
第四次循环:n=9992   n=n&(n-1)=9992&9991= 9984
第五次循环:n=9984   n=n&(n-1)=9984&9983= 9728
第六次循环:n=9728   n=n&(n-1)=9728&9727= 9216
第七次循环:n=9216   n=n&(n-1)=9216&9215= 8192
第八次循环:n=8192   n=n&(n-1)=8192&8191= 0


可以观察下:此种方式,数据的二进制比特位中有几个1,循环就循环几次,而且中间采用了位运算,处理起来比较高效
*/
int count_one_bit(int n)
{
    
    
	int count = 0;
	while(n)
	{
    
    
		n = n&(n-1);
		count++;
	}
	return count;
}

Print the odd and even bits of an integer binary

Get all the even and odd bits in an integer binary sequence, and print out the binary sequence respectively

/*
思路:
1. 提取所有的奇数位,如果该位是1,输出1,是0则输出0
2. 以同样的方式提取偶数位置


 检测num中某一位是0还是1的方式:
   1. 将num向右移动i位
   2. 将移完位之后的结果与1按位与,如果:
      结果是0,则第i个比特位是0
      结果是非0,则第i个比特位是1
*/
void Printbit(int num)
{
    
    
	for(int i=31; i>=1; i-=2)
	{
    
    
		printf("%d ", (num>>i)&1);
	}
	printf("\n");
    
	for(int i=30; i>=0; i-=2)
	{
    
    
		printf("%d ", (num>>i)&1);
	}
	printf("\n");
}

Find the number of different bits in two numbers in binary

Programming implementation: How many bits (bits) are different in the binary representation of two int (32-bit) integers m and n?

Input example:

1999 2299

Example output: 7

Niuke's OJ link (hyperlink)

/*
思路:
1. 先将m和n进行按位异或,此时m和n相同的二进制比特位清零,不同的二进制比特位为1
2. 统计异或完成后结果的二进制比特位中有多少个1即可
*/
#include <stdio.h>
int calc_diff_bit(int m, int n)
{
    
    
	int tmp = m^n;
	int count = 0;
	while(tmp)
	{
    
    
		tmp = tmp&(tmp-1);
		count++;
	}
	return count;
}


int main()
{
    
    
 int m,n;
 while(scanf("%d %d", &m, &n) == 2)
 {
    
    
     printf("%d\n", calc_diff_bit(m, n));
 }
 return 0;
}

The result of the following code is

#include <stdio.h>
int main()
{
    
    
    int i = 1;
    int ret = (++i)+(++i)+(++i);
    printf("ret = %d\n", ret);
	return 0;
}

A.10
B.12
C.9
D. Program error
Answer analysis:

The expression (++i)+(++i)+(++i), only has the priority and associativity of operators, and cannot determine the only calculation path

Therefore, this expression may result in inconsistent results due to the difference in the calculation order, so the expression is a wrong expression.

Can be tested in VS and Linux gcc, the results may be different.

The result of the following code is:

#include <stdio.h>
int i;
int main()
{
    
    
    i--;
    if (i > sizeof(i))
    {
    
    
        printf(">\n");
    }
    else
    {
    
    
        printf("<\n");
    }
    return 0; 
}

A.>
B.<
C. No output
D. There is a problem with the program
Answer analysis:

In C language, 0 is false and non-zero is true.

Global variables, when no initial value is given, will be initialized to 0 by default when compiled.

The initial value of i is 0, i–result -1, i is an integer, sizeof(i) calculates the size of i type is 4, according to this analysis, the result should choose B, but the return value type of sizeof is actually an unsigned integer , so the compiler will automatically convert the i on the left to unsigned integer data, and the unsigned integer corresponding to -1 is a very large number, exceeding 4 or 8, so A should actually be selected

This question is actually very hidden, it's really shrimp and pig's heart! ! !

Therefore: choose A

The statement about expression evaluation that is incorrect is:

A. Expression evaluation first checks whether there is plastic promotion or arithmetic conversion, and then calculates
B. When the expression is actually calculated, it first looks at the priority of adjacent operators to determine who should be counted first
. C. Adjacent operators have the same priority In the case of , the calculation order is determined by the associativity of the operator
D. As long as there is priority and associativity, the expression can find a unique value
Answer analysis:

A: correct

B: correct

C: correct

D: Error, with priority and tuberculosis, expressions may also have different computer paths, resulting in differences in calculation results.

BC100 - Ordered Sequence Merging

https://www.nowcoder.com/practice/a9e943b0dab142759807d0cfb6863897?tpId=107&&tqId=33381&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

#include <stdio.h>


int main()
{
    
    
    int n = 0;
    int m = 0;
    int arr1[100] = {
    
    0};
    int arr2[100] = {
    
    0};
    //输入
    scanf("%d %d", &n, &m);
    int i = 0;
    for(i=0; i<n; i++)
    {
    
    
        scanf("%d", &arr1[i]);
    }
    for(i=0; i<m; i++)
    {
    
    
        scanf("%d", &arr2[i]);
    }
    //处理
    int j = 0;
    i = 0;
    while(i<n && j<m)
    {
    
    
        if(arr1[i] < arr2[j])
        {
    
    
            printf("%d ", arr1[i]);
            i++;
        }
        else
        {
    
    
            printf("%d ", arr2[j]);    
            j++;
        }
    }
    if(i == n)
    {
    
    
        for(; j<m; j++)
        {
    
    
            printf("%d ", arr2[j]);
        }
    }
    else
    {
    
    
        for(; i<n; i++)
        {
    
    
            printf("%d ", arr1[i]);
        }
    }
    return 0;
}

BC96-ordered sequence judgment

https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e?tpId=107&&tqId=33377&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

#include <stdio.h>

int main()
{
    
    
    int n = 0;
    int arr[50] = {
    
    0};
    scanf("%d", &n);
    int i = 0;
    int flag1 = 0;
    int flag2 = 0;
    for(i=0; i<n; i++)
    {
    
    
        scanf("%d", &arr[i]);
        if(i>0)
        {
    
    
            if(arr[i]>arr[i-1])
                flag1 = 1;
            else if(arr[i]<arr[i-1])
                flag2 = 1;
        }
    }
    //flag1 和 flag2 都为1是乱序的
    if(flag1+flag2 > 1)
        printf("unsorted\n");
    else
        printf("sorted\n");
    return 0;
}

BC54-Get the number of days in the month

https://www.nowcoder.com/practice/13aeae34f8ed4697960f7cfc80f9f7f6?tpId=107&&tqId=33335&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

#include <stdio.h>
int main()
{
    
    
    int y = 0;
    int m = 0;
    int days[12] = {
    
    31,28,31,30,31,30,31,31,30,31,30,31}; 
    while(scanf("%d%d", &y, &m) != EOF)
    {
    
    
        int day = days[m-1];
        if((y%4==0 && y%100!=0) || (y%400==0))
        {
    
    
            if(m == 2)
                day += 1;
        }
        printf("%d\n", day);
    }
    return 0;
}

Common error categories for C programs do not include:

A. Compile error
B. Link error
C. Stack overflow
D. Runtime error
Answer analysis:

Stack overflow is a type of runtime error, so the C program does not list stack overflow separately, and stack overflow is included in runtime errors.

Therefore: choose C

Which form in C language declares a pointer p pointing to a variable of type char, the value of p cannot be modified, but the value of the variable pointed to by p can be modified?

A.const char *p
B.char const p
C.char
const p
D.const char *const p
Answer analysis:

A: Error, const modifies *p, indicating that the content pointed to by p cannot be modified

B: Error, same as above

C: Correct, const modifies p itself, indicating that the pointer of p cannot be modified, and the content in the space pointed to by p can be modified

D: Error, the first const means that the content pointed to by p cannot be modified, and the second const means that p cannot point to other variables

Therefore, choose C

Which of the following statements about pointers is true

A.int *const p is equivalent to int const *p
B.const int *p is equivalent to int *const p
C.const int *p is equivalent to int const *p
D.int *p[10] is equivalent to int ( *p)[10] equivalent
answer analysis:

A: Error, in int* const p, const modifies the pointer variable p itself, indicating that the point of p cannot be changed.

In int const *p, const modifies the result after dereferencing the p pointer, indicating that the content pointed to by p cannot be changed

Therefore, not equivalent

B: Error, same as above

C: Correct, const modifies the result after dereferencing the p pointer, indicating that the content pointed to by p cannot be changed

D: Error, int p[10] defines a pointer array, 10 elements in the array, each element is a pointer of type int

int (*p)[10] defines an array pointer that can only point to an array that stores 10 integer elements

Therefore: choose C

BC68-X pattern

https://www.nowcoder.com/practice/83d6afe3018e44539c51265165806ee4?tpId=107&&tqId=33349&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

#include <stdio.h>


int main()
{
    
    
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
    
    
        int i = 0;
        int j = 0;
        for(i=0; i<n; i++)
        {
    
    
            for(j=0; j<n; j++)
            {
    
    
                if(i == j)
                    printf("*");
                else if(i+j == n-1)//因为行和列是从0开始的
                    printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }
    }
    return 0;
}

BC60-right triangle pattern with space

https://www.nowcoder.com/practice/192d1039c3d44155bb868073f5482670?tpId=107&&tqId=33341&rp=1&ru=/ta/beginner-programmers&qru=/ta/beginner-programmers/question-ranking

#include <stdio.h>
/*
int main()
{
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
        int i = 0;
        //行数控制
        for(i=0; i<n; i++)
        {
            //空格
            int j = 0;
            for(j=0; j<n-1-i; j++)
            {
                printf("  ");
            }
            //*
            for(j=0; j<=i; j++)
            {
                printf("* ");    
            }
            printf("\n");
        }
        
    }
    return 0;
}


*/


int main()
{
    
    
    int n = 0;
    while(scanf("%d", &n) != EOF)
    {
    
    
        int i = 0;
        int j = 0;
        //行数
        for(i=0; i<n; i++)
        {
    
    
            //一行
            for(j=0; j<n; j++)
            {
    
    
                //行和列的和
                //这里可以把行数和列数标出来就能看明白
                if(i+j<n-1)
                {
    
    
                    printf("  ");
                }
                else
                {
    
    
                    printf("* ");
                }
            }
            printf("\n");
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/fjj2397194209/article/details/131360013