凑硬币
#include<stdio.h>
int main()
{
int x;//定义目标数为x
int one, two, five;//定义一角,两角和五角。
scanf_s("%d", &x);//输入目标数
for (one = 1; one <= x * 10; one++) {
for (two = 1; two <= x * 10 / 2; two++) {
for (five = 1; five <= x * 10 / 5; five++)
{
if (one + two * 2 + five * 5 == x * 10) {
printf("需要%d个一角 %d 个两角 %d 个五角,组成 %d 元\n", one, two, five, x);
}
}
}
}
return 0;
}
从嵌套的循环中跳出 goto 语句
#include<stdio.h>
int main()
{
int x;//定义目标数为x
int one, two, five;//定义一角,两角和五角。
scanf_s("%d", &x);//输入目标数
for (one = 1; one <= x * 10; one++) {
for (two = 1; two <= x * 10 / 2; two++) {
for (five = 1; five <= x * 10 / 5; five++)
{
if (one + two * 2 + five * 5 == x * 10) {
printf("需要%d个一角 %d 个两角 %d 个五角,组成 %d 元\n", one, two, five, x);
goto out;
}
}
}
}
out:
return 0;
}