码迷,mamicode.com
首页 > 编程语言 > 详细

02、数组

时间:2017-07-01 17:20:58      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:操作   [1]   功能   logs   oid   函数   for   实现   负数   

数组

一、降序一个数组

/*
	2017年3月17日09:53:46
	功能:降序一个数组中的元素
*/
#include"stdio.h"
#define N 10
int main ()
{
	int i, j, t;									
	int a[N];

	printf("please input a array : \n");
	for (i = 0; i < N; i++)
	{
		printf("please input the %dth number: ", i+1);
		scanf("%d",&a[i]);
	}
	
	for(i = 0; i < N; i++)
		for (j = i+1; j < N; j++)								//实现交换,相同下标元素之间是对角线关系
		{
			if(a[i] <= a[j])
			{
				t = a[i];
				a[i] = a[j];
				a[j] = t;
			}

		}

	for (i = 0; i < N; i++)
		printf("%d  ",a[i]);
	printf("\n");

	return 0;
}
/*
	总结:
	在VC++6.0中显示的结果:
	————————————————————
	please input a array :
	please input the 1th number: 1
	please input the 2th number: 2
	please input the 3th number: 3
	please input the 4th number: 4
	please input the 5th number: 5
	please input the 6th number: 6
	please input the 7th number: 7
	please input the 8th number: 8
	please input the 9th number: 9
	please input the 10th number: 10
	10  9  8  7  6  5  4  3  2  1
	————————————————————
*/

 

二、在升序的整型数组中插入一个数据仍为升序

/*
	2017年3月17日11:05:33
	功能:在升序的整型数组中插入一个数据仍为升序
*/
#include"stdio.h"
#define N 5
int main ()
{
	int i, j, m, t;
	int a[N];
	printf("please input a array: ");
	for (i = 0; i < N; i++)
	{
		printf("please input %dth number: ", i+1);
		scanf("%d",&a[i]);
	}
	printf("please input a number: ");
	scanf("%d",&m);
	for (i = 0; i < N; i++)
		for(j = i; j < N; j++)
		{
			if(a[i] >= a[j])
			{
				t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}
	for (i = 0; i < N; i++)
	{
		if(a[i] >= m && a[i-1] <=m)
			break;
	}
	for(j = N-1; j > i; j--)
	{
		a[j+1] = a[j];
	}
	a[j] = m;
	return 0;
}

  

三、在一个数组中插入一个数仍然有序

/*
	2017年7月1日15:32:27
	在一个数组中插入一个数仍然有序
*/
#include"stdio.h"
void insert (int, int*);
void insert1(int, int*, int);
int main()
{
	int insertNum;
	int a[100] = {2,4,6,8,10,12};
	printf("请输入一个数,保证有序序列继续有序:");
	scanf("%d",&insertNum);
	insert(insertNum, a);
	printf("%d %d %d %d %d %d %d\n",a[0],a[1],a[2],a[3],a[4],a[5],a[6]);
	return 0;
}

void insert (int insertNum, int *a)
{
	if(insertNum >= a[5])
	{
		a[6] = insertNum;
	}
	else if (insertNum > a[0])
	{
		for(int i =0; i < 6; i++)
		{
			if(insertNum >= a[i] && insertNum <= a[i+1] )
			{
				insert1(i+1, a, insertNum);
				break;
			}
		}
	}
	else if(insertNum <= a[0])
	{
		insert1(0, a, insertNum);
	}
}

void insert1(int i,int*a,int insertNum)
{
	for(int j = 5; j >= i;j--)
	{
		a[j+1] = a[j];
	}
	a[i] = insertNum;
}
/*
	在VC++6.0中显示的结果:
	——————————————————————
	请输入一个数,保证有序序列继续有序:4
	2 4 4 6 8 10 12
	——————————————————————
*/

 

四、对角线上的各元素求和,以及对角线上行,列下标均为偶数的各元素之和

/*
	2017年3月15日19:38:29
	功能:对角线上的各元素求和,以及对角线上行,列下标均为偶数的各元素之和
*/
#include"stdio.h"
int main ()
{
	int i, j, sum = 0, mul = 1, a[5][5];							//循环输入5*5的矩阵数组
	printf("input 5*5 array:\n");
	for(i = 0; i < 5; i++)
		for(j = 0; j < 5; j++)
			scanf("%d",&a[i][j]);

	printf("\n");

	for (i = 0; i < 5; i++)											//对行数进行确定,对哪些元素进行操作,是在行确定的基础下进行的
	{
		sum += a[i][i];												//对左对角线元素进行累加
	if (i != 2)														//对右对角线上的元素进行累加(对角线中间元素除外)
		sum +=a[i][4-i];
	if (i % 2 != 0)													//如果行下标为奇数,进入下一次循环
		continue;
	mul *= a[i][i];													//对左对角线元素进行累乘
	if (i != 2)
		mul *=a[i][4-i];											//对右对角线上的元素进行累乘(对角线中间元素除外

	}

	printf ("sum = %d mul = %d\n",sum, mul);

	return 0;
}
/*
	总结:
	在VC++6.0中显示的结果:
	————————————————————————————
	input 5*5 array:
	7 2 7 4 8
	9 3 5 7 4
	8 3 5 6 7
	4 8 5 3 5
	2 4 8 9 1

	sum = 44 mul = 560
	————————————————————————————
*/

五、循环输出方阵

/*
	2017年3月14日20:48:38
	功能:循环输出方阵
*/
#include"stdio.h"
int *fun(int *a,int n);
int main ()
{
	int a[10] ;
	int n, m;
	printf("please input some number: ");
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		printf("please input %dth number:",i+1);
		scanf("%d",&a[i]);
	}
	m = n;
	while(m--)
	{
		fun(a,n);
		for (int j = 0; j < 6; j++)
			printf("%d  ",a[j]);
		printf("\n");
	}
	return 0;
}
int *fun(int *a,int n)
{

	int m, t;
	m = n-1;
	t = a[m];
	while(n--)
	{
		a[m] = a[m-1];
		m--;
	}
	a[0] = t;

	return a;
}
/*
	总结:
	在VC++6.0中显示的结果:
	————————————————————————
	please input some number: 6
	please input 1th number:1
	please input 2th number:2
	please input 3th number:3
	please input 4th number:4
	please input 5th number:5
	please input 6th number:6
	6  1  2  3  4  5
	5  6  1  2  3  4
	4  5  6  1  2  3
	3  4  5  6  1  2
	2  3  4  5  6  1
	1  2  3  4  5  6
	————————————————————————
	
*/

 

七、杨辉三角形

/*
	2017年3月16日08:03:31
	功能:杨辉三角形
*/
#include"stdio.h" 
#define N 6											//宏定义
void main ()
{
	int i, j, a[N][N];								//定义变量

	for (i = 0; i < N; i++)							//外循环代表输入所在行的元素
	{
		a[i][0] = 1;								//行首元素的值
		a[i][i] = 1;								//行尾元素的值
		for (j = 1; j < i; j++)						//此for()循环是确定此行中在其他列中的元素
			a[i][j] = a[i-1][j-1] + a[i-1][j];
	}

	for (i = 0; i < N; i++)							//显示结果
	{
		for (j = 0; j < N-i-1; j++)					//在数的前面添加相应的空格
			printf(" ");
		for (j = 0;j <= i; j++)
			printf("%2d ",a[i][j]);					//此时输出存入对应下标的元素
		printf("\n");
	}
}
/*
	总结:
	1、设计思想:对于有6行的杨辉三角形,可以用一个6行6列的二维数组a[6][6]来表示。对于第i行的元素:a[i][0]=1,a[i][i]=1(i=0,1,2,...,5),
	a[i][j] = a[i-1][j-1]+a[i-1][j](j=1,2,...,i-1).然后显示二维数组中的计算的结果,其中第1行的显示1个数据。。。第6行的显示6 个数据
	2、for(i = 0; i < N; i++)
			for(j = 0; j <= i; j++)
		这样的双for()循环的用处是给矩阵左下元素赋值
	3、for(i = 0; i < N; i++)
			for(j = i; j < N; j++)
		这样的双for()循环的用处是给矩阵右上元素赋值
	4、for(i = 0; i < N; i++)
			for(j = i; j <= i ; j++)
		这样的双for()循环的用处是给矩阵主对角线元素赋值
	5、for(i = 0; i < N; i++)
			for(j = N-i; j >= N-i; j--)
		这样的双for()循环的用处是给矩阵负对角线元素赋值
	6、在VC++6.0中显示的结果:
	————————————————————————————————
	      1
		 1  1
		1  2  1
	   1  3  3  1
	  1  4  6  4  1
	 1  5 10 10  5  1
	————————————————————————————————
*/

 

八、统计非负数之和 

/*
	2017年3月16日11:28:56
	功能:统计非负数之和
*/
#include "stdio.h"
int main ()
{
	int m, n = 0, sum = 0;
	int a[100];

	printf("please input 20 number: \n");
	for (int i = 0; i < 20; i++)
	{
		printf("the number is %dth : ", i+1);
		//a[i] = scanf("%d", &m);							//scanf()函数输入成功返回1,error	
		scanf("%d",&a[i]);									//此为正确输入的格式
		if (a[i] >= 0)										//满足条件执行if()语句中的内容,否则执行i++
		{	
			sum += a[i];
			n++;
		}
	}

	printf(" n = %d\n",n);

	return 0;
}
/*
	总结
	在VC++6.0中显示的结果:
	——————————————————————————
	please input 20 number:
	the number is 1th : 1
	the number is 2th : 2
	the number is 3th : 3
	the number is 4th : 4
	the number is 5th : 5
	the number is 6th : 6
	the number is 7th : 7
	the number is 8th : -4
	the number is 9th : 5
	the number is 10th : 3
	the number is 11th : 3
	the number is 12th : 3
	the number is 13th : 3
	the number is 14th : 3
	the number is 15th : 3
	the number is 16th : 3
	the number is 17th : 3
	the number is 18th : 3
	the number is 19th : 3
	the number is 20th : 3
	n =	19
	——————————————————————————
*/

  

 

  

  

  

02、数组

标签:操作   [1]   功能   logs   oid   函数   for   实现   负数   

原文地址:http://www.cnblogs.com/wxt19941024/p/7102745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!