zoukankan      html  css  js  c++  java
  • 第八次作业

      一、实验内容   

    1.输入若干名学生的成绩(输入人数或用负数结束均可),求其平均分,最高分和最低分,并指出最高分和最低分的学生(给出下标即可),输入一个成绩,说明是第几个学生的成绩。

    1.1 代码:

    #include <stdio.h>
    int main()
    {
    	int a,i,sum=0,max,min,score[100],d,e;
    	double average;
    	printf("please input a number
    ");
    	scanf("%d",&a);
    	printf("input the score:
    ");
    	for(i=0;i<a;i++)
    	{
    		scanf("%d",&score[i]);
    		sum+=score[i];
    	}
    		average=(double)sum/a;	
    		max=score[0];
    		int b;
    	for(i=0;i<a;i++) 
    	{
    		if(score[i]>max)
    		{
    			max=score[i];
    				b=i;
    		}
    	}
    		min=score[0];
    		int c;
    	for(i=0;i<a;i++)
    	{
    		if(score[i]<min)
    		{
    			min=score[i];
    					c=i;
    		}
    	} 
    	printf("请输入你要查找的成绩
    ");
    	scanf("%d",&d);
    	for(i=0;i<a;i++)
    	{
    		if(d==score[i])
    		{
    			e=i;
    			printf("the number you look for is %d
    ",e);
    		}
    	}
    	printf("the average is %.2f
    the max is %d
    the min is %d
    max number is %d
    min number is %d
    ",average,max,min,b,c);
    	return 0;
    } 
    

    1.2 程序运行结果:

    2.现有一个有序正整数数组(从小到大排序),输入一个数,插入到数组中,要求插入后的数组仍然有序。对插入到最前、最后、中间位置三种情况进行验证。

    2.1 代码:

    #include <stdio.h>
    int main()
    {
    	int score[11]={1,3,5,7,9,11,13,15,17,19},i,n,a,b;
    	printf("原始数据为:1,3,5,7,9,11,13,15,17,19
    ");
    	printf("请输入一个数字
    ");
    	scanf("%d",&i);
    	for(n=0;n<10;n++)
    	{
    		if(score[n]>i)
    		{
    			a=n;
    			break; 
    		}
    		else
    		{
    			a=10;
    		}
    	}
    	for(n=10;n>a;n--)
    	{
    		score[n]=score[n-1];
    	}
    	score[a]=i;
    	for(n=0;n<11;n++)
    	{
    		printf("%3d",score[n]);
    	}
    	return 0;
    }
    

    2.2 程序运行结果:

    3.用数组实现火柴棍等式

    3.1 代码:

    #include <stdio.h>
    int main ()
    {
    	int match[10]={6,2,5,5,4,5,6,3,7,6};
    	int a,b,c,sum;
    	for(a=0;a<10;a++)
    	{
    		for(b=0;b<10;b++)
    		{
    			c=a+b;
    			if(c>=9)
    			{
    				break;
    			}
    			sum=match[a]+match[b]+match[c];
    			if(sum==12)
    			{
    				printf("%d+%d=%d
    ",a,b,c);
    			}
    		}
    	}
    	return 0;
    } 
    

      3.2 程序运行结果:

     二、知识点总结

     

    1.数组应用时要合理,分清楚是先定义了元素个数好还是直接定义合适

    2.程序有多种写法,要尽量找到简单的方法

    3.注意各种语句的嵌套以及程序的合法性验证

    三、实验总结

    1.第一题在判断之前要先输入要判断的值,而且注意输出与定义变量的类型要保持一致

    2.第二题判断的时候要注意赋值

    3.第三题注意数字为0的情况也要考虑到,数好每个数字分别需要几根火柴。

  • 相关阅读:
    mac c++编译出现segmentation fault :11错误
    ssh 连接缓慢解决方法
    237. Delete Node in a Linked List
    203. Remove Linked List Elements
    Inversion of Control Containers and the Dependency Injection pattern
    82. Remove Duplicates from Sorted List II
    83. Remove Duplicates from Sorted List
    SxsTrace
    使用CCleaner卸载chrome
    decimal and double ToString problem
  • 原文地址:https://www.cnblogs.com/wei530/p/6102005.html
Copyright © 2011-2022 走看看