考虑具有N个元素的整数数组A,其中每个元素与另一个数组元素具有一对一的关系.
对于每个i,其中1≤i≤N,在元素i和元素N-i + 1之间存在1-> 1的关系
任务是对此阵列执行以下操作,如下所示:
给定两个整数(L,R),我们必须将该范围内的每个元素与其相关元素交换.(参见下面的示例说明)
样本输入
5
1 2 3 4 5
2
1 2
2 3
Run Code Online (Sandbox Code Playgroud)
样本输出
5 2 3 4 1
Run Code Online (Sandbox Code Playgroud)
解释对于第一个查询,我们将1与5和2交换为4.现在数组变为 - 5 4 3 2 1
同样现在,对于第二个查询,我们将4与2和3交换.所以最终的阵列将是5 2 3 4 1
我的程序是这样的:
import java.util.Scanner;
public class ProfessorAndOps {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
int n=in.nextInt();//length of array
int a[]=new int[n];//array declaration
for(int i=0;i<n;i++){
//inputting array elements
a[i]=in.nextInt();
}
int q=in.nextInt();//number of queries
for(int i=0;i<q;i++){
int …Run Code Online (Sandbox Code Playgroud) 我用一个方法创建了以下界面:
public interface Greeting {
public void perform();
}
Run Code Online (Sandbox Code Playgroud)
我试图将接口的一个实例传递给 greet() 方法。
public class Greeter {
public void greet(Greeting greeting){
greeting.perform();
}
public static void main(String[] args) {
Greeter greeter=new Greeter();
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个问候界面的实现:
public class HelloWorldGreeting implements Greeting {
@Override
public void perform() {
System.out.println("Hello World");
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误(HelloWorldGreeting 类型的方法 perform() 必须覆盖或实现超类型方法),快速修复为“删除@Override 注释”
我还检查了编译器配置。只有 1.8。
请让我知道为什么会出现错误或帮助我修复它。提前致谢。
java annotations compiler-errors javacompiler java-annotations
我有一个具有以下格式的字符串列表:
('group1-1', 'group1-2','group1-9', 'group2-1','group2-2', 'group2-9','group1-10', 'group2-10' )
我需要将它们排序如下:
先分组,然后数字。
('group1-1', 'group1-2','group1-9','group1-10', 'group2-1','group2-2', 'group2-9', 'group2-10' )
我已经编写了以下代码,但它没有按预期工作:一个基于组排序的比较器,如果组匹配,则根据数字进行排序。
my @list = ('group1-1', 'group1-2','group1-9',
'group2-1','group2-2', 'group2-9','group1-10', 'group2-10' );
@list = sort compare @list;
for (@list){
print($_."\n");
}
sub compare{
my $first_group, $first_num = get_details($a);
my $second_group, $second_num = get_details($b);
if($first_group < $second_group){
return -1;
} elsif($first_group == $second_group){
if ( $first_num < $second_num) {
return -1;
} elsif ( $first_num == $second_num ) {
return 0;
} else {
return 1;
}
} …Run Code Online (Sandbox Code Playgroud) 在这里,我想将每行中的数据打印为3个单独的值,并使用" :"作为分隔符.该文件BatmanFile.txt包含以下详细信息:
Bruce:Batman:bat@bat.com
Santosh:Bhaskar:santosh@santosh.com
Run Code Online (Sandbox Code Playgroud)
我预期的输出是:
Bruce
Batman
bat@bat.com
Santosh
Bhaskar
santosh@santosh.com
Run Code Online (Sandbox Code Playgroud)
执行脚本后的输出是:
Bruce
Batman
bat@bat.com
Bruce
Batman
bat@bat.com
Run Code Online (Sandbox Code Playgroud)
请解释一下我在这里缺少的东西:
use strict;
use warnings;
my $file = 'BatmanFile.txt';
open my $info, $file or die "Could not open $file: $!";
my @resultarray;
while( my $line = <$info>) {
#print $line;
chomp $line;
my @linearray = split(":", $line);
push(@resultarray, @linearray);
print join("\n",$resultarray[0]),"\n";
print join("\n",$resultarray[1]),"\n";
print join("\n",$resultarray[2]),"\n";
}
close $info;
Run Code Online (Sandbox Code Playgroud) 我正在编写一个 Perl 脚本来删除目录中的符号链接,并且我注意到当前使用 find 命令的方法需要花费相当多的时间。我正在寻找更有效的解决方案。
这是当前的代码片段:
system("find '$dir' -type l -exec rm -f {} \\;");
Run Code Online (Sandbox Code Playgroud)
我还尝试了使用 unlink 和 glob 的替代方案:
unlink glob("$dir/*") if -e $dir;
Run Code Online (Sandbox Code Playgroud)
然而,这两种方法似乎都有其缺点,我想知道是否有更优化的方法来实现 Perl 中的符号链接删除。
任何有关优化删除过程的见解或建议将不胜感激。谢谢你!
附加信息:
我正在实现Sieve算法,用于查找最多为n的素数.我无法找出为什么它会进入无限循环.
在这里,我给出了代码片段.请帮忙.
for(int j=2;j<=Math.sqrt(n);j++){
if(a[j]==true){
int x=0;
for(int p=(j*j+x*j); p<=n;x++){
a[p]=true;
}
}
}
Run Code Online (Sandbox Code Playgroud) 给定两个排序的整数数组A和B,将B合并为A作为一个排序的数组.
注意:您必须修改数组A以包含A和B的合并.不要在代码中输出任何内容.提示:C用户请将结果malloc成一个新数组并返回结果.
如果A和B中初始化的元素数分别为m和n,则执行代码后数组A的结果大小应为m + n
示例:
Input :
A : [1 5 8]
B : [6 9]
Modified A : [1 5 6 8 9]
Run Code Online (Sandbox Code Playgroud)
我的解决方案
public class Solution {
public void merge(ArrayList<Integer> a, ArrayList<Integer> b) {
int i=0,j=0;
ArrayList<Integer> al= new ArrayList<Integer>();
while(i<a.size() && j<b.size()){
if(a.get(i)<b.get(j)){
al.add(a.get(i));
i++;
}
else{
al.add(b.get(j));
j++;
}
}
while(i<a.size()){
al.add(a.get(i));
i++;
}
while(j<b.size()){
al.add(b.get(j));
j++;
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了第3个ArrayList,我合并了第一个和第二个ArrayLists的所有元素.现在我必须将3rd ArrayList的所有元素复制到第一个.我现在被困住了.因为,当我通过使用调整大小时
public static void ensureSize(ArrayList<Integer> list, int size){
list.ensureCapacity(size);
while(list.size()<size){
list.add(null);
}
}
ensureSize(a,al.size());
for(int …Run Code Online (Sandbox Code Playgroud) arrays ×3
java ×3
perl ×3
algorithm ×2
annotations ×1
arraylist ×1
collections ×1
file-io ×1
javacompiler ×1
loops ×1
optimization ×1
perl-module ×1
sieve ×1
sorting ×1
symlink ×1