#define _CRT_SECURE_NO_WARNINGS 1
练习3:输入密码,确认是否正确
#include <stdio.h>
#include <string.h>
int main()
{
char password[20] = { 0 };
int ch = 0;
int i = 0;
int ret = 0;
printf("请输入密码:");
scanf("%s", &password); //输入密码,并存放在password 数组
printf("请确认:Y/N?");
while ((ch = getchar()) != '\n')
{
;
}
ret = getchar();
if (ret == 'Y')
{
printf("确认成功\n");
}
else
printf("放弃确认!\n");
return 0;
}
1、for循环 格式:
for (表达式1; 表达式2;表达式3) --表达式1为初始化部分,表达式2为条件判断部分,表达式3为调整部分
循环语句:
用for循环打印1~10的数字
#include <stdio.h>
int main()
{
int i = 0;
// 初始化 判断 调整
for (i = 1; i < 11; i++)
printf("%d ", i);
return 0;
}
1.1 for循环里的continue
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i < 11; i++)
{
if (i == 5)
continue; //不执行循环里其他语句,跳到调整部分
printf("%d ", i);
}
return 0; //1.2.3.4.6.7.8.9.10
}
1.1 for循环里的break
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i < 11; i++)
{
if (i == 5)
break; //不执行循环里其他语句,跳到调整部分
printf("%d ", i);
}
return 0; //1.2.3.4
}
不用随意修改循环体变量的内容
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i < 11; i++)
{
if (i = 5)
printf("hehe\n");
printf("haha\n");
}
return 0; //死循环hehehaha i=5<11,无限循环
}
用for循环打印数组内容
#include <stdio.h>
int main()
{
int i = 0;
int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]); //1.2.3.4.5.6.7.8.9.10
}
return 0;
}
for变种1:
#include <stdio.h> //1.for循环的初始化、调整、判断都可省略。但如被省略,for语句内的判断条件就恒为真
int main() //2.不是非常熟练,不可随意省略
{
int i = 0;
for (;;)
{
printf("hehe");
}
return 0;
}
#include <stdio.h>
int main()
{
int i = 0;
int j = 0;
for (; i < 10; i++)
{
for (; j < 10; j++) //j内循环10次后变为11,外循环后恒定为11不执行内循环
printf("hehe\n");
}
return 0; //10个hehe
}
练习:请问循环要循环几次?
#include <stdio.h>
int main()
{
int i = 0;
int k = 0;
for (i = 0, k = 0; k = 0; i++, k++)
k++;
return 0;
}
2、do while --break与continue同for
打印1~10的值
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d ", i);
i++;
}
while (i < 11);
return 0;
}
练习:
练习1:计算n的阶乘
#include <stdio.h>
int main()
{
int i = 1;
int n = 1;
int ret = 1;
printf("请输入计算阶乘的数字:");
scanf("%d", &n);
for (i = 1; i<=n; i++)
{
ret = ret * i;
}
printf("阶乘的结果为%d\n", ret);
return 0;
}
练习2:计算1~10阶乘的和
#include <stdio.h>
int main()
{
int i = 0;
int n = 10;
int ret = 1;
int sum = 0;
for (i = 1; i <= n; i++)
{
ret = ret *i;
sum = ret + sum;
}
printf("10阶乘的总和为: %d\n", sum);
return 0;
1 2 6 24
3 9 33
}
#include<stdio.h>
int main()
{
int i = 0;
int n = 0;
int ret = 1;
int sum = 0;
for (n = 1; n <= 3; n++)
{
for (i = 1; i <= n; i++)
{
ret = ret * i;//错误算法。。阶乘ret算数错误
}
sum = sum + ret;
}
printf("3阶乘的总和为: %d\n", sum);
return 0;
}
练习3:在一个有序数组中查找具体的某个数字n。
#include <stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int a = 10;
int i = 0;
int b = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < b; i++)
{
if (arr[i]==a)
{
printf("找到了,对应的下标为: %d\n", i);
break;
}
}
if (a != arr[i])
printf("没找到!\n");
return 0;
}
二分查找算法\折半查找算法
#include <stdio.h>
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int k = 8;
int left = 0;//左下标
int sz = sizeof(arr) / sizeof(arr[0]); //计算元素个数
int right = sz - 1;//右下标
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] > k)
{
right = mid - 1;
}
else if (arr[mid] < k)
{
left = mid + 1;
}
else
{
printf("找到了!下标是:%d\n", mid);
break;
}
}
if (left>right)
printf("找不到了!");
return 0;
}
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
int main()
{
char arr1[] = "welcome to bit!!!!!!!!!";
char arr2[] = "_______________________";
int left = 0;
//int right = sizeof(arr2) / sizeof(arr2[0]) - 2; //
int right = strlen(arr2) - 1;
while (left <= right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
Sleep(500); //等待一秒 #include<windows.h>
system("cls");
left++;
right--;
}
printf("%s\n", arr2);
return 0;
}
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
int main()
{
int i = 0;
char password[20] = { 0 };
for (i = 0; i < 3; i++)
{
printf("请输入密码:");
scanf("%s", password);
if(strcmp(password, "123456")==0)//==不能用来判断两个字符串相同,应该使用库函数strcmp
{
printf("登录成功\n");
break;
}
else
{
printf("密码错误\n");
}
}
if (i==3)
printf("3次都不成功!\n");
return 0;
}