//练习1:计算n的阶乘
//while循环
int main()
{
int n;
int fact = 1;
int i = 1;
scanf("%d", &n);
while (i <= n)
{
fact *= i;
i++;
}
printf("%d!=%d\n", n, fact);
return 0;
}
//for循环
int main()
{int n;
int fact = 1;
int i = 1;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact *= i;
}
printf("%d!=%d\n",n,fact);
return 0;
}
//练习2:计算1!+2!+...+10!
//while循环
int main()
{
int n;
int fact = 1;
int i = 1;
int sum = 0;
scanf("%d", &n);
while (i <= n) {
fact *= i;
i++;
sum += fact;
}
printf("sum = %d\n", sum);
return 0;
}
//for循环
int main()
{int n;
int i = 1;
int fact = 1;
int sum = 0;
scanf("%d",&n);
for(i=1;i<=n;i++){
fact *= i;
sum += fact;
}
printf("sum = %d\n", sum);
return 0;
}
//练习3 :在一个有序数组中查找具体的某个数字n。编写int binsearch(int x,int v[],int n);
//功能:在v[0] <= v[1] <= v[2] ... <= v[n-1] 的数组中查找x。
//递归算法
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int k = 7;
//写一个代码在数组里找到7
int i = 0 ;
int sz = sizeof(arr)/sizeof(arr[0]);
for(i=0;i<sz;i++)
{
if(k == arr[i])
{
printf("找到了,下标是: %d\n",i);
break;
}
}
if(i == sz)
{
printf("找不到\n");
}
return 0 ;
}
//折半查找算法、二分查找算法(只能用于有序数列)
int main()
{int arr[] = {1,2,3,4,5,6,7,8,9,10};
int left = 0;//左下标
int right = sz - 1;//右下标
while(left<=right)
{