1.求输出结果
结果:8,10,12,14,16
int sum(int a);
{int c = 0;
static int b = 3;
c += 1;
b += 2;
return(a + b + c);
}
int main()
{
int i = 0;
int a = 2;
for(i=0;i<=5;i++)
{
printf("%d", sum(a));
}
return 0;
}
2.求程序运行结果
结果:死循环打印5
int main()
{
int i = 0;
for (i = 0; i < 10; i++)
{
if (i = 5)//赋值判定为恒为1
printf("%d", i);
}
return 0;
}
3.求程序运行结果
输出:0
int func(int a)
{
int b;
switch (a)
{
case 1: b= 30;
case 2: b= 20;
case 3: b= 16;
defult: b = 0;
}
return b;
}
4.求程序运行结果
结果:hellothird
int main()
{
int x = 3;
int y = 3;
switch(x%2)
{
case 1: switch (y)
{
case 0:printf("first");
case 1:printf("second");
break;
default:printf("hello");
}//没有break,继续往下执行case 2
case 2:printf("third");
}
return 0;
}
5.将三个数从大到小输出
int main()
{
int a = 0;
int b = 0;
int c = 0;
scanf("%d%d%d", &a,&b,&c);
if(a {
int p1 = a;
a = b;
b = p1;
}
if(a{
int p2 = a;
a = c;
c = p2;
}
if(b{
int p3 = b;
b = c;
c = p3;
}
printf("%d %d %d", a, b, c);//算法实现:a中放最大值;b中放中间值;c中放最小值
return 0;
}
6.打印1-100所有三的倍数
int main()
{
int i = 0;
for (i = 1; i <= 100; i++)
{
if (i % 3 == 0)
printf("%d\n", i);
}
return 0;}
7.求两个数最大公约数(辗转相除法)
int main()
{
int a = 0;
int b = 0;
int r = 0;
scanf("%d %d", &a, &b);
while(r=a%b)
{
a = b;
b = r;
}
printf("%d\n", b);
return 0;
}
8.打印1000-2000年之间的闰年
int main()
{
int i = 1000;
for (i = 1000; i <= 2000; i++)
{
if (i % 4 == 0&i%100!=0)
printf("%d\n", i);
else if(i%400==0)
printf("%d\n",i);
}
return 0;
}
9.打印100-200的素数(试除法)
int main()
{
int i = 0;
int count = 0;
for (i = 100; i <= 200; i++)
{
int j = 0;
for (j = 2; j < i; j++)
{
if (i % j == 0)
{
break;
}}
if (j == i)
{
count++;
printf("%d\n", i);
}
}
printf("\ncount=%d\n", count);
return 0;
}
10.打印100-200的素数(试除法-求i的开平方)
#include
int main()
{
int i = 0;
int count = 0;
for (i = 100; i <= 200; i+=2)
{
int j = 0;
for (j = 2; j<=sqrt(i); j++)
{
if (i % j == 0)
{
break;
}}
if (j>sqrt(i))
{
count++;
printf("%d\n", i);
}
}
printf("\ncount=%d\n", count);
return 0;
}
11.计算1-100出现多少个9
int main()
{
int i = 0;
int count = 0;
for (i = 1; i <= 100; i++)
{
if (i%10==9 || i / 10 == 9)
count++;
if (i % 10 == 9 && i / 10 == 9)
count++;
}
printf("\ncount=%d\n", count);
return 0;
}
12.计算1-1/2+1/3-...+1/99-1/100的值
int main()
{
int i = 0;
double sum = 0.0;
int f = 1;
for(i=1;i<=100;i++)
{
sum += f * 1.0/i;
f = -f;
}
printf("%lf", sum);
return 0;
}
13.比较1-10中最大值
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int max = arr[0];//假设最大值处于数组中(注意负数)
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);//计算数组元素个数
for(i=0;i<=sz;i++)
{
if (arr[i] >= 10)
{
max = arr[i];
printf("max=%d\n", max);
}
}
return 0;
}
14.打印9*9乘法口诀表
int main()
{
int i = 0;
for (i = 1; i <= 9; i++)//行数
{
int j = 1;
for (j = 1; j <= i; j++)//列数
{
printf("%d*%d=%-2d ", i, j, i * j);//%2d打印两位,不够用空格补齐“+”右对齐;“-”左对齐
}
printf("\n");
}
return 0;
}
15.猜数字游戏
void game()
{ //生成一个随机数
int ret = 0;
int guess = 0;
//时间戳:当前时间-计算器起始时间(1970.1.1. 0:0:0)令srand的值不断变化
ret = rand()%100+1;//生成1-100的随机数
while(1)
{
printf("猜的数字是:>");
scanf("%d", &guess);
if (guess > ret)
{
printf("大了\n");
}
else if (guess < ret)
{
printf("小了\n");
}
else
{
printf("猜对了\n");
break;
}
}
}
void menu()
{
printf("***********************");
printf("**** 1.play 0.exit ****");
printf("***********************");
}
int main()
{
int i = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请选模式:>");
scanf("%d", &i);
switch (i)
{
case 1:game();//玩游戏
break;
case 0:printf("退出");
break;
default:
printf("选择错误\n");
break;
}
} while (i);
return 0;
}
16.关机程序
int main()
{
char input[20] = { 0 };
system("shutdown -s -t 60");
while (1)
{
printf("请注意你的电脑将在一分钟内关机,如果输入:取消,就取消关机\n:请输入>:");
scanf("%s", input);
if (strcmp(input, "取消") == 0)//-strcmp 比较字符串
{
system("shutdown -a");
break;
}
}
return 0;
}
(所有头文件均省略)