几种换法"/>
C++百钱问题:将100元人民币换成1,2,5元,共有几种换法
百钱问题:将100元人民币换成1,2,5元,共有几种换法
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {int count = 0;//换法int temp;int m = 100;//总钱数int a = 1;//1元面值int b = 2;//2元面值int c = 5;//5元面值//a, b, c的值还可以是10,20,50for (int i = 0; i <= m / b; ++i) {for (int j = 0; j <= m / c; ++j) {temp = m - (b * i + c * j);if (temp >= 0 && temp % a == 0) {//只要2元和5元的总和加起来不大于100,剩下的都换1元++count;}}}cout << "换法有" << count << "种\n";system("pause");return 0;
}
更多推荐
C++百钱问题:将100元人民币换成1,2,5元,共有几种换法
发布评论