test5

1.完成猜数字游戏。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand((unsigned int)time(NULL));
int ran = rand() % 100 + 1;
int guess;
while (1)
{
scanf("%d", &guess);
while (’\n’ != getchar());
if (guess == ran)
{
printf(“It has been found.\n”);
break;
}
else if (guess > ran)
{
printf(“It is bigger than ran.\n”);
}
else
{
printf(“It is smaller than ran.\n”);
}
}

system(“pause”);
return 0;
}
2.写代码可以在整型有序数组中查找想要的数子,找到了返回下标,找不到返回-1。(折半查找)
#include <stdio.h>
#include <stdlib.h>
int search(int arr[], int m, int left, int light)
{
while (left <= light)
{
int mid = left + (light - left) / 2;

if (arr[mid] > m)
{
light = mid - 1;
}
if (arr[mid] < m)
{
left = mid + 1;
}
if (arr[mid] == m)
{
return mid;
}
}
return -1;
}

int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int m = 0;
printf(“请输入要找的数:>”);
scanf_s("%d", &m);
int ret = search(arr, m, 0, 9);
if (ret == -1)
{
printf(“找不到\n”);
}
else
{
printf(“找到了:%d\n”, ret);
}
system(“pause”);
}

3.编写代码模拟三次密码输入的场景。最多能输入三次密码,密码正确,提示“登陆成功”,密码错误可以重新输入,最多输入三次。三次均错,则提示退出程序。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char key[10] = { 0 };
for (int i = 1; i <= 3; i++)
{
printf(“请输入密码: \n”);
scanf("%s", &key);
if (strcmp(key, “123456”) == 0)
{
printf(“登陆成功\n”);
break;
}
else
printf(“密码输入错误,请重新输入!\n”);

}

system(“pause”);
return 0;
}
4.编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的大写字符,如果是数字不输出。
#include<stdio.h>
#include<stdio.h>
#include<string.h>
int main()
{
char i;
char n;
while (i = getchar())
{
if (i >= ‘a’&&i <= ‘z’)
{
n = i - 32;
printf("%c", n);
}
if (i >= ‘A’&&i <= ‘Z’)
{
n = i + 32;
printf("%c", n);
}
if (i >= 0 && i <= 9)
{
}
}
system(“pause”);
return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值