如何解决档位和编码问题的代码(数字高级编码问题)? [英] How do I wite the code for stalls and coupens coding questions (tcs digital advanced coding questions)?

查看:224
本文介绍了如何解决档位和编码问题的代码(数字高级编码问题)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的编码逻辑是什么?他们是n家商店,每家商店都会提供一些带有一些数字的商店。如果你从商店购买coupen,那么你不能从下一个k商店购买coupen。最后你应该拥有最多的coupens。

输入: - n(没有商店)k(商店要跳过)

(你得到的价值)分别是商店)

输出: - 总和(收取的最大费用)



样本输入

10 2



3

7

9

150

0

7

10

6

答案120 + 150 + 10



我尝试了什么:



我试过逻辑但没有运气

解决方案

首先阅读输入并存储它。

然后重复处理输入,尝试每个组合。

例如如果您购买第一张商店优惠券,那么您不能从下两张优惠券中取出优惠券,但如果您愿意,可以使用第四张商店优惠券 - 7.如果您这样做,那么您将获得2张优惠券。所以一个组合是0,3(因为索引从零开始)

如果你不拿第四个商店优惠券,你可以拿第五个:9

所以你将以组合结束

 0,3 
0,4
0,5
0,6,7
0,6,8
0,6,9
1
2,6,7
2,6,8
2,6,9
...

你的工作是找出最长的组合可能是什么:3在这种情况下。



那是粗暴的力和无知方法 - 虽然它会起作用,但它不会特别有效,特别是对于大型数据集。可能您的测试需要更好的方法,例如加权图:最长路径问题 - 维基百科 [ ^ ]但实施起来要复杂得多。



无论哪种方式,你都没有代码:这是对你的能力的测试,而不是mone!


引用:

我如何找到档位和编码问题的代码(tcs数字高级编码问题)?



First of所有,在这种挑战中,每个词都很重要。所以给出原始网页的确切措辞和/或链接是个好主意。

引用:

Ive尝试了逻辑,但没有运气



这是您分配给您的挑战。您的目标是找出是否可以解决问题。既然你没有解决这个挑战,那就意味着你需要提高你的杀伤力。研究数据结构和算法是一个良好的开端,逐步细化也是一个好主意。

结构化编程.pdf [ ^ ]

引用:

我试过逻辑但没有运气



拿一张纸和一支铅笔,试着手工解决问题,没有电脑。解决其他数据集。使它成为算法。

或多或少你必须检查所有商店组合并保持最佳结果。



[更新]

您在解决方案3中发布的代码是错误的:

- 第一个原因,您从未在测试时将 curr_val 重置为0不同的路径。

- 我无法超越10也表示代码错误。


当我写测试时即使我无法理解,但回家后,当我尝试时,它在这里工作的解决方案:

  #include   <   stdio.h  < span class =code-keyword>>  

int main( void ){
int n,k,arr [ 20 ],i,j,curr_val = 0 ,max_val = 0 ,l,c;
scanf( %d,%d,& n,& k) ;
for (i = 0 ; i< n; i ++){
scanf ( %d,& arr [i]);
}
j = 0 ;
while (j< n){
l = 0 ;
while (l< n){
c = 0 ;
for (i = j; i< n;){
++ c;
if (c == 1 ){
if (jk-1> 0)
curr_val + = arr [jk- 1 ];
}
curr_val + = arr [i];
if (i == j)
i + = l + k + 1;
else
i + = k + 1;
}
if (curr_val> max_val){
max_val = curr_val;
}
curr_val = 0 ;
l ++;
}
j ++;
}
printf( %d,max_val);
return 0 ;
}



i无法超越10,所以最有可能输入10个值

what is the coding logic for the question ? thier are n shops, each shop will give a coupen with some number on it. if you take a coupen from a shop then you cant take coupen from the next k shops. at the end you should posses the max total of coupens.
Input:- n(no of shops) k(shops to skip)
(Values of coupen you get at a shop respectively)
Output:- sum(max coupens collected)

Sample input
10 2
120
3
7
9
150
0
7
10
6
Answer 120+150+10

What I have tried:

Ive tried for the logic but no luck

解决方案

Start by reading the input and storing it.
Then process the input repeatedly, trying each combination.
For example, if you take the first shops coupon, then you cannot take a coupon from the next two, but you can take the fourth shops coupon if you want - 7. If you do, then you will have 2 coupons in total. So one combination is 0, 3 (because indexes start at zero)
If you don't take the forth shop coupon, you could take the fifth: 9
So you will end up with combinations

0, 3
0, 4
0, 5
0, 6, 7
0, 6, 8
0, 6, 9
1
2, 6, 7
2, 6, 8
2, 6, 9
...

Your job is to work out what the longest such combination may be: 3 in this case.

That's the "brute force and ignorance" approach - and while it will work, it won't be particularly efficient, particularly for large datasets. It may be that your test will require a better approach, such as a weighted graph: Longest path problem - Wikipedia[^] but that will be a lot more complex to implement.

Either way, you'll get no code: this is a test of your abilities, not mone!


Quote:

How do I wite the code for stalls and coupens coding questions (tcs digital advanced coding questions)?


First of all, in this kind of challenges, every word matters. So it is a good idea to give exact wording and/or a link to original webpage.

Quote:

Ive tried for the logic but no luck


This is a challenge assigned to you by you. Your goal is to find out if you can solve the problem or not. Since you didn't solved the challenge, it means that you need to sharpen your kills. Studying data structures and algorithms is a good start, Stepwise refinement is a good idea too.
Structured Programming.pdf[^]

Quote:

Ive tried for the logic but no luck


Take a sheet of paper and a pencil and try to solve the problem by hand, no computer. Solve other datasets. Make it an algorithm.
More or less you have to check all combination of shops and keep best result.

[Update]
The code you posted in Solution 3 is wrong:
- first reason, you never reset curr_val to 0 as you test different paths.
- "i couldn't make it go beyond 10" is also an indication that the code is wrong.


well when i wrote the test even i couldn't figure it out,but after coming home,when i tried,it worked here the solution:

#include <stdio.h>

int main(void) {
	int n,k,arr[20],i,j,curr_val=0,max_val=0,l,c;
	scanf("%d,%d",&n,&k);
for(i=0;i<n;i++){
    scanf("%d",&arr[i]);
}
j=0;
while(j<n){
    l=0;
while(l<n){
    c=0;
for(i=j;i<n;){
    ++c;
    if(c==1){
 if(j-k-1>0)
     curr_val+=arr[j-k-1];
    }
 curr_val+=arr[i];
if(i==j)
 i+=l+k+1;
 else
 i+=k+1;
}
if(curr_val>max_val){
    max_val=curr_val;
}
curr_val=0;
l++;
}
j++;
}
printf("%d",max_val);
	return 0;
}


i couldn't make it go beyond 10 so most probably it will work good for input of 10 values


这篇关于如何解决档位和编码问题的代码(数字高级编码问题)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆