Usually homework content (statements and some basic logic questions)

   I will update the do while statement and some exercises tomorrow. After the completion, the nesting of the function, a pile of two piles of arrays, recursion or something

 

Table of contents

 1. Dichotomy to find data

2. The factorial of n and the sum of n factorials 

 3. While using getchar

4. Simple application of structure and different methods 

5. Various summation methods of functions


 1. Dichotomy to find data

//二分法查找数据//相对于传统的来说效率更高
#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
    char arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int main()
{
	int a = 11;                   //记住sizeof(arr)不能写成sizeof(arr【】)
	//求它的数组下标
	int lentch = sizeof(arr) / sizeof(arr[0]); //arr【0】为第一个组员的占的空间 ,此公式可以求得数组的有几个数

	int leftpoint = 0;//数组是从下表为0开始的

	int rightpoint =lentch-1;//第一次的时候我设置成了0,因为不知道数组里有几个数,所以要用含参的形式表达

	while(leftpoint<=rightpoint)

	{	int mid = (leftpoint + rightpoint) / 2;//使用二分法的前提条件是:改空间的数字必须为有序排列,
	//倒叙顺序都可

			if (arr[mid] < a)//?
			{
				leftpoint = mid + 1;
			}
			else if (arr[mid] > a)
			{
				rightpoint = mid - 1;
			}
			else
			{
				printf("此数的下标:%d ", mid);
				break;//一定要加break,否则将一直打印。
			}
		
	}
	if (rightpoint > leftpoint)//可以让叙述更加明确
		printf("找不到");
	return 0;
}

2. The factorial of n and the sum of n factorials 

int main()//求n的阶乘
{
	int a = 0;
	int i = 0;
	int n = 0;
	int gret = 1;
	int sum = 0;

	scanf("%d",&n);	
	for (i=1;i<=n;i++)
{
		gret = gret * i;
		若求阶乘的和,则在后需要添加sum+=gret
	}
	printf("%d",gret);
	return 0;
}

 

 3. While using getchar

//(2)while语句的应用(getchar())
int main()
{
	char pass[7] = { 1,3,1,4,5,2,0 };
	printf("请输入密码->:"); //下一步就该输入了
	scanf("%s", pass);
	getchar();//将换行符拿走了
	printf("请确认密码(Y/N)->:");
	int niu = getchar();
	if('Y'==niu)
	{	
		printf("%s", "yes");
	}
	else
	{
		printf("%s","no");

	}
	return 0;

 

4. Simple application of structure and different methods 

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
                                                      //结构体
struct stu
{
	char name[15];
	int age;                      //int* stu   说明stu为指针变量,数据类型为int;
	char sex[10];
	char num[20];
	char high[10];

};
  void dizhi(struct stu* ps)  //子函数     //将地址放到指针变量里边子函数开始运行。
{
	//printf("%s %d %s %s %s",ps->name,ps->age,ps->sex,ps->num,ps->high);
	  printf("%s %d %s %s %s",(*ps).name,(*ps).age,(*ps).sex,(*ps).num,(*ps).high);
}

int main()
{
	struct stu s= {"niupengzhan", 20, "male", "15534525920", "185"};//s为成员名
	//结构体对象.成员名
	//printf("%s %d %s %s\n", s.name, s.age, s.sex, s.num, s.high);
	dizhi(&s);

	return 0;
}

 

5. Various summation methods of functions

//int sumtole(int a,int b) //求和函数的一种写法
//{
	//return a + b;
//}
   //还可以使用定义宏来,define宏的结构:define 参数 宏体



#define sum  ((a)+(b))  //可以以后尝试使用定义宏来写,一顶三
int main()
{
	int a = 0;
	int b = 0;

	scanf("%d %d",&a,&b);
    int c = sum;
	printf("%d",c);

	return 0;
}

 

Guess you like

Origin blog.csdn.net/fcccfffg/article/details/132125996