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;
}