数组
一、降序一个数组
/* 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 : "); 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(" "); 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 ",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: "); for(i = 0; i < 5; i++) for(j = 0; j < 5; j++) scanf("%d",&a[i][j]); printf(" "); 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 ",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(" "); } 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(" "); } } /* 总结: 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: "); 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); 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 —————————————————————————— */
九、将数组中数字元素放于非数字元素的后面
/* 2017年7月1日18:04:34 将数组中数字元素放于非数字元素的后面 */ #include"stdio.h" int main() { int n = 0; char a[100]; char b[100]; char c[100]; printf("please input a string: "); gets(a); char *pa = a; char *pb = b; char *ppb = b; char *pc = c; while(*pa) { if(*pa >= '1'&& *pa <= '9') { *pb = *pa; pb++; n++; } else { *pc = *pa; pc++; } pa++; } *pb = ' '; while(n--) { *pc = *ppb; pc++; ppb++; } *pc = ' '; puts(c); return 0; }
十、将数组中数字元素后面插入*
/* 2017年7月1日18:04:03 将数组中数字元素后面插入* */ #include"stdio.h" int main () { int n = 0; char a[100]; char b[100]; char c[100]; printf("please input a string : "); gets(a); char *pa = a; char *ppa = a; char *pb = b; char *ppb = b; char *pc = c; while(*pa) { if((*pa >= '1')&&(*pa <= '9')) { *pb = '*'; pb++; n++; } pa++; } *pb = ' '; while(*ppa) { *pc = *ppa; if((*pc >='1')&&(*pc <= '9')) { pc++; *pc = *ppb; pb++; } pc++; ppa++; } *pc = ' '; puts(c); return 0; }
十一、计算数组中所有元素的平均数,将低于平均数的数值输出
/* 2017年7月1日18:03:27 计算数组中所有元素的平均数,将低于平均数的数值输出 */ #include"stdio.h" int main() { int m, i, j = 0, sum = 0, aver = 0; int a[100], b[100]; printf("请输入需要统计人数的个数:"); scanf("%d",&m); for(i = 0; i < m; i++) { printf("请输入%d个人的分数:",i+1); scanf("%d",&a[i]); sum += a[i]; } aver = sum / m; for(i = 0; i < m; i++) { if(a[i] < aver) b[j++] = a[i]; } for(i = 0; i < j; i++) { printf("%d ",b[j]); } return 0; }
十二、找出数组中最大的数据
/* 2017年7月1日18:02:06 找出数组中最大的数据 */ #include"stdio.h" #define M 100 int main () { int i, max; int a[M]; for(i = 0; i < M; i++) { printf("please input %dth the number: ",i+1); scanf("%d",&a[i]); if(a[i] == 0) break; } max = a[0]; for(int j = 0; j < i; j++) { if(a[j] > max) max = a[j]; } printf("the result is %d ",max); return 0; }
十三、找出指定数到100之间,并且能够被指定数整除所有的质数
/* 2017年7月1日18:01:01 找出指定数到100之间,并且能够被指定数整除所有的质数 */ #include"stdio.h" int main() { int m, i, j = 0; int a[100]; printf("please input a number: "); scanf("%d",&m); for(i = m; i < 100; i++) { if((i % m == 0)&&(i % 2 != 0)) a[j++] = i; } for(i = 0; i < j; i++) { printf("%d ",a[i]); } printf(" "); return 0; }
十四、将数组中偶数位的元素循环逆置
/* 2017年3月13日08:42:48 功能:将数组中偶数位的元素循环逆置 */ #include"stdio.h" #include"string.h" #define N 100 int main() { int m = 0; int n = 0; int x = 0; int y = 0; int q = -1; char a[N]; char b[N]; char c[N]; char d[N]; printf("please input a string :"); gets(a); for(int i = 0; a[i] != ' ';i++) { if(i % 2 == 0) b[m++] = a[i]; else c[n++] = a[i]; b[m] = ' '; c[n] = ' '; } while( x < strlen(a)) { d[x++] = b[y++]; d[x++] = c[((q++)+n)%n]; } d[x] = ' '; if(strlen(a) % 2 == 0) puts(d); else { d[x-1] = ' '; puts(d); } } /* 总结: 在VC++6.0中显示的结果: ———————————————————— 1、please input a string :12345678 18325476 2、please input a string :123456789 183254769 ———————————————————— */
十五、交换两个数组对应的值
/* 2017年3月13日10:40:04 功能:交换两个数组对应元素的值 */ #include"stdio.h" int main(void) { int n = 0, m = 0, i; int a[100] = {1,3,5,7,9}; int b[100] = {2,4,6,8,10,12,14,16}; int *ppa = a; int *pa = a; int *ppb = b; int *pb = b; int t; while(*ppa) { n++; ppa++; } while(*ppb) { m++; ppb++; } while(*pa && *pb ) { t = *pa; *pa = *pb; *pb = t; pa++; pb++; } for ( i = 0; i < n; i++) { printf("%d ",a[i]); } printf (" "); for ( i = 0; i < m; i++) { printf("%d ",b[i]); } printf (" "); return 0; } /* 总结: 在VC++6.0中显示的结果: —————————————————————— 2 4 6 8 10 1 3 5 7 9 12 14 16 —————————————————————— */
十六、升序,奇数在前,偶数在后
/* 2017年3月14日12:52:39 功能:升序,奇数在前,偶数在后 */ #include "stdio.h" int main () { int j = 0, k = 0; int a[10]; int b[10]; int c[10]; int *pa = a; printf("please input 10 number: "); for (int i = 0; i < 10; i++) { printf("please input %dth number: ",i+1); scanf("%d",&a[i]); if(a[i] % 2 ==0) b[j++] = a[i]; else c[k++] = a[i]; } int *pb = b; int *pc = c; for(int p = 0; p < j; p++) for(int p1 = p; p1 < j; p1++) //此处不能定义int p1 = 1; if(b[p] >= b[p1]) b[p1] = b[p]; for(int q = 0; q < k; q++) for(int q1 = q; q1 < k; q1++) if(c[q] >= c[q1]) c[q1] = c[q]; while(j) //此处不能用while(*pc)语句跳出循环 { *pa = *pc; pa++; pc++; j--; } while(k) { *pa = *pb; pa++; pb++; k--; } for (i = 0; i < 10; i++) printf("%d ",a[i]); return 0; } /* 总结: 在VC++6.0中显示的结果:: —————————————————————————— please input 10 number: 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 please input 7th number: 7 please input 8th number: 8 please input 9th number: 9 please input 10th number: 10 1 3 5 7 9 2 4 6 8 10 —————————————————————————— */
十七、在a数组后面添加b数组的值
/* 2017年3月12日14:07:05 功能:在a数组后面添加b数组的值 */ #include"stdio.h" int main() { int i = 0; char a[20]; char b[20]; char *pa = a; char *pb = b; printf("请输入字符串a:"); gets(a); printf("请输入字符串b:"); gets(b); //使用gets()函数时,最后元素不需要添加结尾标识符' ' while (b[i++] != ' ') { ; } int string_b_len = i; //求出b[]数组的长度 while (*pa) { pa++; } //将a[]数组的指针移到末尾 if (string_b_len < 5) { for (int n = 0; n < string_b_len; n++) { *pa = *pb; pa++; pb++; } puts(a); } else if (string_b_len >= 5) { for (int n = 0; n < 5; n++) { *pa = *pb; pa++; pb++; } puts(a); } } /* 总结: 在VC++6.0中显示的结果: —————————————————————— 请输入字符串a:asdfghjkl 请输入字符串b:dft asdfghjkldft —————————————————————— */
十八、在a数组后面添加b数组的值(数组实现)
源代码程序(数组实现): /* 2017年3月12日14:07:05 功能:在a数组后面添加b数组的值(数组实现) */ #include"stdio.h" int main() { int j = 0; int i = 0; char a[20]; char b[20]; printf("请输入字符串a:"); gets(a); printf("请输入字符串b:"); gets(b); while (b[i] != ' ') { i++; } while (a[j] != ' ') { j++; } if (i < 5) { for (int x = 0; x < i; x++) { a[j++] = b[x]; } a[j] = ' '; } else if (i >= 5) { for (int y = 0; y < 5; y++) { a[j++] = b[y]; } a[j] = ' '; } puts(a); } /* 总结: 在VC++6.0中显示的结果: —————————————————————— 请输入字符串a:asdfghjkl 请输入字符串b:dft asdfghjkldft —————————————————————— */