*
从0到1,回归基础,夯实基础
点滴积累,成就未来
*
#include "stdio.h"
#include "stdlib.h"
#define _CRT_SECURE_NO_WARNINGS
//for逆序
int main1()
{
int i, a[10];
for (int i = 0; i < 10; i++)
a[i] = i;
for (i = 9; i >=0; i--)//i--从i=9开始
printf("%d ", a[i]);
}
//Fibonacci,第三个数是前两个数的和
int main2()
{
int i;
int a[20] = { 1, 1 };//初始化
for (i = 2; i < 20; i++)//i从2开始
a[i] = a[i - 1] + a[i - 2];
for (i = 0; i < 20; i++)
{
if (i % 5 == 0)//每输出5个换行
printf("\n");
printf("%12d", a[i]);
}
}
//冒泡法排序
//思想:每次讲相邻两个数进行比较,把较小的数放在前头;
int main3()
{
int i, j, t;
int a[10];
printf("please input 10 numbers:\n");
for (int i = 0; i < 10; i++)
scanf_s("%d", &a[i]);
printf("\n");
for (i = 0; i < 10-1; i++)//如果有n个数,进行n-1次循环
for (j = 0; j < 10 - 1 - i; j++)//每趟进行n-1-i次比较
{
if (a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
printf("the sorted numbers:\n");
for (int i = 0; i < 10; i++)
printf("%d ", a[i]);
}
int main4()
{
//数组元素互换
int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
int b[3][2], i, j;
printf("array a :\n");
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
{
printf("%5d", a[i][j]);
b[j][i] = a[i][j];//互换
}
printf("\n");
printf("array b :\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
printf("%5d ", b[i][j]);
printf("\n");
}
}
//求数组最大值,并标记行号列号
int main5()
{
int i, j, row = 0, col = 0, max;
int a[3][4] = { { 1, 2, 3, 4 }, { 8, 6, 5, 4 }, { 11, -3, 43, 2 } };
max = a[0][0];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
if (max < a[i][j])
{
max = a[i][j];
row = i+1;
col = j+1;
}
printf("max = %d ,row = %d , col=%d\n", max, row, col);
}
//字符串函数联系
int main6()
{
char str1[20];
char str2[] = "china";
char str3[] = "abcdef";
char str4[] = "ERDDdef";
printf("%s\n", strcpy(str1, str2));
//printf("%s\n", strcat(str1, str3));
//printf("%s\n", str1);
printf("%s\n", strncpy(str1, str3, 1));
printf("%d\n", strcmp(str1, str3));//比较 = 返回0; > 返回1; < 返回-1;
printf("%d\n", strlen(strncpy(str1, str3, 1)));
printf("%s\n", strlwr(str4));//字符串转小写
printf("%s\n", strupr(str4));//字符串转大写
}
//函数调用
int main7()
{
int a, b, c;
printf("input 2 number:\n");
scanf_s("%d%d", &a, &b);
c = n_Max(a, b);
printf("%d\n", c);
}
int n_Max(int x, int y)
{
//int z;
//z = x > y ? x : y;
//return z;
return(x > y ? x : y);
}
int four_Max(int a, int b, int c, int d)
{
//int m;
//m = n_Max(a, b);//递推求大值
//m = n_Max(m, c);
//m = n_Max(m, d);
return n_Max(n_Max(n_Max(a, b), c), d);
}
int main8()
{
int a, b, c, d, max;
scanf_s("%d%d%d%d", &a, &b, &c, &d);
max = four_Max(a, b, c, d);
printf("max number is %d\n", max);
}
//n!递归
int fac(int n)
{
int f;
if (n < 0)
printf("date error!");
else if (n == 0 || n == 1)
f = 1;
else
f = n*fac(n - 1);
return f;
}
int main9()
{
int n, y;
printf("input n :\n");
scanf_s("%d", &n);
y = fac(n);
printf("%d!=%d\n", n, y);
}
//hanoi 递归
void move(char x, char y)
{
printf("%c-->%c\n", x, y);
}
void hanoi(int n, char one, char two, char three)
{
if (n == 1)
move(one, three);
else
{
hanoi(n - 1, one, three, two);
move(one, three);
hanoi(n - 1, two, one, three);
}
}
int main10()
{
int m;
printf("input the number of diskes:\n");
scanf_s("%d", &m);
if (m > 0)
hanoi(m, 'A', 'B', 'C');
}
//数组元素做实参
int main11()
{
int i,num;
int str[10];
int max = 0;
printf("input 10 number :\n");
for (int i=0; i < 10; i++)
scanf_s("%d", &str[i]);
max = str[0];
for (int i=1; i < 10; i++)
{
/*if (n_Max(max, str[i])>max)
max = str[i];*/
if (max < str[i])
max = str[i];
num = i;
}
printf("the largest number is %d,and is the %dth number.\n", max, num);
}
float average(float array[],int n)//数组做形参,传递数组的值
{
int i;
float ave, sum = 0;
for (i = 0; i < n; i++)
sum += array[i];
ave = sum / n;
return ave;
}
int main12()
{
int i, n;
float aver,score[10];
printf("input n number :\n");
scanf_s("%d", &n);
printf("input %d score :\n",n);
for (i = 0;i< n; i++)
scanf_s("%f", &score[i]);
aver = average(score,n);//数组名做实参,退化为指针,向形参传递数组的首个元素的地址
printf("average is :%5.2f\n", aver);
}
//二维数组。二维数组不管是做形参还是做实参,列都不可以省略;
//数组在内存中行存放,所以必须指定每行的长度;
//选择法排序
void sort(int array[], int n)
{
int i,j,k,t;
for (i = 0; i < n-1; i++)//n-1次循环
{
k = i;
for (j = i + 1; j < n;j++)//j从第i+1个起比较
{
if (array[j] < array[k])
k = j;
t = array[k];
array[k] = array[i];
array[i] = t;
}
}
}
int main13()
{
int i;
int a[10];
printf("input 10 number :\n");
for (i = 0; i < 10; i++)
scanf_s("%d", &a[i]);
sort(a, 10);
printf("the sorted number :\n");
for (i = 0; i < 10; i++)
{
if (i % 5 == 0)
printf("\n");
printf("%5d ", a[i]);
}
}
float Max = 0, Min = 0;//全局变量//类型一定要一致
float average1(float array[],int n)
{
int i;
float aver, sum = array[0];
Max = array[0];
Min = array[0];
for (i = 1; i < n; i++)//从1开始.
{
if (Max < array[i])
Max = array[i];
else if (Min > array[i])//
Min = array[i];
sum += array[i];
}
aver = sum / n;
return aver;
}
int main()
{
float aver,score[10];
int i;
printf("input 10 number :\n");
for (i = 0; i < 10; i++)
scanf_s("%f", &score[i]);
aver = average1(score, 10);
//全局变量
printf("max=%5.2f,min=%5.2f,average=%5.2f\n", Max, Min, aver);
system("pause");
return 0;
}