//不可在for循环体内修改循环变量,防止for循环失去控制。
//建议for语句的循环控制变量的取值采用“前闭后开区间”写法;
//for循环的初始化,调整,判断都可以省略。但是:for循环的判断部分如果被省略,那判断条件就是恒为真;
//计算n的阶乘
/*
#include<stdio.h>
int main()
{
int i = 1;
int n = 0;
int ret = 1;
printf("请输入一个数:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
//n = n*i;这样写是错的,改变了n就改变了判断条件。应该加一个变量
ret = ret * i;
}
//printf("%d",n);
printf("%d",ret);
return 0;
}
*/
//计算阶乘和
/*
#include<stdio.h>
int main()
{
int n = 0;
int ret = 1;
int a = 0;
int sum = 0;
printf("请输入一个数");
int b;
scanf("%d",&a);
b = a;
for(n=1;n<=b;n++)
{
ret = ret*n;
sum = ret +sum;
}
printf("sum = %d",sum);
return 0;
}
*/
//找出数组中你要找的数的下标(有序无序数列皆可)
/*
#include<stdio.h>
int main()
{
int arr[]={1,2,3,4,5,6,7,8,9,10};
int k = 0;
printf("请输入你要查找的值");
scanf("%d",&k);
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for(i=0;i<=sz;i++)
{
if(k == arr[i])
{
printf("找到了:%d",i);
break;
}
if(i==sz)
printf("找不到");
}
return 0;
}
*/
//用二分法找出你要找的数的下标(只适用于有序数列)
/*
#include<stdio.h>
int main()
{
int k = 0;
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr)/sizeof(arr[0]);
int left = 0;
int right = sz-1;
scanf("%d",&k);
while(left<=right)
{
int i = (left + right)/2;
if(arr[i]<k)
{
left = i+1;
}
else if(arr[i]>k)
{
right = i-1;
}
else
{
printf("下标是:%d",i);
break;
}
}
if(left>right)
printf("该数不在序列内:");
return 0;
}
*/
//sizeof求的单位是字节;
//strlen求的是字符串长度;
/*
#include<stdio.h>
#include<string.h>
int main()
{
char arr[] = "abcde";
int a = sizeof(int);
printf("%d\n",a);
int b = strlen(arr);
printf("%d",b);
return 0;
}
*/
//实现简单的动画效果
/*
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
char arr1[] = "welcome to bit!";
char arr2[] = "###############";
int left = 0;
int right = strlen(arr1)-1;//等价于sizeof(arr1)/sizeof(arr[0])-2;‘/0’占一个内存;
while(left<=right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n",arr2);
Sleep(1000);
system("cls");//执行系统命令的一个函数-cls-清空屏幕
left += 1;
right -= 1;
}
return 0;
}
*/
//简单实现 登录密码功能。
/*
#include<stdio.h>
#include<string.h>
int main()
{
int i = 0;
char passwd[20] = {0};
for(i=0;i<3;i++)
{
printf("请输入密码:");
scanf("%s",passwd);
if(strcmp(passwd,"123456")== 0)
{
printf("登录成功\n");
break;
}
else
{
printf("密码错误\n");
}
}
if(i == 3)
printf("三次密码均错误,退出程序\n");
return 0;
}
*/
分支与循环(2)-第四天
最新推荐文章于 2023-12-12 16:00:30 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)