1.0 请问循环要循环多少次?
#include<stdio.h>
int main()
{
int i=0;
int k=0;
for(i=0,k=0;k=0;i++,k++)
k++;
return 0;
}
解答:改代码循环0次,原因:for循环中判断语句为k=0恒为假,所以循环0次。
2.0 编写代码,演示多个字符从两端移动,向中间汇聚
编写代码,演示多个字符从两端移动,向中间汇聚
#include<stdio.h>
#include<windows.h>
#include<string.h>
#include<stdlib.h>
int main()
{
//welcome to bite!!!!!
//********************
//w******************!
//we****************!!
//wel**************!!!
//.....
//.....
//welcome to bite!!!!!
char arr1[] = "welcome to bite";
char arr2[] = "###############";
int left = 0;
//int right = sizeof(arr1) / sizeof(arr1[0])-2;
int right = strlen(arr1)-1;
while (right>=left)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
//休息一秒
Sleep(700);
//执行系统命令的一个函数-cls-清空屏幕
system("cls");
left++;
right--;
}
printf("%s\n", arr2);
}
3.0分析代码 计算i和j的值
#include<stdio.h>
static int j;
void fun1(void)
{
static int i = 0;
i++;
}
void fun2(void)
{
j = 0;
j++;
}
int main()
{
int k = 0;
for (k = 0; k < 10; k++)
{
fun1();
fun2();
}
return 0;
}
4.0斐波那契数列(两种方法求解)
#include<stdio.h>
//int Fib(int j)
//{
// int num = 0;
// if (j <= 2)
// {
// return 1;
// }
// else
// {
// return (Fib(j - 1) + Fib(j - 2));
// }
//}
int Fib(int n)
{
int a = 1;
int b = 1;
int c = 1;
while (n > 2)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
int main()
{
int n = 0;
scanf_s("%d", &n);
int ret = 0;
ret = Fib(n);
printf("%d", ret);
return 0;
}
注:循环的方法要比函数递归更加迅速,高效。递归会出现栈溢出的问题