我们如何使用java解决这个难题? [英] How we can solve this puzzle using java ?

查看:66
本文介绍了我们如何使用java解决这个难题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sachin喜欢甜食。所以,他去了一个糖果市场。有一排甜蜜的摊位。每个甜点都有不同的甜点。为了省时间,他决定从邻近的摊位买糖果。因此,他可以从他想要的多个摊位购买,但所有这些摊位都需要连续。他还决定从每个摊位购买1公斤糖果。给出每个摊位1公斤糖果的费用。该市场存在一种奇怪的计费规则。该规则如下 - 所有购买的糖果的总成本是所有糖果的成本乘以他最后购买的甜食成本的总和。例如如果他以相同的顺序购买糖果,费用为2,3,4,那么糖果总费用为2 * 4 + 3 * 4 + 4 * 4 = 36。



现在他想知道购买糖果的所有可能方式的总成本是多少。你能帮助他吗?因为这个数字可能很大,你应该把最终结果的模数乘以10 ^ 9 + 7.



输入规格你的函数包含一个参数 - 一维大小为N的整数数组,其中第i个元素表示来自第i个摊位的1公斤糖果的成本。第一行输入包含表示Array大小的整数N. (1< = N< = 10 ^ 5)接下来的N行输入,每行包含1到9的单个整数。



输出规格你必须返回一个整数 - 所有可能购买糖果模块的成本总和10 ^ 9 + 7.



示例示例测试案例1-输入3 1 2 3



输出53



说明购买糖果的可能方式是 -



a)1



b)1 2



c)2



d)1 2 3



e)2 3



f)3



其中每项费用如下 -



a)1 * 1 = 1



b)1 * 2 + 2 * 2 = 6



c)2 * 2 = 4



d)1 * 3 + 2 * 3 + 3 * 3 = 18



e)2 * 3 + 3 * 3 = 15



f)3 * 3 = 9



因此总费用为1 + 6 + 4 + 18 + 15 + 9 = 53



我尝试过:



< pre lang =jav a> A {

static void printSubsets( int set []){
int n = set.length;
for int i = 0 ; i<( 1 << n); i ++){
for int j = 0 ; j< n; j ++){
if ((i&( 1 << j))> 0 ){
System.out.print(set [j] + );
}
}
System.out.println();
}
}

public static void main( String [] args){
int set [] = { 1 2 3 };
printSubsets(set);
}
}



应该添加什么额外的代码来获得上述结果。

解决方案

Quote:

我们如何使用java解决这个难题?



不是'我们',只是'你',这个谜题是一种竞赛/挑战。目标是找到一种智能的方法来尽快解决这个难题。

我能说的是蛮力永远不是理想的解决方案。

我的建议:

1)创建一个强力计划。一旦它工作,添加代码来计算它需要添加的操作数,乘以得到结果。

2)运行数据集为1,然后是2,依此类推。注意计数如何发展。

3)考虑用更智能的代码减少这些数量的方法。

否则,你必须创建算法或组合已知的算法。



Quote:

我无法找到程序的成本部分。



你有一个详细的例子,使用调试器,很容易比较你的代码正在做什么与它应该做什么。



有一个工具可以让您查看代码正在做什么,其名称是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个错误。


**对于JAVA:**



import java.util.Scanner;

public class xyz {

public static void main(String [] args)

{



扫描仪扫描=新扫描仪(System.in);



int N,i,j,k, s = 0,c;



System.out.println(输入档位数(必须在1到100000之间)\ n);



N = scan.nextInt();



if(N> 0&& N< 10000 )



{



int a [] = new int [N];



c = 0;



for(i = 0; i< N; i ++)



$



System.out.print(输入甜蜜的成本+(i + 1)+档位:\\ \\ n);



a [i] = scan.nextInt();



if(a [i]> 9 || a [i]< 1)



c ++;



}



if(c == 0)



{



for(j = 1; j< = N; j ++)



{



for(k = j; k< = N; k ++)





{



for(i = j; i< = k; i ++)



{



s = s + a [i-1] * a [k-1];





}





}



}



System.out.print(s%1000000007);



}



}



}



}







** FOR C **





#include< stdio.h>

int main(){



int N,i,j,k,s = 0;



puts(输入摊位数量(必须是在1到100000之间)\ n);



scanf(%d,& N);



if(N> 0&& N< 10000)



{



int a [N];





for(i = 0; i< N; i ++)

{

q:printf(输入甜蜜的成本) %d th stall:\ n,i + 1);



scanf(%d,& a [i]);



if(a [i]> 9 || a [i]< 1)



{

put(值超出范围0-9);



转到q;

}

}





for(j = 1; j< = N; j ++)



{



for(k = j; k< = N; k ++)


{



for(i = j; i< = k; i ++)

{

s = s + a [i-1] * a [k-1];

}

}

}

printf(%d,s%1000000007);





}

}

Sachin likes sweets a lot. So, he goes to a market of sweets. There is a row of sweet stalls. Every sweet stall has different sweets. To save some time, he decided to buy sweets from contiguous stalls. So, he can buy from as many stalls as he wants, but all of those stalls need to be contiguous. He also decided to buy only 1 kg of sweets from each of those stalls. Cost of 1 kg of sweets for each stall is given. There is a strange rule of billing in that market. And that rule is as follows- Total cost of all sweets bought is sum of the cost of all sweets multiplied by the cost of sweet he bought at the end. e.g. if he buys sweets having cost 2, 3, 4 in the same order than total cost of sweets will be 2*4+3*4+4*4=36.

Now he wonders what will be the total cost of all possible ways of buying sweets. Can you help him. Because this number could be large, you should take modulo of the final result by 10^9+7.

INPUT SPECIFICATION Your function contains a single argument- A One dimensional Integer array of Size N in which ith element denotes the cost of 1 kg sweets from ith stall. First line of input contains an Integer N denoting the size of Array. (1<=N<=10^5) Next N lines of input each containing a single integer from 1 to 9.

OUTPUT SPECIFICATION You must return an integer- sum of the cost of all possible ways of buying sweets modulo 10^9+7.

EXAMPLES Sample Test Case 1- Input 3 1 2 3

Output 53

Explanation Possible ways of buying sweets are-

a) 1

b) 1 2

c) 2

d) 1 2 3

e) 2 3

f) 3

cost of each of these is following-

a) 1*1= 1

b) 1*2+2*2= 6

c) 2*2= 4

d) 1*3+2*3+3*3= 18

e) 2*3+3*3= 15

f) 3*3= 9

Hence total cost will be 1+6+4+18+15+9=53

What I have tried:

class A {

    static void printSubsets(int set[]) {
        int n = set.length;
        for (int i = 0; i < (1 << n); i++) {
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) > 0) {
                    System.out.print(set[j] + " ");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int set[] = { 1, 2, 3 };
        printSubsets(set);
    }
}


What additional code should be added to get the above result.

解决方案

Quote:

How we can solve this puzzle using java ?


Not 'we', just 'you', this puzzle is a kind of contest/challenge. The goal is you find a smart way to solve the puzzle as fast as possible.
What I can say is that brute force is never the wanted solution.
My advice:
1) Create a brute force program. Once it works, add code to count the number of operations it need adds, multiplies to get the result.
2) Do runs with dataset of 1, then 2 and so on. Note how counts evolve.
3) Think about ways to reduce those counts with smarter code.
Said otherwise, you have to create algorithm or combine known ones.

Quote:

I am not able to find the cost part of the program .


you have a detailed example, with the debugger, it is easy to compare what your code is doing against what it should.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


**FOR JAVA:**

import java.util.Scanner;
public class xyz {
public static void main(String[] args)
{

Scanner scan = new Scanner(System.in);

int N,i,j,k,s=0,c;

System.out.println("enter the number of stalls(MUST BE IN BETWEEN 1 TO 100000)\n");

N=scan.nextInt();

if(N>0&&N<10000)

{

int a[]=new int[N];

c=0;

for(i=0;i<N;i++)

{

System.out.print("enter cost of sweet in "+ (i+1) +"th stall:\n");

a[i]=scan.nextInt();

if(a[i]>9||a[i]<1)

c++;

}

if(c==0)

{

for(j=1;j<=N;j++)

{

for(k=j;k<=N;k++)


{

for(i=j;i<=k;i++)

{

s=s+a[i-1]*a[k-1];


}


}

}

System.out.print(s%1000000007);

}

}

}

}



**FOR C**


#include<stdio.h>
int main(){

int N,i,j,k,s=0;

puts("enter the number of stalls(MUST BE IN BETWEEN 1 TO 100000)\n");

scanf("%d",&N);

if(N>0&&N<10000)

{

int a[N];


for(i=0;i<N;i++)
{
q:printf("enter cost of sweet in %d th stall:\n",i+1);

scanf("%d",&a[i]);

if(a[i]>9||a[i]<1)

{
puts("value out of range 0-9 ");

goto q;
}
}


for(j=1;j<=N;j++)

{

for(k=j;k<=N;k++)

{

for(i=j;i<=k;i++)
{
s=s+a[i-1]*a[k-1];
}
}
}
printf("%d",s%1000000007);


}
}


这篇关于我们如何使用java解决这个难题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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