c语言题目
第一题代码:
#define _crt_secure_no_warnings 1
#include<stdio.h>
int main() //计算n的阶乘
{
int n;
int sum = 1;
int ret = 1;
while (1)
{
ret = 1;
scanf("%d", &n);
for (int i = 1;i <= n;i++)
{
ret = ret * i;
}
printf("%d\n", ret);
}
return 0;
}
第二题代码
#define _crt_secure_no_warnings 1
#include<stdio.h>
int main()
{
int sum = 0;
int ret = 1;
int i=1;
while(i<=10)
{
ret = 1; //计算10的阶乘累加和
for (int j = 1;j <= i;j++)
{
ret *= j;
}
sum += ret;
i++;
}
printf("%d", sum);
return 0;
}
第三题代码
#define __CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main() //有序数组的查找
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int n=9;
int letf = 0;
int right = (sizeof(arr) / sizeof(arr[0])) - 1;
while (letf <= right) //胜利之笔 好好推敲 卡住
{
int mid = letf + (right - letf) / 2;
if (arr[mid] > n)
{
right = mid - 1;
}
else if (arr[mid] < n)
{
letf = mid + 1;
}
else
{
printf("恭喜你 找到了,下标为%d\n", mid);
break;
}
}
if (letf > right) //卡住
{
printf("不在该数组\n");
}
return 0;
}
第四题代码
#define __CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<windows.h>
int main() //视觉残留效果
{
char ch1[] = "wo ai ni zhong guo love!!!!!!!";
char ch2[] = "******************************";
int letf = 0;
int right = strlen(ch1)-1; //strlen不统计\n,, 而sizeof(arr)/sizeof(arr[0])会统计\n
while (letf<= right)
{
ch2[letf] = ch1[letf];
ch2[right] = ch1[right];
printf("%s\n", ch2);
Sleep(1000);
system("cls");
letf++;
right--;
}
return 0;
}
第五题代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{
char ch[10] = { 0 };
printf("请输入密码\n");
int i = 1;
while (i <= 3)
{
scanf("%s", ch); //若输入123456 s 会有两行提示错误,重新输入
if (strcmp(ch, "123456 s") == 0)
{
printf("输入正确\n");
break;
}
else
{
printf("输入错误,请重新输入\n");
}
i++;
}
return 0;
}