 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Wed Jun 30, 2010 6:37 am Post subject: |
|
|
| Good, assuming "abs" is conditionless as shown earlier.
|
|
| Back to top |
|
 |
XaLeX Expert Cheater
Reputation: 0
Joined: 19 Aug 2008 Posts: 226
|
Posted: Wed Jul 07, 2010 7:29 pm Post subject: Re: Small Programming Challanges :) |
|
|
| Deltron Z wrote: | 5. Coins Problems
5.1. The near by convenience store decided to buy an automatic cash register, but it doesn't seem to work properly. Devise an algorithm that receives an array/list of cash in the register (each coin is worth 1 unit, 2 units, 5 units, 10 units, 20 units, 50 units, 100 units and 200 units) and a variable, amount. returns true if amount can be produced by using the cash in the register or false if you can't give back amount of change. (2 points)
Example: for the amount 253 and cash in the register { 1, 1, 0, 0, 0, 1, 2, 1 }, aka 1 coin worth 1 unit, 1 coin worth 2 units, one coin worth 50 units, 2 coins worth 100 units and 1 coin worth 200 units, the return value will be true, since we can return 1x1, 1x2, 1x50 and 2x100 or 1x200. for the amount 254 the return value is false. |
this should do =D
the raw function code without outputs and such: (note it needs math.h for the pow function.)
| Code: |
int value(int x) {return pow(10,x/3)*(x%3?(--x%3? 5 : 2): 1); }
bool f(int* arr, int amount) {
for(int i=7; i>=0; i--) {
while((amount >= value(i)) && arr[i]) {
arr[i]--;
amount -= value(i);
}
if(amount == 0) return true;
}
return false;
}
|
the whole thing for compiling:
| Code: | #include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
//0 1 2 3 4 5 6 7
//1 2 5 10 20 50 100 200
int value(int x) {
return pow(10,x/3)*(x%3?(--x%3? 5 : 2): 1);
//the double->int cast warning is not a problem, is it? this line's too cool to change it to a simple switch xD
}
bool f(int* arr, int amount) {
for(int i=7; i>=0; i--) {
while((amount >= value(i)) && arr[i]) {
cout << setw(5) << amount << " - " << setw(3) << value(i) << " -> ";
arr[i]--;
amount -= value(i);
cout << setw(4) << amount << " (" << arr[i] << "x" << value(i) << " coins left)\n";
}
if(amount == 0) return true;
}
return false;
}
int main() {
int a[8] = {1,1,0,0,0,1,2,1};
int x = 254;
if(f(a,x)) cout << "Ok!";
else cout << "Couldn't give back that amount of change!";
cin.get();
return 0;
} |
The next one's quite similar.. i'll start working on it xD
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|