intmain() { int arr[] = {10, 2, 8, 7, 1, 5, 4, 3, 9, 6}; int sz = sizeof(arr) / sizeof(arr[0]); int i, j; for (i = 0; i < sz - 1; i++) { int min = i; for (j = i + 1; j < sz; j++) { if (arr[j] < arr[min]) { min = j; } } if (i != min) { int temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; } } for (i = 0; i < sz; i++) { printf("%d ", arr[i]); }
// 1 1 2 3 5 8 13 21 34 #include<stdio.h> ⭐ intmain() { long f1, f2; int i; f1 = f2 = 1; for (i = 1; i <= 20; i++) { printf("%12ld %12ld", f1, f2); // 控制输出,每行四个 if (i % 2 == 0) { printf("\n"); } // 前两个月加起来赋值给第三个月 f1 = f1 + f2; f2 = f1 + f2; } return0; }
intmain() ⭐ { long f1, f2, f; int i; f1 = f2 = 1; printf("%12ld %12ld", f1, f2); for (i = 3; i <= 40; i++) { f = f1 + f2; f1 = f2; f2 = f; printf("%12ld ", f2); if (i % 4 == 0) printf("\n"); } return0; }
intmain() ▲ { long f1, f2; int i; f1 = f2 = 1; printf("%12ld %12ld", f1, f2); for (i = 3; i <= 40; i++) { // 前两个月加起来赋值给第三个月f2 f2 = f1 + f2; // f1作为第二个月 f1 = f2 - f1; printf("%12ld ", f2); if (i % 4 == 0) { printf("\n"); } } return0; }
数组法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<stdio.h> intmain() { int a[40] = {1, 1}; int i; printf("%12d%12d", a[0], a[1]); for (i = 2; i < 40; i++) { if (i % 4 == 0) { printf("\n"); } a[i] = a[i - 1] + a[i - 2]; printf("%12d", a[i]); } return0; }
intmain() { char str[100] = "A A A A A "; // gets(str); int i = 0; int word = 0; int num = 0; while (str[i] != '\0') { if (str[i] == ' ') { word = 0; } elseif (word == 0) { num++; word = 1; } i++; } printf("%d", num);
#define ROW 5 #define COL 5 intmain() { int arr[ROW][COL] = {0}; int i, j; int max, min; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { scanf("%d", &arr[i][j]); } }
for (i = 0; i < ROW; i++) { max = 0; for (j = 1; j < COL; j++) { if (arr[i][max] < arr[i][j]) { max = j; } } min = 0; for (j = 0; j < ROW; j++) { if (arr[j][max] < arr[i][min]) { min = j; } } if (arr[i][max] == arr[max][min]) { printf("%d", arr[i][max]); } }
return0; }
三角形面积和周长
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<stdio.h> #include<math.h> intmain() { int a,b,c; float area,perimeter,s; scanf("%d %d %d",&a,&b,&c); s = (a+b+c)/2.0; area = (sqrt(s*(s-a)*(s-b)*(s-c))); perimeter = a+b+c; if (a+b>c && a+c>b && b+c>a) { printf("area = %.2f; perimeter = %.2f",area,perimeter); } else printf("These sides do not correspond to a valid triangle"); return0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intmain() { //在圆内:高度10,不在圆内:高度0; int r = 1; printf("请输入任意一点的坐标:"); int a, b; int x, y; float c; scanf("%d %d", &a, &b); x = fabs(a); y = fabs(b);//先将a和b的坐标取绝对值 c = sqrt((x - 2) * (x - 2) + (y - 2) * (y - x));//sqrt是求平方根公式,c是求点到圆心的距离为多少,公式为根号下x1-x2的平方+y1-y2的平方 if (c > r) printf("0"); else printf("10");//当求出的点到圆心的距离小于等于半径,说明在圆塔上,高度为10 return0; }
voidparitition(int *arr, int n){ int left = 0; int right = n - 1; while(left < right){ while(left < right && arr[left] < 0){ left ++; } while(left < right && arr[right] > 0){ right --; } if(left < right){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } }
intmain() { int arr[9] = {-5,3,2,-1,4,-3,-4,5,9}; paritition(arr, 9); for(int i = 0; i < 9; i ++){ cout<<arr[i]<<" "; } cout<<endl; return0; }
voidhanoi(int n, char a, char b, char c) { if (n == 1) { printf("%c --> %c\n", a, c); //只有1块的时候,直接从 a 移动到 c } else { hanoi(n - 1, a, c, b); //将 n - 1 块从 a 移到到 c,借助 b printf("%c --> %c\n", a, c); //把 a 中剩下的那一块,移动到 c hanoi(n - 1, b, a, c); //将 n - 1 块从 b 移动到 c,借助 a } }
intmain() { float sn = 100.0, hn = sn / 2; int n; for (n = 2; n <= 10; n++) { // 第n次落地时共经过的米数 sn = sn + 2 * hn; // 第n次反跳高度 hn = hn / 2; } printf("the total of road is %f\n", sn); printf("the tenth is %f meter\n", hn); return0; }
#include<stdio.h> voidmain() { int i, j, k, sum = 0, n = 1, m, a = 0; printf("输入累加到几位数:"); scanf("%d", &j); printf("输入累加数字:"); scanf("%d", &k); for (i = 1; i <= j; i++) { sum = sum + k * n; // 也可以是sum=sum*10+k; n = n * 10; a = a + sum; } printf("该数为%d\n", a); }
有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前20项之和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
intmain() { int n, t, number = 20; float a = 2, b = 1, s = 0; for (n = 1; n <= number; n++) { s = s + a / b; t = a; a = a + b; b = t; } printf("sum is %9.6f\n", s); return0; }