Test.多个字符从两端,向中间汇聚。
#define _CRT_SECURE_NO_WARNINGS 1;
#include <stdio.h>
#include <string.h>
#include <Windows.h>//用Sleep函数时需要引用的头文件
int main()
{
char arr1[] = "I CAN GET AN OFFER";
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(130);//延时/休眠函数。
system("cls");//清空屏幕。
left++;
right--;
}
printf("%s",arr2);
return 0;
}
Test.模拟登录(限制3次) 密码正确显示登陆成果,密码错误显示再次尝试,三次错误后显示退出程序。
#define _CRT_SECURE_NO_WARNINGS 1;
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0;
char R_Password[16] = "chr1s";
char E_Password[16] = "0";
for ( i = 0; i < 3; i++)
{
printf("Please enter your password:");
scanf("%s", E_Password);
if (strcmp(E_Password, R_Password) == 0)
//两个字符串进行比较要用string compare(strcmp)
//一一对比:abcdef
// abccef
//(a=a,b=b,c=c,d>c)→abcdef>abccef
{
printf("Log in successful");
break;
}
else
{
printf("Wrong password.Please try again\n");
}
if (i == 2)
{
printf("You have tried too many times,exit the program");
break;
}
else
{
;
}
}
return 0;
}
strcmp输出值:
Test.猜数游戏实现(100内)**
代码编辑思维:
1.进入界面
1-1进入游戏
1-2退出游戏
2.猜数游戏
2-1随机数字实现
2-2有游戏反馈(猜大了、猜小了....)
3.猜对后是否继续游戏
#define _CRT_SECURE_NO_WARNINGS 1;
#include <stdio.h>
#include <stdlib.h>//使用rand(),srand(),
#include <time.h>
void menu()//建立界面
{
printf("--------------\n");
printf("----1.PLAY----\n");
printf("--------------\n");
printf("----0.EXIT----\n");
printf("--------------\n");
}
void guess_game()//建立游戏函数
{
int guessing = 0;
while (1)
{
printf("please enter your guessing:");
scanf("%d", &guessing);
if (guessing < (rand() % 101))
{
printf("the entered number less than the guessing\n");
}
else if (guessing > (rand() % 101))
{
printf("the entered number greater than the guessing\n");
}
else
{
printf("Congratulations!You find it:%d\n", guessing);
break;
}
}
}
int main()//主函数
{
srand((unsigned int)time(NULL));
//在使用rand()函数寻找随机值时,需要使用srand(),
//且在调用rand()函数时需要先使用srand()找一个随机的初始种子。
//转到定义知void srand(unsigned int seed)需要的是unsigned int类型
//而time()函数返回类型为time_t,因此使用强制转换将time的类型改为unsigned int类型
//因为time()自带参数,且我们不要用,因此向其传递一个空指针NULL。
//注:The constant RAND_MAX is the maximum value that can be returned by the rand function.
//RAND_MAX is defined as the value **0x7fff**.
int Choose = 0;
do
{
menu();
printf("Choose Your Answer:");
scanf("%d", &Choose);
if (1 == Choose)
{
guess_game();
}
else if (0 == Choose)
{
break;
}
else
{
printf("Input can't be identify,please choose 1 or 0 :)\n");
}
} while (1);
return;
}
Test:
将乱序数组的三个数按循序输出(升降都行)。
#define _CRT_SECURE_NO_WARNINGS 1;
#include <stdio.h>
int main()
{
int arr[] = { 8,1,0 };
int i = 0;
int bridge = 0;
if (arr[0] > arr[1])
{
bridge = arr[1];
arr[1] = arr[0];
arr[0] = bridge;
}
if (arr[0] > arr[2])
{
bridge = arr[2];
arr[2] = arr[0];
arr[0] = bridge;
}
if (arr[1] > arr[2])
{
bridge = arr[2];
arr[2] = arr[1];
arr[1] = bridge;
}
while (i <=2)
{
printf("%d ", arr[i]);
i++;
}
return 0;
}
Test
输出指定数内能被三整除的数
int main()
{
int n = 0;
printf("enter an integer\n");
scanf("%d", &n);
int i = 0;
for ( i = 0; i < n; i++)
{
if (0 == i % 3)
{
printf("%d\n", i);
}
}
return 0;
}
Test.
求2个数的最大公约数:
int main()
{
int a = 0;
int b = 0;
int max = 0;
scanf("%d %d", &a, &b);
if (a < b)
{
max = b;
}
else
{
max = a;
}
while (1)
{
if (a % max == 0 && b % max == 0)
{
printf("common divisor:%d\n",max);
break;
}
max--;
}
return 0;
}
//辗转相除法
int main()
{
int a = 0;
int b = 0;
int n = 0;
printf("enter two integer:");
scanf("%d %d", &a, &b);
while (1)
{
if (0 != a % b)
{
n = a % b;
a = b;
b = n;
}
else
{
printf("common divisor:%d\n", b);
break;
}
}
return 0;
}