#include <stdio.h>
int main()
{
int i = 0;
int n = 0;
int ret = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
ret *= i;
}
printf("%d\n", ret);
return 0;
}
题目二:计算1!+2!+3!+......+10!
#include <stdio.h>
int main()
{
int i = 1;
int j = 1;
int sum = 0;
for (i = 1; i <= 10; i++)
{
int n = 1;
for (j = 1; j <= i; j++)
{
n *= j;
}
sum += n;
}
printf("sum = %d\n", sum);
return 0;
}
#include <stdio.h>
int main()
{
int n = 0;
int ret = 1;
int sum = 0;
for (n = 1; n <= 10; n++)
{
ret *= n;
sum += ret;
}
printf("%d\n", sum);
return 0;
}
include <stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 7;//要查找的数字
//在arr这个有序的数组中查找k(7)的值
int sz = sizeof(arr) / sizeof(arr[0]);//数组的元素个数
int left = 0;
int right = sz-1;
while (left<=right)
{
int mid = (left + right) / 2;
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid] > k)
{
right = mid - 1;
}
else
{
printf("找到了,下标是:%d\n", mid);
break;
}
}
if (left > right)
{
printf("找不到了\n");
}
return 0;
}
题目四:编写代码,演示多个字符从两端移动,向中间汇聚。
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main()
{
char arr1[] = "Hello World!!!!!!";
char arr2[] = "#################";
int left = 0;
int right = strlen(arr1)-1;
while (left<=right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
Sleep(1000);//睡眠1秒
system("cls");//清空屏幕
left++;
right--;
}
printf("%s\n", arr2);
return 0;
}