写在这里的初衷,一是备忘,二是希望得到高人指点,三是希望能遇到志同道合的朋友。
//先输入10个地区的面积 #include<stdio.h> int main() { int a[10]; int i; printf("请输入10个面积:\n"); for (i = 0; i <= 9; i++) scanf_s("%d", &a[i]); printf("\n"); return 0; }
例1
#include<stdio.h> int main() { int i, a[10] = { 0,1,2,3,4,5,6,7,8,9 }; for (i = 9; i >= 0; i--) printf("%d\t", a[i]); printf("\n"); return 0; }
运行结果:
练习
选择法/冒泡法(沉底法)
//选择法 #include<stdio.h> int main() { int a[10]; int i,j,t; printf("请输入10个面积:\n"); for (i = 0; i <= 9; i++) scanf_s("%d", &a[i]); for(i=0;i<=8;i++) for (j = i+1; j <= 9; j++) if(a[i]>a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } printf("排序后的面积依次是:\n"); for (i = 0; i <= 9; i++) printf("%d\t", a[i]); printf("\n"); return 0; }
//冒泡法(沉底法) #include<stdio.h> int main() { int a[10]; int i,j,t; printf("请输入10个面积:\n"); for (i = 0; i <= 9; i++) scanf_s("%d", &a[i]); for(j=0;j<=8;j++) for (i = 0; i <= 8-j; i++) if(a[i]>a[i+1]) { t = a[i]; a[i] = a[i+1]; a[i+1] = t; } printf("排序后的面积依次是:\n"); for (i = 0; i <= 9; i++) printf("%d\t", a[i]); printf("\n"); return 0; }
练习
#include<stdio.h> int main() { int a[2][3] = { {1,2,3},{4,5,6} }; int b[3][2], i, j; for (i = 0; i <= 1; i++) for (j = 0; j <= 2; j++) b[j][i] = a[i][j]; printf("数组b为:\n"); for (j = 0; j <= 2; j++) { for (i = 0; i <= 1; i++) printf("%d\t", b[j][i]); printf("\n"); } return 0; }
运行结果:
例2
#include<stdio.h> int main() { int i, j, max; int a[3][3] = { {1,2,3},{9,8,7},{-10,10,-5} }; max = a[0][0]; for (i = 0; i <= 2; i++) { for (j = 0; j <= 2; j++) if (a[i][j] > max) max = a[i][j]; } printf("最大元素的值为%d\n",max); return 0; }
运行结果:
1.挨个输出printf("%c",) 2.整体输出printf("%s",)或者puts()
例3
#include<stdio.h> int main() { char c[15] = { 'H','o','u','B','o','S','h','i',' ','S','h','u','a','i','!' }; int i; for (i = 0; i <= 14; i++) printf("%c", c[i]); //挨个输出字符 printf("\n"); return 0; }
运行结果:
例4
#include<stdio.h> int main() { char c[3][3] = { {' ','*',' '},{'*',' ','*'},{' ','*',' '} }; int i, j; for (i = 0; i <= 2; i++) { for (j = 0; j <= 2; j++) printf("%c", c[i][j]); printf("\n"); } return 0; }
运行结果:
例5
#include<stdio.h> int main() { char c[15] = { 'H','o','u','B','o','S','h','i',' ','S','h','u','a','i','!' }; printf("%s\n", c); return 0; }
#include<stdio.h> int main() { char c[15] = { 'H','o','u','B','o','S','h','i',' ','S','h','u','a','i','!' }; puts(c); return 0; }
在这里需要注意:\0
\0后边的字符不会被输出来
定义字符的时候养成一个好习惯:在字符最后加一个’\0’
#include<stdio.h> int main() { char c[16] = { 'H','o','u','B','o','S','h','i',' ','S','h','u','a','i','!','\0' }; puts(c); return 0; }
1.挨个单词输入 scanf("%s%s...",) 2.整体输入gets()
例6
#include<stdio.h> int main() { char c1[12],c2[12],c3[12]; scanf("%s%s%s", c1, c2, c3); return 0; }
#include<stdio.h> int main() { char c[18]; gets_s(c); return 0; }
练习
//程序1 #include<stdio.h> int main() { char c[81]; int i, dancishu = 1; gets_s(c); for (i = 0; c[i] != '\0'; i++) if (c[i] = ' '&&c[i + 1] != ' ') dancishu++; return 0; } //遇到第一位是空格,需要单词数=0
//程序2 #include<stdio.h> int main() { char c[81]; int i, dancishu = 1; gets_s(c); if (c[0] == ' ') //解决程序1存在的问题 dancishu = 0; for (i = 0; c[i] != '\0'; i++) if (c[i] = ' '&&c[i + 1] != ' ') dancishu++; return 0; } //最后两位是:空格+\0,会多计算一个单词数
//程序3 #include<stdio.h> int main() { char c[81]; int i, dancishu = 1; gets_s(c); if (c[0] == ' ') dancishu = 0; for (i = 0; c[i] != '\0'; i++) if (c[i] = ' '&&c[i + 1] != ' '&&c[i+1]!='\0') //解决程序2的问题 dancishu++; printf("有%d个单词",dancishu); return 0; }
以上是自己通过学习整理所得,大家有相关问题,可以留言,一起交流学习!
转载或者引用本文内容请注明来源及原作者
参考
《C语言》4小时不挂科【猴博士爱讲课】——第4~7课