• 洛谷刷题C语言:数列求和、求极差/最大跨度值、求三角形、冰雹猜想、旗鼓相当的对手


    记录洛谷刷题C语言,一些不太优雅的代码


    一、【深基4.例11】数列求和

    题目描述

    计算 1 + 2 + 3 + ⋯ + ( n − 1 ) + n 1+2+3+\cdots+(n-1)+n 1+2+3++(n1)+n 的值,其中正整数 n n n 不大于 100。由于你没有高斯聪明,所以你不被允许使用等差数列求和公式直接求出答案。

    输入格式

    输入一个正整数 n n n

    输出格式

    输出一个正整数,表示最后求和的答案。

    样例 #1

    样例输入 #1

    100
    
    • 1

    样例输出 #1

    5050
    
    • 1

    提示

    数据保证, 1 ≤ n ≤ 100 1 \leq n \leq 100 1n100

    代码如下

    #include
    #include
    #include
    #include 
     
    int main()
    {
    	int n;
    	scanf("%d",&n);
    	
    	int sum = 0;
    	for(int i = 1;i <= n;i++)
    	{
    		sum = sum + i;
    	}
    	
    	printf("%d",sum);
    	return 0;	
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    二、【深基4.习5】求极差 / 最大跨度值

    题目描述

    给出 n n n n n n 个整数 a i a_i ai,求这 n n n 个整数中的极差是什么。极差的意思是一组数中的最大值减去最小值的差。

    输入格式

    第一行输入一个正整数 n n n,表示整数个数。

    第二行输入 n n n 个整数 a 1 , a 2 … a n a_1,a_2 \dots a_n a1,a2an,以空格隔开。

    输出格式

    输出一个整数,表示这 n n n 个整数的极差。

    样例 #1

    样例输入 #1

    6
    1 1 4 5 1 4
    
    • 1
    • 2

    样例输出 #1

    4
    
    • 1

    提示

    数据保证, 1 ≤ n ≤ 100 1 \leq n\leq 100 1n100 0 ≤ a i ≤ 1000 0\le a_i \le 1000 0ai1000

    代码如下

    #include
    #include
    #include
    #include 
     
    int main()
    {
    	int n;
    	scanf("%d",&n);
    	
    	int min = 1000, max = 1;
    	
    	for(int i = 1;i <= n;i++)
    	{
    		int num;
    		scanf("%d",&num);
    		if(num < min)
    		{
    			min = num;
    		}
    		if(num > max)
    		{
    			max = num;
    		}
    	}
    	
    	printf("%d",max - min);	
    	return 0;	
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    三、【深基4.习8】求三角形

    题目描述

    模仿例题,打印出不同方向的正方形,然后打印三角形矩阵。中间有个空行。

    输入格式

    输入矩阵的规模,不超过 9 9 9

    输出格式

    输出矩形和正方形

    样例 #1

    样例输入 #1

    4
    
    • 1

    样例输出 #1

    01020304
    05060708
    09101112
    13141516
    
          01
        0203
      040506
    07080910
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    代码如下

    #include
    #include
    #include
    #include 
     
    int main()
    {
    	int n;
    	scanf("%d",&n);
    	
    	int x = 1; 
    	for(int i = 1;i <= n;i++)
    	{
    		for(int j = 1;j <= n;j++)
    		{
    			if(x < 10)
    			{
    				printf("0%d",x);
    				x++;
    			}
    			else
    			{
    				printf("%d",x);
    				x++;
    			}
    		}
    		printf("\n");
    	}
    	
    	printf("\n");
    	int m = 1;
    	for(int i = n-1;i >= 0;i--)
    	{
    		for(int j = 0;j < i;j++)
    		{
    			printf("  ");
    		}
    		for(int t = i;t <= n-1;t++)
    		{
    			if(m < 10)
    			{
    				printf("0%d",m);
    				m++;
    			}
    			else 
    			{
    				printf("%d",m);
    				m++;
    			}
    		}
    		printf("\n");
    	}
    	return 0;	
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    四、【深基5.例3】冰雹猜想

    题目描述

    给出一个正整数 n n n,然后对这个数字一直进行下面的操作:如果这个数字是奇数,那么将其乘 3 3 3 再加 1 1 1,否则除以 2 2 2。经过若干次循环后,最终都会回到 1 1 1。经过验证很大的数字( 7 × 1 0 11 7\times10^{11} 7×1011)都可以按照这样的方式比变成 1 1 1,所以被称为“冰雹猜想”。例如当 n n n 20 20 20,变化的过程是 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 20\to 10\to 5\to 16\to 8\to 4\to 2\to 1 20105168421

    根据给定的数字,验证这个猜想,并从最后的 1 1 1 开始,倒序输出整个变化序列。

    输入格式

    输入一个正整数 n n n

    输出格式

    输出若干个由空格隔开的正整数,表示从最后的 1 1 1 开始倒序的变化数列。

    样例 #1

    样例输入 #1

    20
    
    • 1

    样例输出 #1

    1 2 4 8 16 5 10 20
    
    • 1

    提示

    数据保证, 1 ≤ n ≤ 100 1 \le n\le 100 1n100

    代码如下

    #include
    #include
    #include
    #include 
     
    int main()
    {	
    	int n;
    	scanf("%d",&n);
    	
    	int num[100000];
    	int i = 0; 
    	int m = n;
    	while(m != 1)
    	{
    		if(m % 2 ==0)
    		{
    			m = m/2;
    			num[i] = m;
    			i++;
    		}
    		else
    		{
    			m = m*3 + 1;
    			num[i] = m;
    			i++;
    		}
    	}
    	
    	for(int j = i - 1;j >= 0;j--)
    	{
    		printf("%d ",num[j]);
    	}
    	printf("%d\n",n);
    	return 0;	
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    五、【深基5.例5】旗鼓相当的对手

    题目描述

    现有 N N N 名同学参加了期末考试,并且获得了每名同学的信息:语文、数学、英语成绩(均为不超过 150 150 150 的自然数)。如果某对学生 < i , j > \text{<}i,j\text{>} <i,j> 的每一科成绩的分差都不大于 5 5 5,且总分分差不大于 10 10 10,那么这对学生就是“旗鼓相当的对手”。现在想知道这些同学中,有几对“旗鼓相当的对手”?同样一个人可能会和其他好几名同学结对。

    输入格式

    第一行一个正整数 N N N

    接下来 N N N 行,每行三个整数,其中第 i i i 行表示第 i i i 名同学的语文、数学、英语成绩。最先读入的同学编号为 1 1 1

    输出格式

    输出一个整数,表示“旗鼓相当的对手”的对数。

    样例 #1

    样例输入 #1

    3
    90 90 90
    85 95 90
    80 100 91
    
    • 1
    • 2
    • 3
    • 4

    样例输出 #1

    2
    
    • 1

    提示

    数据保证, 2 ≤ N ≤ 1000 2 \le N\le 1000 2N1000 且每科成绩为不超过 150 150 150 的自然数。

    代码如下

    #include
    #include
    #include
    #include 
     
    int main()
    {	
    	int n;
    	scanf("%d",&n);
    	
    	int C[n], M[n], E[n];
    	for(int i = 0;i < n;i++)
    	{
    		scanf("%d%d%d",&C[i],&M[i],&E[i]);
    	}
    	
    	int couple = 0;
    	for(int i = 0;i < n;i++)
    	{
    		for(int j = i+1;j < n;j++)
    		{
    			int num = C[i] + M[i] + E[i];
    			int sum = C[j] + M[j] + E[j];
    			if(abs(C[i]-C[j]) <= 5&&abs(M[i]-M[j]) <= 5&&abs(E[i]-E[j])<= 5&&abs(num-sum) <= 10)
    			{
    				couple ++;
    			}
    		}
    	}
    	
    	printf("%d\n",couple);
    	return 0;	
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    2022-04-25-ElasticSearch
    Jenkins kubernetes(k8s)滚动发布实战
    记账APP:小哈记账5——记账首页页面的制作(2)
    Word控件Spire.Doc 【段落处理】教程(十七):在 C#、VB.NET 中的 Word 中按样式名称获取段落
    python excel 读取及写入固定格式
    【done】剑指 Offer 53 - II:0~n-1中缺失的数字
    【python基础】类-初识类
    docker容器内调用宿主机docker执行
    C# 结构体介绍
    论文阅读笔记(二)——Mask R-CNN
  • 原文地址:https://blog.csdn.net/weixin_62529383/article/details/126012518