1.猜数字游戏
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void Menu(){
printf("****************************\n");
printf("**********1.start***********\n");
printf("**********2.exit************\n");
printf("****************************\n");
}
void Guss_num(){
int num;
int key = rand() % 100 + 1;
while (1){
printf("请输入数字\n");
scanf("%d", &num);
if (num > key){
printf("猜大了\n");
}
else if (num < key){
printf("猜小了\n");
}
else{
printf("猜对啦~~\n");
break;
}
}
}
int main(){
int choose;
do{
Menu();
scanf("%d", &choose);
switch (choose)
{
case 1:
Guss_num();
break;
case 2:
exit(0);
break;
default:
printf("输入错误,请重新输入\n");
Menu();
break;
}
} while (choose);
srand((unsigned)time(NULL));
system("pause");
return 0;
}
2.写代码可以在整型有序数组中查找想要的数字,
找到了返回下标,找不到返回-1.(折半查找)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 25, 47 };
int left = 0;
int right = sizeof(arr) / sizeof(arr[1]) - 1;
int mid = 0;
int key = 9;
while (left <= right){
mid = (left + right) / 2;
if (arr[mid]>key){
--mid;
right = mid;
}
else if (arr[mid] < key){
++mid;
left = mid;
}
else if (arr[mid] = key){
printf("find it!operator is %d ",mid);
break;
}
else{
return -1;
}
}
system("pause");
return 0;
}
3.编写代码模拟三次密码输入的场景。
最多能输入三次密码,密码正确,提示“登录成功”,密码错误,
可以重新输入,最多输入三次。三次均错,则提示退出程序。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int i = 0;
char password[] = "123456";
char user[10];
for (i = 0; i < 3; i++){
printf("请输入密码:\n");
scanf("%s", &user);
if (strcmp(password,user) == 0){
printf("密码正确\n");
break;
}
else{
printf("输入错误请重试!!\n");
}
if (i == 3){
printf("exit!");
}
}
system("pause");
return 0;
}
4.编写一个程序,可以一直接收键盘字符,
如果是小写字符就输出对应的大写字符,
如果接收的是大写字符,就输出对应的小写字符,
如果是数字不输出。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void transverter();
int main(){
transverter();
system("pause");
return 0;
}
void transverter(){
char input = ' ';
printf("请输入需转换的字母\n");
while (1){
scanf("%c", &input);
if ((input > 64 && input<91)){
printf("%c的小写字母是%c\n", input, (input + 32));
continue;
}
else if (input>96 && input < 123){
printf("%c的大写字母是%c\n", input, (input - 32));
continue;
}
else if (input>47&&input<58)
{
continue;
}
else
{
continue;
}
}
return 0;
}