Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2005, 07:59 AM   #1 (permalink)
Boltress
Registered User
 
Join Date: Jan 2005
Posts: 1
Boltress is on a distinguished road
Problem Assignment (Urgent help req.)

Ok guys heres the problem and its partial soultion, i'd like your help in completing it casue i'm at my wits end.

Problem Statement

Suppose I want to make a decision rationally. One approach is to come up with several categories and weight each category according to its importance. Then I assign scores in each category to the competing alternatives, and pick the alternative with the highest total weighted score. For example, suppose I am buying a new car and I need to decide between a sedan, a minivan, or a sport-utility vehicle (SUV). In consultation with my wife, I come up with four categories

cost (weight 3)
carrying capacity (weight 2)
fuel efficiency (weight 1)
fun (weight 1)
Code:
After studying each vehicle carefully, we give them the following scores: Vehicle Cost Carrying Capacity Fuel Efficiency Fun Total Score Sedan 6 3 5 4 6*3+3*2+5*1+4*1 = 33 Minivan 5 5 3 2 5*3+5*2+3*1+2*1 = 30 SUV 4 6 2 6 4*3+6*2+2*1+6*1 = 32
Clearly we should purchase the sedan. Unfortunately, neither of us wants the sedan. I really want the minivan, and my wife really wants the SUV. And so begins the process of rationalization, in which we each try to tweak the numbers to make our choice come out on top. She quickly realizes that by tweaking one number, changing the weight of the fun category from 1 to 2, she can cause the SUV to win with a score of 38 (versus 37 for the sedan and 32 for the minivan). I have to work harder, but if I tweak two numbers, changing the cost score of the minivan from 5 to 6 and the efficiency score of the sedan from 5 to 4, then I can make the minivan win with a score of 33 (versus 32 for both the SUV and the sedan). Note that there are several other tweaks that each of us could have made that would have achieved our respective goals.

Given the initial weights and scores, as well as the zero-based index desired of the alternative that you want to win, determine the minimum number of tweaks needed to make your chosen alternative win. To win, your chosen alternative must end up with a score strictly higher than all the other alternatives--ties are not good enough. A single tweak involves changing the value of a particular weight or a particular score up or down by one. The same number cannot be tweaked more than once, and a tweak may not cause a weight or a score to exceed 9 or drop below 1. If no amount of tweaking can make your chosen alternative win, return -1.

Weights will be given as a int[], and scores will be given as a String[]. Element J of weights is the weight for category J, and element I of scores contains the scores for alternative I. Within element I of scores, character J represents the score for alternative I in category J. In the example above, weights would be { 3, 2, 1, 1 } and scores would be {"6354", "5532", "4626"}, with desired = 2 for the SUV and desired = 1 for the minivan.

Definition

Class: Rationalization
Method: minTweaks
Parameters: int[], String[], int
Returns: int
Method signature: int minTweaks(int[] weights, String[] scores, int desired)

Constraints

- Weights contains between 2 and 10 elements, inclusive.
- Each element of weights is between 1 and 9, inclusive.
- Scores contains between 2 and 10 elements, inclusive.
- Each element of scores contains exactly W characters, where W is the number of elements in weights.
- Each character in scores is a digit between '1' and '9', inclusive.
- Desired is between 0 and S-1, inclusive, where S is the number of elements in scores.

Examples

1) {3, 2, 1, 1}

{"6354", "5532", "4626"}

2

Returns: 1

The example above was trying to make the SUV win.


2) {3, 2, 1, 1}

{"6354", "5532", "4626"}

1

Returns: 2

The example above was trying to make the minivan win.


3) {3, 2, 1}

{"555", "333"}

1

Returns: -1

Option 1 can never beat option 0. The best it can do is tie.


4) {1, 2, 3, 3}

{"9234", "1334"}

1
Returns: 3

Unfortunately, we can't drop the weight of the first category to 0.


5) {8, 2}

{"55", "92"}

0

Returns: 6


6) {2, 8, 7, 3, 6, 5, 2, 4, 7, 2}

{"9197287893", "9492555365", "3459972761", "4886112198", "5963616776",
"6325897129", "3248793133", "7984474438", "4518544769", "1592681682"}

5

Returns: 17

-------------------------------------------------------------------------

GENERAL ALGORITHM


this is not an efficient solution but it will work
as mentioned in the problem, many rationalization can be there and this short time i managed to find this simple and just working form




1. get weight in an array of integer W[]

check for valid values(1 to 9)
check for no of elements(2 to 10)

2. get scores in a string array S[]

get limit of array(2 to 10)
enter the srings one by one checking each string for-
lingth of string = size of weight array
every character should be a digit between 1 to 9

set n = size of score array

3. get desire (0 to n)
let option be x

4. caldulate total score
create an integer array A[] of size same as n
create a temporary array T[]of integers with size same as weight array
repeat for every string of score array
store every string character of a single string as integer value in T[] with first character at first location and so on.
perform W[i]*T[i] and store it in A[i] for every i
select largest integer in array A and get its location
if Amax is a location x
return 0
exit
else
move to next step

5 Rationalization


Set tweak=0
find D=Amax-A[x]
store S[x] in T[] as integer numbers

arrange all the indices of s[] in a temporary array in descending order of their content
repeat for all i in the temporary array
if S[i]+1 gives total of A[x]>D
tweak+=1
elseif S[i]+1,s[i-1]+1 gives total of A[x]>D
tweak+=2
-
-

elseif s[i]+...,s[i-upperbound]+1 gives total of A[x]>D
tweak+=9
else
tweak=-1


if tweak = -1 then

{arrange all the indices of W[] in a temporary array in descending order of their content
repeat for all i in the temporary array
if W[i]>D
tweak+=1
elseif W[i]+W[i-1]>D
tweak+=2
elseif W[i]+W[i-1]+W[i-2]>D
tweak+=3
-
-

elseif W[i]+...+W[i-upperbound]>D
tweak+=9
else
tweak=-1

}
end if
return tweak

6 End


--------------------------------------------------------------------------


PART of the PROGRAM (its not fully based on above algorithm)

STEP 1 & 2. If u tweak weight say ‘j’, then gain made by the ‘desired’ total score is equal to the difference between score ‘j’ of ‘desired’ and score ‘j’ of ‘winner’ ( multiplied by amount of tweak, in this case +/-1 ).



STEP 3 & 4. If u tweak score say ‘j’ of ‘desired’, then the gain made by the ‘desired’ total score is equal to the weight ‘j’ (multiplied by amount of tweak, in this case +/-1 ).

Code:
#include <conio.h> #include <iostream.h> #include <io.h> int t=0; //no. of tweaks int w[4]; //weights int s[2][4]; //scores int tot[2]; //total scores int d; //desired element int g[2]; //points losing by 'd' to win int wintot=-1; //winner total int win=-1; //winner element void main() { //compute total scores computetotal(); if(d==win) cout << t << " Tweaks"; exit(0); //tweak weights +ve changes int tflag=0; for(int j=0;j<4;j++) { tflag=0; g[win]=s[d][j]-s[win][j]; if(g[win]>0) { for(int i=0;i<2;i++) { g[i]=s[d][j]-s[i][j]; if(g[i]<=tot[d]-tot[i]) tflag=1; } if(!tflag) { w[j]++; //tweak weight j t++; } computetotal(); if(d==win) cout << t << " Tweaks"; exit(0); else //tweak weights -ve changes {int tflag=0; for(int j=0;j<4;j++) { tflag=0; g[win]=s[win][j]-s[d][j]; if(g[win]>0) { for(int i=0;i<2;i++) { g[i]=s[i][j]-s[d][j]; if(g[i]<=tot[d]-tot[i]) tflag=1; } if(!tflag) { w[j]++; //tweak weight j t++; } computetotal(); if(d==win) cout << t << " Tweaks"; exit(0);} } } } //tweak scores +ve changes //tweak scores -ve changes } void computetotal() { for(int i=0;i<2;i++) { for(int j=0;j<4;j++) tot[i]+=s[i][j]*w[i]; if(tot[i]>wintot) { wintot=tot[i]; win=i; } } }
-------------------------------------------------------------------------
-------------------------------------------------------------------------

I would like ur help in implementing the score section of the program.

Help please,
Boltress
__________________

Last edited by Valmont : 01-12-2005 at 09:11 AM.
Boltress is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with linked list Database toast28 Standard C, C++ 12 01-14-2005 12:03 PM
looping problem maria_arif MS Technologies ( ASP, VB, C#, .NET ) 1 11-29-2004 08:07 AM
omfg... well, i may need help just understanding this problem... .pakmon. Standard C, C++ 3 01-08-2004 08:44 AM
Help debugging a power problem Belisarius Lounge 0 10-25-2003 04:44 PM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM


All times are GMT -8. The time now is 02:32 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle