marți, 11 decembrie 2018

c++ programming part 15



1237. Greedy Technique
1238. The second programming technique is the greedy method.
1239. It is used when asking for the BEST solution for a problem.
1240. The technique is a common sense: at every step of solving the problem, I
choose the one better option, so that in the end the solution obtained is the
best. 1241. In this type of problem, an OPTIM criteria is usually used.
1242. Example:
1243. car problem:
1244. There is a car that can carry up to 600 kg.
1245. There are n objects (eg n = 5).
1246. For each object we know: its weight and the gain if its transport.
1247. Choose the objects that will be carried with the car and will lead to maximum
gain.
1248. Obvious. An object is better, its lower the weight and the gainobtained by
transport is higher.

1249. This is the optimal criteria: earning / weight, usd / kg: how many usd you earn at
one
1250. kilogram transported.
1251. Object:
1252. 1
1253. 2
1254. 3
1255. 4
1256. 5
1257. I earn
1258. 100
1259. 200
1260. 400
1261. 50
1262. 25
1263. weight
1264. 1000
1265. 400
1266. 400
1267. 25
1268. 100
1269. optimum
1270. 10.00%
1271. 50.00%
1272. 100.00%
1273. 200.00%
1274. 25.00%

1275. Sorts objects after optimal: object with the highest optimal first, and so on
1276. object
1277. 4
1278. 3
1279. 2
1280. 5
1281. 1
1282. I earn
1283. 50
1284. 400

1285. 200
1286. 25
1287. 100
1288. weight
1289. 25
1290. 400
1291. 400
1292. 100
1293. 1000
1294. optimum
1295. 200.00%
1296. 100.00%
1297. 50.00%
1298. 25.00%
1299. 10.00%
1300. It is considered that it can not "cut" objects.
1301. Start by putting the first object in the car with the most effective big, object 4.
1302. Then enter item 3, the total weight in the c reachinarg at 425.
1303. Object 2 can not be taken because it has 400 kg and the weight is exceeded
1304. maximum transportable.
1305. Enter item 5, the weight reaches 525.
1306. Object 1 can not be taken, has a weight of 1000.

Niciun comentariu:

Trimiteți un comentariu

Rețineți: Numai membrii acestui blog pot posta comentarii.