Posted 2020-10-10Updated 2020-10-11Data Structure / C2 minutes read (About 264 words)DS_1_1_Recursion数据结构_chapter 1_1.1_递归(recursion)1. 递归实现阶乘12345678910111213141516#include<stdio.h>double factorial(unsigned int number) //数据类型 double 可用于阶乘运算较宽数值范围乘积的存储{ if(number<=0){ //此处应修改为: number<=1 ,若为 number<=0, 则会重复一次乘1操作 return 1; } return number*factorial(number-1); } int main(){ unsigned int number; scanf("%d",&number); printf("%d的阶乘结果为:%f\n",number,factorial(number)); return 0; } 2. 递归实现斐波那契数列前 ‘n’ 项输出123456789101112131415161718192021222324#include<stdio.h>int fibonaci(int i){ if(i==1) //数列第一项 { return 0; } if(i==2) //数列第二项 { return 1; } return fibonaci(i-1) + fibonaci(i-2);}int main(){ int number,i; scanf("%d",&number); for(i=1;i<=number;i++) { printf("%d\t\n",fibonaci(i)); } return 0;} 3. 递归实现从’1~n’整型数据顺序输出非递归方法: 1234567891011121314151617#include<stdio.h>void PrintN(int N){ int i; for(i=1;i<=N;i++) printf("%d\n",i); return;}int main(){ int N; scanf("%d",&N); PrintN(N); return 0;} 递归方法: 123456789101112131415#include<stdio.h>void PrintN(int N){ if(N>0){ PrintN(N-1); printf("%d\n",N); }}int main(){ int N; scanf("%d",&N); PrintN(N); return 0;} or 1234567891011121314151617#include<stdio.h>void PrintN(int N);int main(){ int N; scanf("%d",&N); PrintN(N); return;}void PrintN(int N){ if(N>0){ PrintN(N-1); printf("%d\n",N); }}
Posted 2020-10-08Updated 2020-10-08Programming / C2 minutes read (About 345 words)6_array_data_processing_26.数组处理批量数据6.4 将整型数组元素行列位置对调1234567891011121314151617181920212223242526#include<stdio.h>int main() { int a[2][3]={{1,2,3},{4,5,6}}; int b[3][2],i,j; printf("array a:\n"); for(i=0;i<=1;i++) //遍历二维数组a 元素并赋值于b;遍历行 { for (j=0;j<=2;j++) //遍历数组a 列 { printf("%5d",a[i][j]); b[j][i]=a[i][j]; //数组a中元素与数组b中 元素对调(行、列) } printf("\n"); } printf("array b:\n"); for(i=0;i<=2;i++) //遍历数组b,循环结构for遍历行 { for(j=0;j<=1;j++) //遍历列 printf("%5d",b[i][j]); printf("\n"); } return 0;} 6.5 将整型数组中最大值元素及相应行列位置输出12345678910111213141516#include<stdio.h>int main(){ int i,j,row=0,colum=0,max; int a[3][4]={{1,2,3,4},{9,8,7,6},{-10,10,-5,2}}; max=a[0][0]; for(i=0;i<=2;i++) //数组遍历,行遍历 for(j=0;j<=3;j++) // 数组遍历,列遍历 if(a[i][j]>max) //“打擂台”算法 {max=a[i][j]; row=i; colum=j; } printf("max=%d\nrow=%d\ncolum=%d\n",max,row,colum); //最大值输出、行/列号输出 return 0; } 6.6 字符数组输出特定字符12345678910#include<stdio.h>int main(){ char c[15]={'I',' ','a','m',' ','a',' ','s','t','u','d','e','n','t','.'}; int i; for(i=0;i<15;i++) printf("%c",c[i]); printf("\n"); return 0; } 6.7 用星号(*)输出钻石图案12345678910111213#include<stdio.h>int main(){ char diamond[][5]={{' ',' ','*'},{' ','*',' ','*',' '},{'*',' ',' ',' ','*'},{' ','*',' ','*',' '},{' ',' ','*',' ',' '}}; //定义字符数组元素初始值,5行5列 int i,j; for(i=0;i<5;i++) //遍历数组元素,行遍历 { for(j=0;j<5;j++) //遍历数组元素,列遍历 printf("%c",diamond[i][j]); printf("\n"); } return 0;}
Posted 2020-10-07Updated 2020-10-08Programming / C2 minutes read (About 338 words)6_array_data_processing6.数组处理批量数据6.1 为数组赋值0~9,并将数组元素逆序输出123456789101112#include<stdio.h>int main(){ int i,a[10]; //定义整型变量i ,整型数组a for(i=0;i<=9;i++) //循环for为数组元素赋值 a[i]=i; for(i=9;i>=0;i--) //循环for 将数组元素逆序输出 printf("%d",a[i]); printf("\n"); return 0;} 6.2 输出斐波那契数列第2~21项(斐波那契数列:0,1,1,2,3,5…)12345678910111213141516171819#include<stdio.h>int main(){ int i; int f[20]={1,1}; //定义整型数组,第1,2项均为1 for(i=2;i<20;i++) //循环结构for为整型数组第3至20项赋值 f[i]=f[i-2]+f[i-1]; for(i=0;i<20;i++) { if(i%5==0) printf("\n"); printf("%12d",f[i]); //输出数组元素值,每数值占12列且右对齐 } printf("\n"); return 0;} 6.3 自定义10个数值,将数值按升序形式输出(冒泡排序法)1234567891011121314151617181920#include<stdio.h>int main(){ int a[10]; int i,j,t; printf("input 10 numbers :\n"); for(i=0;i<10;i++) //循环结构for 读取数据并存储 scanf("%d",&a[i]); printf("\n"); for(j=0;j<9;j++) //重复趟数总计9次,遍历每趟 for(i=0;i<9-j;i++) // 每趟内遍历数组元素 if(a[i]>a[i+1]) {t=a[i];a[i]=a[i+1];a[i+1]=t;} //条件判断,若前一项大于后一项,进行数值交换 printf("the sorted numbers :\n"); for(i=0;i<10;i++) //循环结构输出sorted 后的数组元素 printf("%d",a[i]); printf("\n"); return 0; }