//dengfenfa
int main()
{
char arr[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
int i;
int sz = sizeof(arr)/sizeof(arr[0]);
int k = 14;
int left =0;//左下标
int right =sz-1;//右下标
// printf("mid%d\n",mid);
// printf("left%d\n",left);
// printf("right%d\n",right);
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;
}
int main4()
{
//在数组中找到 a 所对应的下标
char arr[] = {0,1,2,3,4,5,6,7,8,9};
int a = 9;
int i;
int sz = sizeof(arr)/sizeof(arr[0]);//求数组的个数
for(i=0;i<sz;i++)
{
printf("%d\n",arr[i]);
if(arr[i] == a)
{
printf("找到了,下标是%d\n共找了 %d 次\n",i,i+1);
break;
}
}
if(i == sz)
printf("找不到");
else
printf("找得到");
return 0;
}
int main3()
{
//1*1 + 2*1 + 3*2*1 + 4*3*2*1 ...+ 10*9*8*7*6*5*4*3*2*1
int ret = 1;
int n;
int i;
int num;
for(n=1;n<=10;n++)
{
ret = ret*n;
num += ret;
}
printf("%d\n",num);
return 0;
}
int main2()
{
//n *n-1...*5 *4 *3 *2 *1
int ret = 1;
int n;
int i;
system("cls");
printf("求乘阶n的乘阶\n请输入n ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
ret = ret*i;
}
printf("n的乘阶=%d\n",ret);
return 0;
}
int main1()
{
//---------------------------------------------------------------
int n;
int i,j;
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)
{
if(i==0||j==0)
continue;
printf("%d",i);
printf("*%d=%d ",j,i*j);
}
printf("\n");
}
//---------------------------------------------------------------
return 0;
}
int mai11()
{
int x,y;
int a = 0;
int b = 0;
int i = 0;
int z,zz;
for(i = 0;i<10;i++)//建议采用"前闭后开区间"的写法。
{
if(5 == i)//不在for循环体内改变循环变量,防止for循环失去控制。
printf ("hehe\n");
printf("haha\n");
}
for(;a<10;a++)
for(;b<10;b++)
printf("qqq\n");
for(x = 0,y =0;x < 2 && y < 5;++x,y++)
printf("aaa\n");
//---------------------------------------------------------
for(z=0,zz=0;z=0;z++,zz++)//0为假 不循环
{
z++;
printf("z\n");
}
//------------------------------------------------------------
//1-10
int l = 1;
//执行再判断
do
{
printf("%d\t",l);
//if(l == 9)
//{
// l++;
// printf("%d\t",l);
//}
l++;
}
while(/*l<10*/l<=10);
return 0;
}
int maPP()
{
int i;
//初始化 判断 调整
for(i = 1;i<=10;i++)
// 1 2 3
{
printf("%d ",i);
// 4 1->2->4->3
}
return 0;
}
标签:arr,return,int,mid,ret,C语言,学习,printf From: https://blog.51cto.com/u_15855324/5818454