当前位置: 首页 > >

找零 C语言

发布时间:

#include

int main()
{
int x;
int one, two, five;

scanf("%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个1角加%d个2角加%d个5角得到%d元
",
one, two, five, x);
break;
}
}
}
}

return 0;
}



加入循环停止条件,只要第一个符合条件的结果?
一层一层退出循环


#include

int main()
{
int x;
int one, two, five;
int exit = 0;

scanf("%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个1角加%d个2角加%d个5角得到%d元
",
one, two, five, x);
exit = 1;
break;
}
}
if ( exit == 1 ) break;
}
if ( exit == 1 ) break;
}

return 0;
}

goto跳出多重循环?

#include

int main()
{
int x;
int one, two, five;

scanf("%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个1角加%d个2角加%d个5角得到%d元
",
one, two, five, x);
goto out;
}
}
}
}
out:
return 0;
}


?



友情链接: