如何用1毛,2毛和5毛的硬币凑出10元以下的金额?
#include <stdio.h>//凑硬币
int main()
{
int x;
int one,two,five;
int exit = 0;//用于接力break;
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+five==x*10){
printf("可以用%d个1毛加%d个2毛加%d个5毛得到%d元\n", one, two, five);
exit = 1;
break;//从内到外break
}
}
if(exit == 1) break;
}
if(exit == 1) break;
}
return 0;
}
goto语句:从多重循环的内层跳到最外层去
#include <stdio.h>//凑硬币
int main()
{
int x;
int one,two,five;
int exit = 0;//用于接力break;
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+five==x*10){
printf("可以用%d个1毛加%d个2毛加%d个5毛得到%d元\n", one, two, five,x);
//exit = 1;
goto out;
}
}
//if(exit == 1) break;
}
//if(exit == 1) break;
}
out:
return 0;
}