C语言总复习第一阶段部分练习题及其代码

来源:互联网 发布:长安汽车软件下载 编辑:程序博客网 时间:2024/04/28 04:48
#define _CRT_SECURE_NO_WARNINGS 1


//#include<stdio.h>
//#include<stdlib.h>
//int main()
//{
// int i = 43;
// printf("%d\n", i);
// printf("%d\n", printf("%d", i));//432
// printf("%d\n", printf("%d", printf("%d", i)));//4321
// system("pause");
// //printf函数有返回值,返回它的位数输出43返回2,输出2返回1,所以输出4321;
// return 0;
//}



//打印100-200之间的素数
//#include<stdio.h>
//#include<stdlib.h>
//#include<math.h>
//void print_su()
//{
// for (int n = 101; n <= 200; n=n+2)
// {
// int k = sqrt(n);
// int i = 0;
// for ( i = 2; i <= k; i++)
// {
// if (n%i == 0)
// break;
// }
// if (i > k)
// {
// printf("%d  ", n);
// }
//
// }
//}
//int main()
//{
// print_su();
// system("pause");
// return 0;
//}




//输出乘发口诀表
//#include<stdio.h>
//#include<stdlib.h>
//
//void print_mul1()
//{
// int i = 0;
// int j = 0;
// for (i = 1; i < 10; i++)
// {
// for (j = 1; j <= i; j++)
// {
// printf("%d*%d=%2d " ,j, i, i*j);
// }
// printf("\n");
// }
//}
//int main()
//{
// print_mul();//左正三角
// system("pause");
// return 0;
//}




//判断1000年到2000年之间的闰年
//#include<stdio.h>
//#include<stdlib.h>
//#include<assert.h>
//
//int judge_leap_year(int i)
//{
// assert(i >= 1000 && i <= 2000);
// for (int i = 1000; i <= 2000; i++)
// {
// if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
// return 1;
// else
// return 0;
// }
// return 0;
//}
//int main()
//{
// int i = 0;
// scanf("%d", &i);
// int ret=judge_leap_year(i);
// if (ret == 1)
// printf("%d是闰年\n", i);
// else
// printf("%d不是闰年\n", i);
// system("pause");
// return 0;
//}




//#include<stdio.h>
//#include<stdlib.h>
//
//int count;
//void func1();
//void func2();
//
//int main()
//{
// count = 100;
// func1();
// system("pause");
// return 0;
//}
//void func1()
//{
// int tmp;
// tmp = count;
// func2();
// printf("count is %d\n", count);
//}
//void func2()
//{
// int count;
// for (count = 1; count < 10; count++)
// {
// putchar('.');
// }
//}



//不允许创建临时变量,交换两个数的内容
//#include<stdlib.h>
//#include<stdio.h>
//
////void exchange_val(int a, int b)//没有临时变量
////{
//// a = a^b;
//// b = a^b;
//// a = a^b;
//// printf("a=%d\n", a);
//// printf("b=%d\n", b);
////}
//void exchange_val(int a, int b)//有临时变量
//{
// int c=0;
// c = a;
// a = b;
// b = c;
// printf("a=%d\n", a);
// printf("b=%d\n", b);
//}
//
////void exchange_valP(int *a, int *b)//指针交换两个变量的值
////{
//// int c;
//// c = *a;
//// *a = *b;
//// *b = c;
//// printf("a=%d,b=%d\n", *a, *b);
////}
//int main()
//{
// int a = 10;
// int b = 20;
// exchange_val(a, b);
// //exchange_valP(&a,&b);
// system("pause");
// return 0;
//}




//求十个数中的最大值和最小数
//#include<stdlib.h>
//#include<stdio.h>
//
//int MAX_10(int *a)
//{
// int max = a[0];
// for (int i = 1; i < 10; i++)
// {
// if (a[i]>max)
// {
// max = a[i];
// }
// }
// return max;
//}
//int MIN_10(int *a)
//{
// int min = a[0];
// for (int i = 1; i < 10; i++)
// {
// if (a[i]<min)
// {
// min = a[i];
// }
// }
// return min;
//}
//int main()
//{
// int a[] = { 1, 2, 3, 11, 5, 6, 7, 8, 9, 10 };
// int max = MAX_10(a);
// int min = MIN_10(a);
// printf("%d\n", max);
// printf("%d\n", min);
// system("pause");
// return 0;
//}




//写一个函数返回二进制参数二进制中1的个数
//#include<stdlib.h>
//#include<stdio.h>
//
//int print(int n)
//{
// int ret = 0;
// int arr[32] = { 0 };
// while (n)
// {
//       for (int i = 0; i < 32; i++)
//     {
//   ret = n % 2;
//   n = n / 2;
//   arr[i] = ret;
//     }
//   for (int i = 31; i>=0; i--)//逆置打印
//   printf("%d", arr[i]);
// }
//
// printf("\n");
// return 0;
//}
//int count_one_bits(unsigned int n)
//{
// int count = 0;
// while (n)
// {
// if (n % 2 == 1)
// {
// count++;
// }
// n = n / 2;
// }
// return count;
//}
//int main()
//{
// int a = 0;
// scanf("%d", &a);
// print(a);
// int count=count_one_bits(a);
// printf("%d\n", count);
// system("pause");
// return 0;
//}


//输出一个整数的每一位
//#include<stdlib.h>
//#include<stdio.h>
//#include<assert.h>
//void print_val(int n)
//{
// assert(n);
// int val = n;
// int count = 0;
// while (val)
// {
// int m = 0;
// m = val % 10;
// val = val / 10;
// count++;
// }
// printf("这个数一共有%d位\n",count);
// int ret = 0;
// int arr[32] = { 0 };
//
//       for (int i = 0; i < 32; i++)
//   {
//                 ret = n % 10;
//     n = n / 10;
//     arr[i] = ret;
//     }
//   printf("每一位分别是:");
//   for (int i = count-1; i >= 0; i--)//逆置打印
//   {
//   printf("%d ", arr[i]);
//   }
//   printf("\n");
// }
//
//int main()
//{
// int n = 0;
// scanf("%d", &n);
// print_val(n);
// system("pause");
// return 0;
//}




//计算1-1/2+1/3-1/4+...-1/100的值
//#include<stdlib.h>
//#include<stdio.h>
//float Count_Fen()
//{
// int flag = 1;
// float val = 0.0;
// for (int i = 1; i <= 100; i++)
// {
// val += (float)flag/ i;
// flag = -flag;
// }
// return val;
//}
//int main()
//{
// float sum = Count_Fen();
// printf("%f\n", sum);
// system("pause");
// return 0;
//}




//编程书一下1到100 的所有整数有多少个九
//#include<stdlib.h>
//#include<stdio.h>
//int Count_9()
//{
// int count = 0;
// for (int i = 0; i <= 100; i++)
// {
// if (i / 9 == 0 || i % 9 == 0)
// {
// count++;
// }
// }
// return count -1;//除去99即能被9整除又能被9模除
//}
//int main()
//{
// int count = Count_9();
// printf("%d\n", count);
// system("pause");
// return 0;
//}




//360面试题,循环执行了多少次?
//#include<stdlib.h>
//#include<stdio.h>
//int main()
//{
// int i = 0;
// int k ;
// int count = 0;
// for (i = 0, k = -1; k = 0; i++, k++)
// {
// k++;
// ++count;
// }
// printf("%d\n", count);//执行结果count=0;
// system("pause");
// return 0;
//}




//演示多个字符从两端向中间汇集
//#include<stdlib.h>
//#include<stdio.h>
//#include<windows.h>
//
//int main()
//{
// char ch[]="####################";
// char str[]="HuQingdou is a girl!";
// int i;
// int j;
// printf("%s\n", ch);
// for (i = 0, j = strlen(str)-1;  i<= j; i++, j--)
// {
// ch[i] = str[i];
// ch[j] = str[j];
// Sleep(1000);
// printf("%s\n", ch);
// }
// system("pause");
// return 0;
//}




//模拟一个登陆页面,输出三次密码,密码正确,正常运行,密码错误则退出
//#include<stdlib.h>
//#include<stdio.h>
//#include<string.h>
//
//int main()
//{
// int i = 0;
// char password[] = "qingdou";
// char psw[10] = {0};
// for (i = 0; i < 3; i++)
// {
// printf("please input password:");
// scanf("%s", &psw);
// if (strcmp(psw, password) == 0)
// {
// printf("password right!\n");
// break;
// }
//
// else
// printf("password error!\n");
// }
// if (i == 3)
// {
// printf("program exit!\n");
// }
// else
// printf("log in ...\n");
// system("pause");
// return 0;
//}




//计算n的阶乘。
//#include<stdlib.h>
//#include<stdio.h>
//
//int main()
//{
// int n = 0;
// int sum = 1;
// scanf("%d", &n);
// while (n)
// {
// sum *= n;
// n--;
// }
// printf("%d\n", sum);
// system("pause");
// return 0;
//}




//计算1!+2!+...+n!
//#include<stdlib.h>
//#include<stdio.h>
//int mul_t(int n)
//{
// int mul = 1;
// while (n)
// {
// mul *= n;
// n--;
// }
// return mul;
//}
//int main()
//{
// int n = 0;
// int sum = 0;
// scanf("%d", &n);
// for (int i = 1; i <= n; i++)
// {
// sum += mul_t(i);
// }
//
// printf("%d\n", sum);
// system("pause");
// return 0;
//}




//折半查找,在一个有序数组中找某个值
//#include<stdlib.h>
//#include<stdio.h>
//
//int Binserch(int x, int arr[], int n)
//{
// int end = n - 1;
// int begin = 0;
// while (begin <= end)
// {
//        int mid = (end + begin) / 2;
// if (x < arr[mid])
// {
// end = mid - 1;
// }
// else if (x>arr[mid])
// {
// begin = mid + 1;
// }
// else
// return mid;
// }
// return -1;
//}
//
//int main()
//{
// int arr[] = { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10 };
// int ret = Binserch(5, arr, 10);
// if (ret == -1)
// {
// printf("can not find.\n");
// }
// else
// printf("5能被找到下标为:%d\n", ret);
// system("pause");
// return 0;
//}




//编写一个函数,他从一个字符串中提取一个子字符串,复制到dst中;
//#include<stdlib.h>
//#include<stdio.h>
//#include<string.h>
//#include<assert.h>
//
//void substr(char dst[], char src[], int start, int len)
//{
// assert(dst);
// assert(src);
// int i = 0;
// char *p = src + start;//指针变量指向要提取字符串的地址
// int n = strlen(p);//要提取的以后有多少个字符串
// while (len)
// {
// dst[i] = p[i];
// len--;
// i++;
// }
// dst[i] = '\0';
//}
//
//int main()
//{
// char dst[10];
// char src[] = "Huqing is a girl!";
// substr(dst, src, 12, 4);
// printf("%s\n", src);
// printf("%d\n", sizeof(src));
// printf("%d\n", strlen(src));
// printf("\n");
// printf("\n");
// printf("%s\n", dst);
// printf("%d\n", sizeof(dst));
// printf("%d\n", strlen(dst));
// system("pause");
// return 0;
//}
0 0