One week summary: c language exercises

A small summary a week, Hello every, I’m BaldCub


Today I will summarize the problems that I encountered in learning the C language this week. When the
follow-up time is sufficient, I will do some detailed code ideas to explain the
code may not be too perfect. If there are any problems, please point out that we can exchange and learn together!

1. Write code to output three numbers from small to large

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

3. Given two numbers, find the greatest common divisor of these two numbers

4. Print leap years between 1000 and 2000

5. Write a code: print prime numbers between 100-200

6. Write a program to count how many numbers appear in all integers from 1 to 100 9

7. Calculate the value of 1/1-1/2+1/3-1/4+1/5…+1/99-1/100 and print out the result

8. Find the largest value among ten integers

9. Output 9*9 multiplication formula table on the screen

10. Write code to find a specific number in an integer ordered array

Topic 1:

#include<stdio.h>      
int main()
{
    
    
	int a ;
    int b ;
	int c ;
	int s;
	printf("请输入三个数字:\n");
	scanf("%d %d %d", &a, &b, &c);
	if (a > b )
	{
    
    
		s = b;
		b = a;
		a = s;
	}
	if (a > c)
	{
    
    
		s = c;
		c = a;
		a = s;
	}
	if (b > c)
	{
    
    
		s = c;
		c = b;
		b = s;
	}
	printf("从小到大输出:%d %d %d\n", a, b, c);

}

Topic 2:

#include<stdio.h>
int main()
{
    
    
	int i = 1;
	while (i<=100)
	{
    
    
		if (i % 3 == 0)
			printf("%d ", i);
		i++;
	}
	return 0;
}

Question 3:

#include<stdio.h>  
int main()
{
    
    
	int a = 0;
	int b = 0;
	int s = 0;
	scanf("%d %d", &a, &b);
	while (s = a % b)
	{
    
    
		a = b;
		b = s;
	}
	printf("%d\n", b);
	return 0;

}

Question 4:

#include<stdio.h>  
int main()
{
    
    
	int i = 0;
	int count = 0;
	for (i = 1000; i <= 2000; i++)
	{
    
    
		if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
		{
    
    
			printf("%d ", i);
			count++;
		}
	}
	printf("共有多少个符合条件的闰年:%d ", count);
	return 0;
}

Question 5:

#include<stdio.h>  
int main()
{
    
    
	int i = 0;
	for (i = 100; i <= 200; i++)
	{
    
    
		int j = 0;
		for (j = 2; j < i; j++)
		{
    
    
			if (i%j == 0)
			{
    
    
				break;
			}
		}
		if (i == j)
		{
    
    
			printf("%d ", i);
		}
	}
	return 0;
}

Question 6:

#include<stdio.h>  //题目六
int main()
{
    
    
	int i = 0;
	int count = 0;
	for (i = 1; i <= 100; i++)
	{
    
    
		if (i / 10 == 9)
		{
    
    
			count++;
			
		}
		if (i % 10 == 9)
		{
    
    
			count++;
			
		}
	}
	printf("总数是:%d\n", count-1); //重复两次99 减去一次
	return 0;
}	

Question 7:

#include<stdio.h>  //题目七
int main()
{
    
    
	int i = 0;
	float sum = 0.0;
	int flag = 1.0;
	for (i = 1; i <= 100; i++)
	{
    
    
		sum += flag * 1.0 / i;
		flag = -flag;  //解决 + - 问题

	}
	printf("%f\n", sum);
	return 0;
}

Question 8:

#include<stdio.h>  

int main()
{
    
    
	int a[10];
	int max;
	int i;
	printf("请输入10个整数:");
	for (i = 0; i < 10; i++)
	{
    
    
		scanf("%d", &a[i]);
	}
	max = a[0];
	for (i = 0; i < 10; i++)
	{
    
    
		if (a[i]>max)
		{
    
    
			max = a[i];
			i++;
		}
	}
	printf("max=%d", max);
	return 0;
}

Question 9:

#include<stdio.h>  
int main()
{
    
    
	int i = 0;
	for (i = 1; i <= 9; i++)
	{
    
    
		int j = 1;
		for (j = 1; j <= 9; j++)
		{
    
    
			printf("%d * %d =  %-2d  ", i, j, i*j);
			
		}
           printf("\n");
	}
	
	return 0;
}

Question 10:

#include<stdio.h>  
int main()
{
    
    
	int arr[] = {
    
    0,1,2,3,4,5,6,7,8,9};
	int k=7;
	 int sz = sizeof(arr) / sizeof(arr[0]);
	 int left = 0;
	 int right = sz - 1;
	 for (int i = 0; i<sz; i++)
	 {
    
    
		 while (left <= right)
		 {
    
    
			 int mid = (left + right) / 2;
			 if (arr[mid] > k)
			 {
    
    
				 right = mid - 1;
			 }
			 else if (arr[mid] < k)
			 {
    
    
				 left = mid + 1;
			 }
			 else
			 {
    
    
				 printf("已找到这个数字的下标为:%d\n", mid);
				 break;
			 }
			 if (left>right)
			 {
    
    
				 printf("找不到此数字\n");
			 }
		 }

		 return 0;
	 }


}

Guess you like

Origin blog.csdn.net/weixin_47721582/article/details/112791011