一、n的阶乘
#include<stdio.h>// n的阶乘
int main()
{
int i = 0;
int n = 0;
int ret = 1;
scanf_s("%d", &n);
for (i = 1; i <= n; i++)
{
ret *= i;
}
printf("%d", ret);//1*2*3*4*5=120;
return 0;
}
二、计算1!+2!+3!+4!+n!
#include<stdio.h>//计算1!+2!+3!+4!+n!
int main()
{
int i = 0;
int n = 0;
int ret = 1;
int num = 0;
scanf_s("%d", &n);
for (i = 1; i <= n; i++)
{
ret *= i;
num += ret;
}
printf("%d", num);
return 0;
}
三、有序数组查找i的下标
#include<stdio.h>
int main()
{
int i = 0;//找i所在的下标
scanf_s("%d", &i);
int arr[] = { 1,2,3, 4, 5 ,6 ,7 ,8 ,9, 10 };
int sz = sizeof(arr) / sizeof(arr[1]);
int left = 0;
int right = sz - 1;
while (left<=right)
{
int mid = (left + right) / 2;
if (arr[mid] < i)
left = mid + 1;
else if (arr[mid] > i)
right = mid - 1;
else
{
printf("找到了,下标是%d", mid);
break;
}
}
return 0;
}