tags:C
C语言浅整理
by – catherine
从简单代码开始
1.求根
#include<stdio.h>
int main() {
int a, b, c;
float root;
scanf("%d%d%d", &a, &b, &c);
root = (2*b - sqrt(b*b*0.0 - 4*a*b*0.0))/(2.0*a);
printf("%f\n", root);
return 0;
}
2.蚂蚁问题:
已知在长为l的路上右n只蚂蚁,第一行输入n 和 l , 接下来右n行, 每一行有两个输入,分别为蚂蚁的走向和蚂蚁距离最左端的距离, 且蚂蚁相撞后方向爬行(提示:蚂蚁方向与直接穿过有何不同)。
#include<stdio.h>
int main() {
int n, l, i, b[20];
char a[20];
scanf("%d %d", &n, &l);
for (i = 0; i < n; i++) {
getchar();// 因为第一个输入是字符,要避免回车的影响,scanf不会读回车
scanf("%c %d", &a[i], &b[i]);
if (a[i] == 'R') b[i] = l - b[i];
}
int max = b[0];
for (i = 1; i < n; i++)
if (b[i] > max) max = b[i];
printf("%d\n", max);
return 0;
}
3.楼梯问题:
已知用n阶楼梯,每次可以走一阶或者是两阶,问总共有多少中走法
此题有m组测试数据
(1)循环做法
#include<stdio.h>
#include<stdlib.h>
int num(int n) {
int result, i;
int num1, num2;
num1 = 1; num2 = 2;
if (n == 1||n == 2) return n;
for (i = 3; i <= n; i++) {
result = num1 + num2;
num1 = num2;
num2 = result;
}
return result;
}
int main() {
int i, j, n, m;
scanf("%d", &m);
for (i = 0; i < m; i++) {
scanf("%d", &n);
printf("%d\n", num(n));
}
return 0;
}
(2)递归做法
#include<stdio.h>
#include<stdlib.h>
int num(int n) {
if (n == 1||n == 2) return n;
else return num(n - 1) + num(n - 2);
}
int main() {
int m , n, i;
scanf("%d", &m);
for (i = 0; i < m; i++) {
scanf("%d", &n);
printf("%d\n", num(n));
}
return 0;
}
4.杨辉三角:
输入为一个数n ,表示输出右n行
#include<stdio.h>
int main() {
int n, a[30][30], i, j;
scanf("%d", &n);
for (i = 0; i < n; i++) {
a[i][0] = a[i][i] = 1;
for (j = 1; j < i; j++)
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
printf("%d\t", a[i][j]);
if (i == j) printf("\n");
}
}
return 0;
}
5.二元系数
此题与上面的杨辉三角有着密切的联系,其实也是杨辉三角的实际应用
(x+y)的n次方,求x的a次方和y的b次方对应的系数
#include<stdio.h>
int main() {
int n, m, i, j, f[300][300];
scanf("%d%d", &n, &m);
for (i = 0; i <= n + m; i++) {
f[i][0] = f[i][i] = 1;
for (j = 1; j < i; j++)
f[i][j] = (f[i - 1][j] + f[i - 1][j - 1])%10007;
}
printf("%d\n", f[n + m][n]);
return 0;
}
6.switch case
简单代码,输入数字的星期几,输出英文的星期几
#include<stdio.h>
int main() {
int n;
while (scanf("%d", &n) != EOF) {
switch(n) {
case 0: printf("Sunday\n"); break;
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wendnesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
default:
printf("error input\n");
}
}
return 0;
}
7.桶排序
#include<stdio.h>
#include<stdlib.h>
int main() {
int i, j, k, count, a[100], t;
i = 0;
while (scanf("%d", &a[i]) != EOF) {
a[i] %= 21;
i++;
}
count = i;
for (i = 0; i < count - 1; i++)
for (j = count - 1; j > i; j--) {
if (a[j] < a[j - 1]) {
t = a[j];
a[j] = a[j - 1];
a[j - 1] = t;
}
}
for (i = 0; i < count; i++)
printf("%d " , a[i]);
printf("\n");
return 0;
}
8.字符串的合并
利用指针,动态声明内存的malloc知识,注意长度要加1,因为u回车需要占用一个字符。指针与数组的混合使用。
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char *join(char *a, char *b, int l1, int l2) {
char *e = malloc(sizeof(a[0])*(l1 + l2 + 1));
int i;
for (i = 0; i < l1; i++)
*(e + i) = a[i];
for (i = l1; i < l1 + l2; i++)
*(e + i) = b[i - l1];
*(e + l1 + l2) = '\0';
return e;
}
// memset(a, 0, sizeof(a));
int main() {
char a[20], b[20];
scanf("%s%s", a, b);
char *c = join(a, b, strlen(a), strlen(b));
printf("%s\n", c);
return 0;
}
乐而学,学而乐,最简单的学习,最深的快乐