1.实现倒计时(2:00,1:59........)
#include<stdio.h>
#include<windows.h>
int main()
{
int a = 2, b = 0;
printf("请输入分钟数:");
scanf("%d", &a);
while (a >= 0)
{
printf("倒计时: %d:%02d", a, b);
Sleep(1000);
b = b - 1;
if (b < 0)
{
a = a - 1;
b = 59;
}
printf("\n");
}
return 0;
}
其中%02d为当数字不足两位时补零
同样的还有%03d %04d........
2.while的内嵌
输出此样式文件
首先,分析其格式,为5行需要5次循环(需要一个自增变量)
其次,每一行星星数加一(需要一个自增变量)
#include <stdio.h>
int main()
{
int a = 1, b;
while (a <= 5)
{
b = 1;
while (b <= a)
{
printf("*");
b++;
}
printf("\n");
a++;
return 0;
}

#include <stdio.h>
int main()
{
int a = 1, b;
while (a <= 5)
{
b = 1;
while (b <= a)
{
printf("%d ", a);
b++;
}
printf("\n");
a++;
return 0;
}

此处则需要三个自增变量
两个变量同上,一个用于自增输出数字
#include <stdio.h>
int main()
{
int a, b, c;
a = 1; c = 1;
while (a <= 5)
{
b = 1;
while (b <= a)
{
printf("%d ", c);
b++; c++;
}
printf("\n");
a++;
}
return 0;
}
本文展示了如何使用C语言实现倒计时功能,从指定分钟开始递减,每秒更新显示,并在分钟转换时正确重置秒数。此外,还介绍了如何通过嵌套循环来输出逐渐增加星号数的图案,每个新行比前一行多一个星号,形成一个等腰三角形。
2547

被折叠的 条评论
为什么被折叠?



