Query

24 views
Skip to first unread message

Sami Ullah Kashif

unread,
Apr 21, 2013, 3:50:04 PM4/21/13
to cs1003-s...@googlegroups.com
Assalam o alaikum,
                             I have a question for the guys who attended the programming competition. What was the correct answer for Q # 5 when we had 4 digit numbers?

Sami

Sami Ullah Kashif

unread,
Apr 22, 2013, 10:28:57 AM4/22/13
to cs1003-s...@googlegroups.com
This is the code that I wrote for Problem # 5. Seems to be working. You will have to figure out the logic behind this code :)

#include <iostream>
#include <string>

using namespace std;

int calculateSum(int arr[], int index);

void main()
{
int arr[10] = {1,1,1,1,1,1,1,1,1,1};

for(int i = 1; i < 15; i++)
{
int total_sum = 0;
for(int j = 0; j < 10; j++)
{
int sub_sum = calculateSum(arr, j);
//cout << sub_sum << endl;
total_sum = total_sum + sub_sum;
arr[j] = sub_sum;
}
//cout << "===================================" << endl;
cout << "For " << i+1 << " digit number, the answer is: " << total_sum << endl;
}
}


int calculateSum(int arr[], int index)
{
int sum = 0;
for(int i = index; i < 10; i++)
{
sum = sum + arr[i];
}

return sum;
}

Shahmir Sajid

unread,
Apr 23, 2013, 8:30:41 AM4/23/13
to cs1003-s...@googlegroups.com
Sir mehran & I didnt attempt that question but on the paper it was 715

Sami Ullah Kashif

unread,
Apr 23, 2013, 11:58:44 AM4/23/13
to cs1003-s...@googlegroups.com
Yes the correct answer is 715. Try to start from 1 digit monotonically increasing numbers. Then think how you can build 2 digits monotonically increasing numbers using 1 digit monotonically increasing numbers. Once you have made 2 digit numbers, think how you can make 3 digit numbers and so on. Since you also have solution in the form of the code, it shouldn't be too hard to figure out the logic.

Regards,
Sami
Reply all
Reply to author
Forward
0 new messages