46.把数组元素依次向后移动一个位置
#include <stdio.h>
#include <math.h>
int main() {
int a[11];
for (int i = 0; i <= 10; i++) {
a[i] = i;
printf("%d ", a[i]);
}
printf("\n");
for (int i = 10; i >= 0; i--) {
a[i] = a[i - 1];
}
for (int i = 0; i <= 10; i++) {
printf("%d ", a[i]);
}
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
0 1 2 3 4 5 6 7 8 9 10
0 0 1 2 3 4 5 6 7 8 9
Process finished with exit code 0
47.输入一组数据,找出最大值
#include <stdio.h>
#include <math.h>
int main() {
int a[10], max;
for (int i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
max = a[0];
for (int i = 1; i < 10; i++) {
if (max < a[i])
max = a[i];
}
printf("max=%d", max);
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
12
3
2
4
5
6
3453
123
1
1
max=3453
Process finished with exit code 0
48.输入一组数据,颠倒之后再输出
#include <stdio.h>
#include <math.h>
int main() {
int a[10], t;
for (int i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
printf("原始顺序:\n");
for (int i = 0; i <= 9; i++) {
printf("%d ", a[i]);
}
for (int i = 0; i < 9 / 2; i++) {
t = a[i];
a[i] = a[9 - i];
a[9 - i] = t;
}
printf("\n交换之后的顺序:\n");
for (int i = 0; i <= 9; i++) {
printf("%d ", a[i]);
}
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
1
2
3
4
5
6
7
8
9
0
原始顺序:
1 2 3 4 5 6 7 8 9 0
交换之后的顺序:
0 9 8 7 5 6 4 3 2 1
Process finished with exit code 0
49.冒泡排序
#include <stdio.h>
#include <math.h>
int main() {
int a[10] = {9, 7, 2, 5, 4, 1}, t;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
if (a[j] > a[j + 1]) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
for (int i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
1 2 4 5 7 9
Process finished with exit code 0
50.选择排序
#include <stdio.h>
#include <math.h>
int main() {
int a[6] = {9, 7, 2, 5, 4, 1}, k, t;
for (int i = 0; i < 5; i++) {
k = i;
for (int j = k + 1; j < 6; j++) {
if (a[k] > a[j]) {
k = j;
}
}
if (k != i) {
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
for (int i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
return 0;
}
#include <stdio.h>
#include <math.h>
int main() {
int a[6] = {9, 7, 2, 5, 4, 1}, k, t;
for (int i = 0; i < 5; i++) {
k = 0;
for (int j = 1; j < 6-i; j++) {
if (a[k] < a[j]) {
k = j;
}
}
if (k != 5-i) {
t = a[5-i];
a[5-i] = a[k];
a[k] = t;
}
}
for (int i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
1 2 4 5 7 9
Process finished with exit code 0
51.有一个已经排好序的数值,要求输入一个数之后,按原来排序的规律将他插入数组中
#include <stdio.h>
#include <math.h>
int main() {
int a[9] = {10, 20, 30, 40, 50, 60, 70, 80}, x, p = 8;
printf("输入要插入的数:");
scanf("%d", &x);
for (int i = 0; i < 8; i++) {
if (x < a[i]) {
p = i;
break;
}
}
for (int i = 8; i > p; i--) {
a[i] = a[i - 1];
}
a[p] = x;
for (int i = 0; i < 9; i++) {
printf("%d ", a[i]);
}
return 0;
}
输入:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
输入要插入的数:78
10 20 30 40 50 60 70 78 80
Process finished with exit code 0
52.在数组中删除一个数
#include <stdio.h>
#include <math.h>
int main() {
int a[8] = {10, 20, 30, 40, 50, 60, 70, 80}, x, j, i,flag=0;
printf("输入要删除的数:");
scanf("%d", &x);
for (i = 0, j = 0; i < 8; i++) {
if (a[i] != x) {
a[j] = a[i];
j++;
}else{
flag=1;
}
}
if(flag==1)
a[8-1]=0;
for (int k = 0; k < 8; k++) {
printf("%d ", a[k]);
}
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
输入要删除的数:10
20 30 40 50 60 70 80 0
Process finished with exit code 0
53.二维矩阵进行行列倒置
#include <stdio.h>
#include <math.h>
int main() {
int a[3][4],b[4][3];
for (int i = 0; i <3 ; i++) {
for (int j = 0; j <4 ; j++) {
scanf("%d",&a[i][j]);
}
}
for (int i = 0; i <3 ; i++) {
for (int j = 0; j <4 ; j++) {
b[j][i]=a[i][j];
}
}
for (int i = 0; i <3 ; i++) {
for (int j = 0; j <4 ; j++) {
printf("%3d ",a[i][j]);
}
printf("\n");
}
printf("\n");
for (int i = 0; i <4 ; i++) {
for (int j = 0; j <3 ; j++) {
printf("%3d ",b[i][j]);
}
printf("\n");
}
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
1
2
3
4
5
6
7
8
9
0
1
2
1 2 3 4
5 6 7 8
9 0 1 2
1 5 9
2 6 0
3 7 1
4 8 2
Process finished with exit code 0
54.找出矩阵最大值,并输出行号和列号
#include <stdio.h>
#include <math.h>
int main() {
int a[3][4] = {{5, 2, 0, 9},
{3, 7, 12, 6},
{19, 4, 1, 8}}, max, col, row;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (a[i][j] > max) {
max = a[i][j];
row = i;
col = j;
}
}
}
printf("max=%d\n", max);
printf("max=a[%d][%d]", row, col);
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
max=19
max=a[2][0]
Process finished with exit code 0
55.折半查找
#include <stdio.h>
#include <math.h>
int main() {
int a[6] = {-2, 3, 6, 8, 12, 15}, bottom, top, mid, x;
scanf("%d", &x);
bottom = 0;
top = 5;
while (bottom <= top) {
mid = (bottom + top) / 2;
if (a[mid] == x) {
printf("Find! a[%d]=%d\n", mid, x);
break;
} else if (x > a[mid]) {
bottom = mid + 1;
} else {
top = mid - 1;
}
}
if (bottom > top)
printf("No find!\n");
return 0;
}
输出:
/Users/wuyanbo/Documents/ClionProject/cmake-build-debug/ClionProject
-2
Find! a[0]=-2
Process finished with exit code 0
56.456789321
#include<stdio.h>
int main()
{ int t;
int a[9]= {1,2,3,4,5,6,7,8,9};
for(int j=0; j<4; j++) {
t=a[0];
for(int i=1; i<9-j; i++) {
a[i-1]=a[i];
}
a[9-j-1]=t;
}
for(int i=0; i<9; i++)
printf("%d",a[i]);
return 0;
}
#include<stdio.h>
int main()
{
int a[]= {1,2,3,4,5,6,7,8,9,0},i,j,t;
for(i=0; i<3; i++) {
t=a[0];
for(j=0; j<10-i-2; j++)
a[j]=a[j+1];
a[10-i-2]=t;
}
for(i=0; i<10; i++)
printf("%d",a[i]);
return 0;
}
57.进制转换
#include<stdio.h>
int converse(int n)
{
int i=0;
char a[20],b[]="0123456789ABCDEF";
while(n) {
a[i++]=b[n%16];
n=n/16;
}
for(--i; i>=0; i--)
printf("%c",a[i]);
}
int main()
{
converse(200);
return 0;
}
58.打印杨辉三角
#include <stdio.h>
#include <stdlib.h>
const int length = 10;// 定义杨辉三角的大小
int main()
{
int nums[length][length];
int i, j;
/*计算杨辉三角*/
for(i=0; i<length; i++)
{
nums[i][0] = 1;
nums[i][i] = 1;
for(j=1; j<i; j++)
nums[i][j] = nums[i-1][j-1] + nums[i-1][j];
}
/*打印输出*/
for(i=0; i<length; i++)
{
for(j=0; j<length-i-1; j++)
printf(" ");
for(j=0; j<=i; j++)
printf("%-5d ", nums[i][j]);
putchar('\n');
}
}
59.字母加密解密
加密:
#include<stdio.h>
#include<ctype.h>
int main()
{ char ch;
while((ch=getchar())!='\n') {
if(isupper(ch))
ch=(ch-'A'+4)%26+'A';
if(islower(ch))
ch=(ch-'a'+4)%26+'a';
printf("%c",ch);
}
return 0;
}
解密:
#include<stdio.h>
#include<ctype.h>
int main()
{ char ch;
while((ch=getchar())!='\n') {
if(isupper(ch))
ch=(ch-'A'+22)%26+'A';
if(islower(ch))
ch=(ch-'a'+22)%26+'a';
printf("%c",ch);
}
return 0;
}
60.递归二分查找
#include<stdio.h>
int find(int n[],int x,int low,int high) {
if(high>=low) {
int mid = (low + high)/2;
if(x>n[mid])
return find(n,x,mid+1,high);
else if(x<n[mid])
return find(n,x,low,mid-1);
else
return 1;
} else {
return 0;
}
}
int main() {
int a[]= {1,2,4,5,8,9,10};
printf("%d",find(a,8,0,6));
return 0;
}