需要针对以下问题的C解决方案 [英] Need a C solution for the below problem

查看:84
本文介绍了需要针对以下问题的C解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明



Krishna非常喜欢糖果,所以每当他拿到糖果时,他都会储存它们以便以后随时都可以吃他想要。



他最近收到了N盒糖果,每盒包含Ci糖果,其中Ci代表第i盒中的糖果总数。克里希纳希望将它们存放在一个盒子里。唯一的限制是他可以选择任意两个盒子并将他们的联合内容存储在一个空盒子中。假设有无限数量的空盒子可用。



一次他可以拿起任何两个盒子进行转移,如果两个盒子都说包含X和Y数字分别是糖果,然后需要他精确的X + Y秒时间。由于他急于收集所有这些,他已经找你告诉他收集所有糖果的最短时间。



输入格式:



第一行输入是测试用例数T

每个测试用例由两个输入组成

第一次输入测试用例的数量是盒子的数量N

第二个输入是N个整数,用空格分隔,表示每个盒子里的糖果数量





输出格式:



打印每个测试用例所需的最短时间(以秒为单位)。在新线上打印每个输出。



限制条件:



1≤T≤10

1≤N≤10000

1≤[每箱中的糖果]≤100009





样本输入和输出



SNo。输入输出说明



1.



1

4
1 2 3 4



o / p:19



4盒,每盒包含分别为1,2,3和4个糖果。

在一个新盒子中加1 + 2需要3秒

在新盒子中添加3 + 3需要6秒

在新方框中添加4 + 6需要10秒

因此总花费时间为19秒。可能还有其他组合,但总时间不会低于19秒。



2.



1

5

1 2 3 4 5



o / p:33



5盒,每盒分别包含1个,2个,3个,4个和5个糖果。

在新盒子中加入1 + 2需要3秒钟

在新盒子中添加3 + 3需要6秒

在新盒子中添加4 + 5需要9秒

在新盒子中添加6 + 9需要15秒

因此总花费的时间是33秒。可能还有其他组合,但总时间不会低于33秒。



我尝试过的事情:



Statement

Krishna loves candies a lot, so whenever he gets them, he stores them so that he can eat them later whenever he wants to.

He has recently received N boxes of candies each containing Ci candies where Ci represents the total number of candies in the ith box. Krishna wants to store them in a single box. The only constraint is that he can choose any two boxes and store their joint contents in an empty box only. Assume that there are infinite number of empty boxes available.

At a time he can pick up any two boxes for transferring and if both the boxes say contain X and Y number of candies respectively, then it takes him exactly X+Y seconds of time. As he is to eager to collect all of them he has approached you to tell him the minimum time in which all the candies can be collected.

Input Format:

First line of input is number of test case T
Each test case is comprised of two inputs
First input of a test case is the number of boxes N
Second input is N integers delimited by whitespace denoting number of candies in each box


Output Format:

Print minimum time required, in seconds, for each of the test case. Print each output on a new line.

Constraints:

1 ≤T≤10
1 ≤N≤ 10000
1 ≤ [Candies in each box] ≤ 100009


Sample Input and Output

SNo. Input Output Explaination

1.

1
4
1 2 3 4

o/p:19

4 boxes, each containing 1, 2, 3 and 4 candies respectively.
Adding 1 + 2 in a new box takes 3 seconds
Adding 3 + 3 in a new box takes 6 seconds
Adding 4 + 6 in a new box takes 10 seconds
Hence total time taken is 19 seconds. There could be other combinations also, but overall time does not go below 19 seconds.

2.

1
5
1 2 3 4 5

o/p:33

5 boxes, each containing 1, 2, 3, 4 and 5 candies respectively.
Adding 1 + 2 in a new box takes 3 seconds
Adding 3 + 3 in a new box takes 6 seconds
Adding 4 + 5 in a new box takes 9 seconds
Adding 6 + 9 in a new box takes 15 seconds
Hence total time taken is 33 seconds. There could be other combinations also, but overall time does not go below 33 seconds.

What I have tried:

#include <stdio.h>
int main(){
	int i,t;
	scanf("%d",&t);
	for(i=0; i<t;i++)
		int n,j,time=0,a[10001],sum=0,add=0;
		scanf("%d",&n);

		
		for(j=0; j<n;j++)
		    scanf("%d",&a[j]);
		}
		for(j=0;j<n;j++){
		    time=a[j]+a[j+1];
		    sum=time+sum;
		    
		}
		printf("%d",sum);
	}
	return 0;
}

推荐答案

我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不打算为你做这一切!

因此,如果该代码不能满足要求,那么使用调试器找出原因。测试和修复代码是开发的一部分,就像编写它一样!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
So if that code doesn't do what is required, then use the debugger to find out why. Testing and fixing your code is as much a part of development as coding it in the first place!


引用:

我在循环中遇到问题。请提供任何建议或任何链接以供参考

I have a problem in looping. please give any suggestion or any link for reference

最好说明你遇到的问题。



你的问题不是问题循环,这是一个理解你需要做什么来解决声明的问题。



你有一个盒子列表,里面有糖果的数量。你有1个操作:选择2个盒子合并它们并放回产生的盒子。



以1 10 5为例检查所有可能性

1 10 5 => 11 5 => 16成本= 11 + 16 = 27

1 10 5 => 1 15 => 16成本= 15 + 16 = 31

1 10 5 => 6 10 => 16成本= 6 + 16 = 22



您的任务是了解为什么解决方案比另一个更好。这种理解将告诉您程序必须做什么才能获得最佳分数。

It is a good idea to state what is the problem you encounter.

Your problem is not a problem of looping, it is a problem of understanding what you need to do to solve the statement.

you have a list of boxes known by the number of candies inside. you have 1 operation: pick 2 boxes merge them and put back the resulting box.

Take an example with 1 10 5 check all possibilities
1 10 5 => 11 5 => 16 cost= 11 + 16 = 27
1 10 5 => 1 15 => 16 cost= 15 + 16 = 31
1 10 5 => 6 10 => 16 cost= 6 + 16 = 22

Your task is to understand why a solution is better than another. This understanding will tell you what the program must do in order to get the best score.


这篇关于需要针对以下问题的C解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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