C语言题目复习前7章重点程序

时间:2021-06-28 02:15:22
/**
#include <stdio.h>
#include <stdlib.h> int max(int n1, int n2)
{
return (n1 > n2) ? n1 : n2;
} int main()
{
int a, b, c;
if(a == max(a, b))
{
if(a == max(a, c))
printf("%d\n", a);
else if(c == max(b, c))
printf("%d\n", c);
else
printf("%d\n", b);
}
return 0;
}
*/
/**失误代码告诉我,不要尝试抖机灵。。。,越写越垃圾。。。*/
///能正确实现功能的程序,就是好程序
/**
#include <stdio.h>
#include <stdlib.h> int main()
{
int a, b, c;
scanf("%d %d %d",&a, &b, &c);
if(a > b)
{
if(a > c)
printf("%d\n", a);
else{
printf("%d\n", c);
}
}else{
if(b > c)
printf("%d\n", b);
else{
printf("%d\n", c);
}
}
return 0;
}
*/
/**
#include <stdio.h>
#include <stdlib.h> int judge_year(int y)
{
if((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
return 1;
return 0;
} int main()
{
int year;
scanf("%d", &year);
if(judge_year(year))
{
puts("leap year!");
}else{
puts("nonleap year");
}
return 0;
}
*/
/**
#include <stdio.h>
#include <stdlib.h> double sum_decimals(int n)
{
int i;
double sum = 0;
for(i = 1; i <= n; ++i)
if(i % 2 == 0)
sum += -(1.0/i);
else
sum += 1.0/i;
return sum;
} int main()
{
int n;
scanf("%d", &n);
printf("%f\n", sum_decimals(n));
return 0;
}
*/
/**
///要注意的地方是,乘号,不要忘记了,从数学公式到程序,这个容易丢
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int main()
{
int a, b, c;
double area, p;
scanf("%d %d %d", &a, &b, &c);
if(a+b > c && b+c > a && a+c > b)
{
p = (a+b+c+0.0)/2;
area = sqrt(p*(p-a)*(p-b)*(p-c));
printf("%f\n", area);
}else{
printf("Do not construct triangle");
}
return 0;
}
*/
/**
//char int 存储数据时的区别
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int main()
{
char c1 = 197, c2 = 198;
printf("%c %c\n", c1, c2);
printf("%d %d", c1, c2);
return 0;
}
*/ /**
//逻辑运算符的结合性都是自左向右的,而且逻辑运算符有短路特性
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int judge(int n, int a, int b, int c)
{
int x, y;
if(n == 1)
{
if(a || b+c && b-c)
return 1;
}else if(n == 2){
if(!(a>b) && !c || 1)
return 1;
}else if(n == 3){
if(!(x=a) && (y=b) && 0)
return 1;
}else if(n == 4){
if(!(a+b)+c-1 && b+c/2)
return 1;
}
return 0;
} int main()
{
int i;
for(i = 1; i < 5; ++i)
{
if(judge(i, 3, 4, 5))
puts("yes");
else
puts("no");
}
return 0;
}
*/ /**
///在switch case语句中的要点,在switch括号中的变量,只能是int / char类型
///在case后边的,只能是对应的int / char值
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int main()
{
int score;
scanf("%d", &score);
switch(score/10)
{
case 10: puts("A"); break;
case 9 : puts("B"); break;
case 8 : puts("C"); break;
default: puts("D");
}
return 0;
}
*/ /**
///递归写的时候,注意递归基,和递归结束条件。把握好这两点,递归就行
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int fabonacci(int n)
{
if(n == 1 || n == 2)
return 1;
else
return fabonacci(n-1)+fabonacci(n-2);
} int main()
{
printf("%d %d\n", 8, fabonacci(8));
return 0;
}
*/ /**
///这道题没什么好说的,很简单
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int num(int i, int base)
{
int j;
for(j = 0; j < i-1; ++j)
{
base = base*10 + base%10;
printf("%d\n", base);
}
return base;
} int main()
{
int n = 5, sum = 0, i;
for(i = 1; i <= 5; ++i)
{
sum += num(i, 2);
printf("%d\n", sum);
}
return 0;
}
*/ /**
//书上竟然有这样的题目。。。 用int型的话,阶乘达不到20!,就会超出int范围,所以用long long型,注意,long long型输出用 %lld
#include <stdio.h>
#include <stdlib.h>
#include <math.h> long long factorial(long long n)
{
long long i , fac = 1;
for(i = 1; i <= n; ++i)
{
fac = fac * i;
}
return fac;
} long long main()
{
long long sum = 0, i;
for(i = 1; i <= 20; ++i)
{
sum += factorial(i);
printf("%lld\n", sum);
}
return 0;
}
*/ /**
///久违的水仙花数
///写程序的时候注意,如果函数的形参可以不动,尽量不动。因为改动了之后,就找不回了,程序运行完成之后,它会被释放掉
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int cude(int n)
{
return n*n*n;
} int judge_num(int n)
{
int bit[3], i = 0, tmp = n;
while(tmp)
{
bit[i] = tmp % 10;
tmp /= 10;
i++;
}
if(n == (cude(bit[0]) + cude(bit[1]) + cude(bit[2])))
{
return 1;
}
return 0;
} int main()
{
int i;
for(i = 100; i < 1000; ++i)
{
if(judge_num(i))
printf("%d is!\n", i);
}
return 0;
}
*/ /**
//不说了,很简单
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int judge(int n)
{
int i, fac_sum = 0;
for(i = 1; i < n; ++i)
{
if(n % i == 0)
fac_sum += i;
}
if(n == fac_sum)
return 1;
return 0;
} int main()
{
int i;
for(i = 1; i <= 1000; ++i)
{
if(judge(i))
{
printf("%d!\n", i);
}
}
return 0;
}
*/ /**
///斐波那契数列的变形
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int main()
{
int i, n1 = 1, n2 = 1, tmp;
double sum = 0;
for(i = 0; i < 20; ++i)
{
tmp = n1 + n2;
printf("%f\n", (tmp+0.0) / n2);
sum += (tmp+0.0) / n2;
n1 = n2;
n2 = tmp;
}
printf("%f\n", sum);
return 0;
}
*/ /**
///牛顿迭代法求根,这个要记住
///这个程序,除了记住牛顿迭代法的方法外,还要注意, double类型要用 %lf 进行读入
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double my_sqrt(double n)
{
double a1 = 2, a2 = 0, tmp;
while(fabs(a1 - a2) > 0.00001)
{
a2 = 0.5*(a1 + n/a1);
//printf("%f\n", n);
//printf("%f %f %f\n", a1, a2, tmp);
tmp = a2;
a2 = a1;
a1 = tmp;
}
return a1;
}
int main()
{
double n1;
while(scanf("%lf", &n1) != EOF)
printf("%f\n", my_sqrt(n1));
return 0;
}
*/ /**
///注意,字符串的大小是根据 “字典序” 来定义的,这里我用指针写了写
#include <stdio.h>
#include <string.h> int judge_str(char *str1, char *str2)
{
int i, len1 = strlen(str1), len2 = strlen(str2);
for(i = 0; i < len1 && i < len2; ++i)
{
if(*(str1 + i) < *(str2 + i))
return 1;
else if(*(str1 + i ) > *(str2 + i))
return -1;
}
return 0;
} int main()
{
int i,j;
char str[3][10] = {"china", "japan", "indin"}, *max;
for(i = 0; i < 3; ++i)
{
for(j = i+1; j < 3; ++j)
{
if(judge_str(str[i], str[j]) == 1)
{
max = str[i];
}
}
}
puts(max);
return 0;
}
*/ /**
///简单选择排序
#include <stdio.h>
#include <stdlib.h>
#include <math.h> void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
} void select_sort(int *arr, int n)
{
int i, j, index;
for(i = 0; i < n; ++i)
{
index = i;
for(j = i+1; j < n; ++j)
{
if(arr[j] > arr[index])
{
index = j;
}
}
if(index != i)
{
swap(&arr[i], &arr[index]);
}
}
} int main()
{
int i, arr[10] = {1, 4, 5, 6, 8, 7, 9, 0, 2, 3};
select_sort(arr, 10);
for(i = 0; i < 10; ++i)
{
printf("%d ", arr[i]);
}
puts("");
return 0;
}
*/ /**
///冒泡排序
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void bubble_sort(int *arr, int n)
{
int i, j;
for(i = 0; i < n; ++i)
{
for(j = i+1; j < n; ++j)
{
if(arr[i] > arr[j])
swap(&arr[i], &arr[j]); ///上边写的,这里就错了,总是忘了 '&' 符号,注意,注意
}
}
} int main()
{
int i, arr[10] = {1, 4, 5, 6, 8, 7, 9, 0, 2, 3};
bubble_sort(arr, 10);
for(i = 0; i < 10; ++i)
{
printf("%d ", arr[i]);
}
puts("");
return 0;
return 0;
}
*/ /**
///将一个数,插入到已经有序的数组中
#include <stdio.h>
#include <stdlib.h>
#include <math.h> void insert_num(int *arr, int n, int num)
{
int i, j;
for(i = 0; i < n; ++i)
{
if(num < arr[0])
break;
if(num >= arr[i] && num < arr[i+1]) ///注意这里的写法,
break;
} for( j = n; j > i; --j)
{
arr[j] = arr[j-1];
}
arr[i] = num;
} void print_arr(int *arr, int n)
{
int i;
for(i = 0; i < n; ++i)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[10] = {2, 3, 4, 5, 5, 7, 8};
insert_num(arr, 7, 9);
print_arr(arr, 8);
return 0;
}
*/ /**
///将数组中的数,逆序排放
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void print_arr(int *arr, int n)
{
int i;
for(i = 0; i < n; ++i)
{
printf("%d ", arr[i]);
}
} void inverst_arr(int *arr, int n)
{
int i;
for(i = 0; i < n/2; ++i)
{
swap(&arr[i], &arr[n-i-1]);
}
} int main()
{
int arr[10] = {2, 3, 4, 5, 5, 7, 8};
inverst_arr(arr, 10);
print_arr(arr, 10);
return 0;
}
*/ /**
///杨辉三角,前10行
#include <stdio.h>
#include <stdlib.h>
#include <math.h> void printf_yh_triangle()
{
int i, j, arr[10][10] = {{1}, {1,1}};
printf("%d\n", arr[0][0]);
for(i = 1; i < 10; ++i)
{
for(j = 0; j <= i; ++j)
{
if(j == 0 || j == i)
{
arr[i][j] = 1;
printf("%d ", arr[i][j]);
}
else{
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
printf("%d ", arr[i][j]);
}
}
puts("");
}
} int main()
{
printf_yh_triangle();
return 0;
}
*/ /**
///1~n*n 构成的魔方阵
///dfs 的写法 4*4 就跑不动了。。。。。。
///其他写法呢?? -> http://blog.csdn.net/cmutoo/article/details/5487157
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 3
int vis[10000]; int judge(int arr[][N], int n)
{
int i, j, a[10] = {0}, b[10] = {0}, x1 = 0, x2 = 0;
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
a[i] += arr[i][j];
}
}
for(i = 0; i < n-1; ++i)
{
if(a[i] != a[i+1])
return 0;
}
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
b[i] += arr[j][i];
}
}
for(i = 0; i < n-1; ++i)
{
if(b[i] != b[i+1])
return 0;
}
for(i = 0; i < n; ++i)
{
x1 += arr[i][i];
}
for(i = 0; i < n; ++i)
{
x2 += arr[n-i-1][i];
}
if(a[0] == b[0] && b[0] == x1 && x1 == x2)
return 1;
return 0;
} void print_dfs(int arr[][N])
{
int i, j;
for(i = 0; i < N; ++i)
{
for(j = 0; j < N; ++j)
{
printf("%d ", arr[i][j]);
}
puts("");
}
puts("");
} void dfs(int arr[][N], int n, int num)
{
int row = num / n;
int col = num % n;
int i;
if(row > n-1)
return;
for(i = 1; i <= N*N; ++i)
{
if(!vis[i] && arr[row][col] == 0)
{
vis[i] = 1;
arr[row][col] = i;
if(row == col && row == n-1 && judge(arr, N) == 1)
{
print_dfs(arr);
}
dfs(arr, n, num+1);
arr[row][col] = 0;
vis[i] = 0;
}
}
} int main()
{
int i, brr[N][N] = {0}; //arr[3][3] = {{8,1,6}, {3,5,7}, {4,9,2}};
memset(vis, 0, sizeof(vis));
for(i = 1; i <= N*N; ++i)
{
dfs(brr, N, i-1);
}
return 0;
}
*/ ///奇幻方阵的构造方法
/**
奇数阶幻方
最经典的填法是罗伯特法(楼梯法),填写方法是这样:
把1(或最小的数)放在第一行正中;按以下规律排列剩下的n×n-1个数:
(1)每一个数放在前一个数的右上一格;
(2)如果这个数所要放的格已经超出了顶行那么就把它放在底行,仍然要放在右一列;
(3)如果这个数所要放的格已经超出了最右列那么就把它放在最左列,仍然要放在上一行;
(4)如果这个数所要放的格已经超出了顶行且超出了最右列,那么就把它放在前一个数的下一行同一列的格内;
(5)如果这个数所要放的格已经有数填入,处理方法同(4)。
这种写法总是先向“右上”的方向,象是在爬楼梯。
三阶幻方:
8 1 6
3 5 7
4 9 2
*/
// 解奇阶幻方的通用模块
///写了这么多,也就这点价值高**********************************************************
/**
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 100
int main()
{
int arr[N][N] = {0};
int n, x, y, i, j;
scanf("%d", &n);
x = n / 2;
y = 0;
for(i = 1; i <= n*n; ++i)
{
arr[y][x] = i;
if(i%n == 0)
y++;
else
{
x++;
y--;
}
x = (x%n + n) % n; ///存在负数时,取模的方法
y = (y%n + n) % n;
} for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
{
printf("%d ", arr[i][j]);
}
puts("");
}
puts("");
return 0;
}
*/
/**
///找一个二位数组中的鞍点(可能不存在)
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int judge(int arr[][4], int num, int row, int col)
{
int i = 0;
for(i = 0; i < 4; ++i)
{
if(num < arr[row][i])
return 0;
if(num > arr[i][col])
return 0;
}
return 1;
} int main()
{
int i, j, max, min;
int arr[4][4] = {{1,2,3,4}, {5,6,7,8}, {2,3,4,5}, {6,7,8,9}};
for(i = 0; i < 4; ++i)
{
for(j = 0; j < 4; ++j)
{
if(judge(arr, arr[i][j], i, j))
{
printf("%d %d %d\n",i, j, arr[i][j]);
}
}
}
return 0;
}
*/
/**
///二分查找,写最简单的就好了
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int binary_find(int *arr, int n, int num)
{
int low, high, mid;
low = 0;
high = n-1; while(low < high)
{
//printf("%d %d %d\n", low, mid, high);
mid = (low+high) / 2;
if(arr[mid] == num)
return mid+1;
else if(num > arr[mid])
low = mid;
else
high = mid;
}
return -1;
} int main()
{
int arry[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; int index = binary_find(arry, 20, 4);
if(index)
{
printf("%d\n", index);
}else{
puts("Not Found!");
}
return 0;
}
*/
/**
///P.169 连接两个字符串,不用strcat
#include <stdio.h>
#include <stdlib.h>
#include <math.h> int get_str_len(char *str)
{
int len = 0;
while(str[len] != '\0')
len++;
return len;
}
///源码写法,好强
char *str_cat(char *s1, const char *s2)
{
char *s;
for(s = s1; *s != '\0'; ++s)
; for(; (*s = *s2) != '\0'; ++s, ++s2)
;
return s1;
}
int main()
{
char str1[100], str2[100];
scanf("%s %s", str1, str2);
///将str2 连接在 str1的后边
int i, j;
int len1 = get_str_len(str1);
int len2 = get_str_len(str2);
for(i = len1, j = 0; j < len2;)
{
str1[i++] = str2[j++];
}
str1[i] = '\0';
puts(str1);
//--------------
str_cat(str1, str2);
puts(str1);
return 0;
}
*/
/**
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int get_str_len(char *str)
{
int len = 0;
while(str[len] != '\0')
len++;
return len;
} ///学会这种写法,很nice
int str_cmp(const char *str1,const char *str2)
{
//不可用while(*str1++==*str2++)来比较,当不相等时仍会执行一次++,
//return返回的比较值实际上是下一个字符。应将++放到循环体中进行。
// while(*str1 == *str2)
// {
// if(*str1 == '\0')
// return 0;
//
// str1++;
// str2++;
// }
///用for写的近似于ASCI C源码
for(;*str1 == *str2; ++str1, ++str2)
if(*str1 == '\0')
return 0;
return *str1 - *str2;
} int main()
{
char str1[100], str2[100];
gets(str1);
gets(str2); int len1 = get_str_len(str1);
int len2 = get_str_len(str2); printf("%d\n", str_cmp(str1, str2));
return 0;
}
*/
/**
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
///源码写法,简直优美!!
char *str_cpy(char *s1, const char *s2)
{
char *s;
for(s = s1; (*s++ = *s2++) != '\0'; )
;
return s1;
} int main()
{
char str1[100], str2[100];
gets(str2);
str_cpy(str1, str2);
puts(str1);
return 0;
}
*/
/**
///练练hanoi塔的写法,会写还是很简单的
///课本P 190
#include <stdio.h>
void move(char a, char c)
{
printf("%c -> %c\n", a, c);
} void hanoi(int n, char a, char b, char c)
{
if(n == 1)
move(a, c);
else{
hanoi(n-1, a, c, b);
move(a, c);
hanoi(n-1, b, a, c);
}
} int main()
{
char A = 'A', B = 'B', C = 'C';
hanoi(3, A, B, C);
return 0;
}
///搞定
*/
/**
///求最大公约数,最小公倍数,
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
///这种实现很简单
int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b, a%b);
} int lcm(int a, int b)
{
return (a*b)/(gcd(a, b));
} int main()
{
int n1, n2;
scanf("%d %d", &n1, &n2);
printf("%d %d\n", gcd(n1, n2), lcm(n1, n2));
return 0;
}
*/ /**
///将矩阵转置
#include <stdio.h>
#include <stdlib.h>
#include <math.h> void trans(int arr[3][3])
{
int i, j, tmp;
for(i = 0; i < 3; ++i)
{
for(j = i; j < 3; ++j)
{
tmp = arr[j][i];
arr[j][i] = arr[i][j];
arr[i][j] = tmp;
}
}
} int main()
{
int i,j;
int a[3][3] = {0,1,2,3,4,5,6,7,8};
trans(a);
for(i = 0; i < 3; ++i)
{
for(j = 0; j < 3; ++j)
printf("%d ", a[i][j]);
puts("");
}
return 0;
}
*/
/**
///字符串反转
#include <stdio.h>
#include <stdlib.h>
#include <math.h> void str_reversed(char *str)
{
int i;
char tmp;
int len = strlen(str);
for(i = 0; i < len/2; ++i)
{
tmp = str[len-i-1];
str[len-i-1] = str[i];
str[i] = tmp;
}
} int main()
{
char str[] = "qwerqwer";
str_reversed(str);
puts(str);
return 0;
}
*/ /**
///统计字符串中各种元素的个数
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int num1 = 0, num2 = 0, num3 = 0; void statistics(char *str)
{
while(*str != '\0')
{
if((*str >= 'a' && *str <= 'z') ||
(*str >= 'A' && *str <= 'Z'))
{
num1 ++;
} else if((*str >= '0' && *str <= '9'))
{
num2 ++;
} else if (*str == ' '){
num3 ++;
}
str++;
}
} int main()
{
char str[] = "China is a beautiful country 23333!";
statistics(str);
printf("%d %d %d\n", num1, num2, num3);
return 0;
}
*/
/**
///冒泡排序,最基本的交换排序
#include <stdio.h>
#include <stdlib.h>
#include <math.h> void bubble_sort(char *str, int len)
{
int i, j;
char tmp;
for(i = 0; i < len; ++i)
{
for(j = i; j < len; ++j)
{
if(str[i] > str[j])
{
tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}
}
}
} int main()
{
char str[] = "asdfvjkloi";
bubble_sort(str, 10);
puts(str);
return 0;
}
*/
/**/
///用递归,将数字转化为字符串
#include <stdio.h>
#include <stdlib.h>
#include <math.h> void int_to_str(int num, char *str)
{
int tmp;
if(num / 10 == 0)
{
*str = num + '0';
str++;
*str = '\0';
}
else
{
tmp = num / 10;
*str = num % 10 + '0';
str++;
*str = '\0';
int_to_str(tmp, str);
}
} int main()
{
int num, i;
scanf("%d", &num);
char str[100];
int_to_str(num, str);
int len = strlen(str);
for(i = len-1; i >= 0; --i)
putchar(str[i]);
puts("");
return 0;
}