C language questions and analysis

1. Enter an integer (1~6), consider the last three digits of this integer, arrange and combine them into three digits, and output them in ascending order. The same three digits in the first digit are output as a line, and each number is a space. Separate, no spaces at the end of the line.
enter:

6

Output:

678  679  687  689  697  698
768  769  786  789  796  798
867  869  876  879  896  897
967  968  976  978  986  987

answer:

#include <stdio.h>

int main() {
    
    
	int cnt = 0;
	int number;
	int i, j, k;
	printf("请输入一个不超过6的数字:");
	scanf("%d", &number);

	for (i = number; i <= number + 3; i++) {
    
    
		for (j = number; j <= number + 3; j++) {
    
    
			for (k = number; k <= number + 3; k++) {
    
    
				if ((i != j) && (i != k) && (j != k)) {
    
    
					printf("%d%d%d", i, j, k);
					cnt++;
					if (cnt != 6)
						printf("  ");
				}
			}
		}
		printf("\n");
		cnt = 0;
	}

	return 0;
}

2. Print the lower triangle ninety-nine multiplication table

//九九乘法表
#include <stdio.h>
 
int main() {
    
    
	int i, j, product;
	for (i = 1; i <= 9; i++) {
    
    
		for (j = 1; j <= i; j++) {
    
    
			product = j * i;
			printf("%d*%d=%d", j, i, product);
			if (product < 10) {
    
    
				printf("   ");
			} else
				printf("  ");
		}
		printf("\n");
	}
	return 0;
}

3. Enter a number N (3~7), and print N-digit daffodil number, such as 153 is a 3-digit daffodil number, 153 = 1 3 + 5 3 + 3 3 153 = 1^3+5^3+3 ^3153=13+53+33

//水仙花数
#include <stdio.h>
#include <math.h>
 
int main() {
    
    
	int N, i, s, e;
	int start[] = {
    
    0, 0, 0, 100, 1000, 10000, 100000, 1000000};
	int end[] = {
    
    0, 0, 0, 999, 9999, 99999, 999999, 9999999};
	int sum = 0;
	int t;
	scanf("%d", &N);
	s = start[N];
	e = end[N];
 
	for (i = s; i <= e; i++) {
    
    
		t = i;
		//分解数字
		do {
    
    
			int d = t % 10;
			t /= 10;
			sum = sum + pow(d, N);
		} while (t > 0);
 
		if (i == sum) {
    
    
			printf("%d\n", i);
		}
		sum = 0;
	}
	return 0;
}

4. Input two numbers M and N, print the number of prime numbers between M and N, and the sum of these prime numbers.

#include <stdio.h>

int main() {
    
    
	int M, N;
	int i, j;
	int isPrime;
	int cnt = 0, sum = 0;
	scanf("%d %d", &M, &N);
	for (i = M; i <= N; i++) {
    
    
		isPrime = 1;
		for (j = 2; j < i; j++) {
    
    
			if (i % j == 0) {
    
    
				isPrime = 0;
				break;
			}
		}
		if (isPrime == 1) {
    
    
			sum += i;
			cnt++;
		}
	}
	printf("%d %d", cnt, sum);
	return 0;
}

5. Corporate bonus

#include <stdio.h>
#include "math.h"
/*
10- 	10-20	20-40	40-60	60-100	100+
0.1 	0.075	0.05	0.03	0.015	0.01

*/
int main() {
    
    
	double bonus[] = {
    
    0.1, 0.075, 0.05, 0.03, 0.015, 0.01};
	double table[] = {
    
    0, 10, 20, 40, 60, 100};
	int i;
	double sum = 0;
	double profit;
	scanf("%lf", &profit);

	for (i = 0; profit - table[i] >= 0; i++) {
    
    
	}
	i--;

	sum += bonus[i] * (profit - table[i]);
	i--;

	for (i; i >= 0; i--) {
    
    
		sum += bonus[i] * (table[i + 1] - table[i]);
	}
	printf("盈利为%f万,奖金为%f万", profit, sum);

	return 0;
}

6. Decompose prime factors

//分解质因数
#include <stdio.h>

int main() {
    
    
	int number;
	int i;
	printf("请输入一个整数:");
	scanf("%d", &number);
	printf("%d=", number);
	for (i = 2; i <= number; i++) {
    
    
		if (number % i == 0) {
    
    
			printf("%d", i);
			number = number / i;
			i = 2;
			if (number != 1) {
    
    
				printf("*");
			}
		}

	}
	return 0;
}

7. Greatest common divisor, least common multiple

//最大公约数,最小公倍数
#include <stdio.h>

int main() {
    
    
	int gcd, lcm;
	int min, max, t;
	int i;
	scanf("%d %d", &min, &max);
	if (min > max) {
    
    
		t = max;
		max = min;
		min = t;
	}
	for (i = 2; i <= min; i++) {
    
    
		if (min % i == 0 && max % i == 0) {
    
    
			gcd = i;
		}
	}
	for (i = max; i <= max * min; i++) {
    
    
		if (i % max == 0 && i % min == 0) {
    
    
			lcm = i;
			break;
		}
	}
	printf("最大公因数为%d,最小公倍数为%d", gcd, lcm);
	return 0;
}
//完数 = 其因子之和(因子不包括本身)
#include <stdio.h>

int main() {
    
    

	int i;
	int j;
	int sum;
	for (i = 1; i <= 1000; i++) {
    
    
		sum = 0;
		for (j = 1; j < i; j++) {
    
    
			if (i % j == 0) {
    
    
				sum += j;
			}
		}
		if (sum == i)
			printf("完数:%d\n", sum);
		else
			continue;
	}

	return 0;
}

8. The ball falls to the ground

//完数 = 其因子之和(因子不包括本身)
//小球落地
#include <stdio.h>

int main() {
    
    

	int i;
	double height = 100;
	double sum = 0;
	for (i = 1; i <= 9; i++) {
    
    
		sum = sum + height;
		printf("第%d次落地\n", i);
		height /= 2;
		sum = sum + height;
	}
	sum = sum + height;
	printf("第10次落地,共经过%.2fm", sum);
	return 0;
}

9. Monkey eats peach

//猴子吃桃
#include <stdio.h>

int main() {
    
    
	int x = 1;
	int sum = 1;
	int i;
	for (i = 1; i <= 10; i++) {
    
    
		x = x + 1;
		x = 2 * x;
	}
	printf("%d\n", x);
	return 0;
}

10. Table tennis game, the first group a, b, c, the second group x, y, z. a is not compared with x, c is not compared with x, z, find the list of matches.

//程序分析
//i,j,k 是a,b,c的对手,可能取值范围均为x,y,z,因此通过i,j,k遍历x,y,z来获得取值

#include <stdio.h>

int main() {
    
    
	char i, j, k;
	for (i = 'x'; i <= 'z'; i++) {
    
    
		for (j = 'x'; j <= 'z'; j++) {
    
    
			for (k = 'x'; k <= 'z'; k++) {
    
    
				if (i != j && i != k && j != k) {
    
    
					if (i != 'x' && k != 'x' && k != 'z') {
    
    
						printf("a-%c b-%c c-%c ", i, j, k);
					}
				}

			}
		}
	}
	return 0;
}

Detailed explanation: The two ifs are the key here. The first if is to ensure that the team members are not repeated, and the second if is the title information.
11. Count the length of the number, and output in reverse order

#include <stdio.h>

int main() {
    
    
	char number[100];
	int len = 0;
	int i;
	for (i = 0; i <= 100; i++) {
    
    
		number[i] = getchar();
		if (number[i] == '\n') {
    
    
			break;
		} else
			len++;
	}
	printf("长度为%d", len);
	for (i = len; i >= 0; i--) {
    
    
		putchar(number[i]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44823313/article/details/113823512