C language homework practice in Wuhan University of Technology


first question

Find the greatest common divisor and the least common multiple. Enter two positive integers m and n (m≤1000, n≤1000), and find the greatest common divisor and least common multiple. Try to write the corresponding program.

#include<stdio.h>
int main() {
    
    
	int m, n,a,b,c;
	printf("请输入两个正整数m和n:(m<=1000,n<=1000,m>=n)\n");
	scanf("%d %d", &m, &n);
	a = m;
	b = n;
	while (b != 0) {
    
    
		c = a % b;
		a = b;
		b = c;
	}
	printf("最大公约数为%d\n", a);
	printf("最小公倍数为%d\n", m * n / a);
}

Second question

Use the function to verify Goldbach’s conjecture: any even number not less than 6 can be expressed as the sum of two odd prime numbers. For example, 6=3+3, 8=3+5,..., 18=5+13. Express the even numbers between 6 and 100 as the sum of two prime numbers, and print 5 groups on one line when printing. Try to write the corresponding program.

#include <stdio.h>                
int prime(int n);                   
int main()
{
    
    int m, i, a = 0;
	for (int n = 6; n <= 100; n += 2) {
    
    
		
		for (i = 3; i < n; i += 2) {
    
    
			m = n - i;
			if (prime(m) == 1 && prime(i) == 1) {
    
    
				printf("%d=%d+%d  ", n, i, m);
				a++;
				if (a % 5 == 0)
					printf("\n");
				break;
			}
		}
	}
	return 0;
}
int prime(int n)
{
    
    
	int i;
	for (i = 2; i < n && (n % i != 0); i++);
	return n == i ? 1 : 0;                      //判断是否为素数,是返回1,否返回0
}

Third question

Find the number that appears the most in a batch of integers. Enter a positive integer n (n≤1000), then enter n integers, analyze each digit of each integer, and find the number with the most occurrences. For example, if you enter 3 integers 1234, 2345, 3456, the most frequently occurring numbers are 3 and 4, both appearing 3 times. Try to write the corresponding program.

#include<stdio.h>
int main()
{
    
    
	int n,t=0;
	int b[10];
	int a,c;
	for (int i = 0; i < 10; i++) {
    
    
		b[i] = 0;
	}
	printf("你想要输入几个整数:(n<=1000)\n");
	scanf("%d", &n);
	printf("请输入这%d个整数(以空格隔开):\n",n);
	for (int i = 1; i <= n; i++) {
    
    
		scanf("%d", &a);
		if (a == 0) b[0] == 0;
		while (a != 0) {
    
    
			c = a % 10;
			a /= 10;
			b[c]++;
		}
	
	}
	for (int i = 0; i < 10; i++)
	{
    
    
		if (b[i] > t)
			t = b[i];
	}
	for (int i = 0; i < 10; i++)
	{
    
    
		if (t==b[i])
			printf(" %d", i);
	}
	return 0;
}

Fourth question

#include<stdio.h>
int main()
{
    
    
	int n,m=0;
	int a[6][6];
	printf("请输入n:(1<=n<=6)\n");
	scanf("%d", &n);
	printf("输入%d阶方阵\n", n);
	for (int i = 0; i < n; i++) {
    
    
		for (int j = 0; j < n; j++) {
    
    
			scanf("%d", &a[i][j]);
		}
	}
	for (int i = 0; i < n; i++) {
    
    
		for (int j = 0; j < i + 1; j++)
			if (a[i][j] == 0)
				m++;
	}
	if (m != 0)
		printf("YES");
	else
		printf("NO");
}

to sum up

The above is the C language-related homework exercises and the author's own thinking, I hope to help everyone.

Guess you like

Origin blog.csdn.net/mo_zhe/article/details/112443331