Algorithm 如何打印此输出?
输出:Algorithm 如何打印此输出?,algorithm,language-agnostic,Algorithm,Language Agnostic,输出: String x = "1 -7 2"; String y = "-2 2 1"; 我们将使用x的第一个负数或正数,y的第一个数…只是一个粗略的草图 将空白处的两个字符串拆分为数组 在这两个数组上循环并逐个合并它们 对于Java: 您可以使用split方法拆分每个字符串,然后您就有了一个数组,可以从每个数组中打印出相应的字符。在java中直接使用split函数on space“” 在c中,在空间'上使用strtok,将它们放入2个整数数组中,并在其上循环。对于奇数迭代的第一个数组,对
String x = "1 -7 2";
String y = "-2 2 1";
我们将使用x的第一个负数或正数,y的第一个数…只是一个粗略的草图
- 将空白处的两个字符串拆分为数组
- 在这两个数组上循环并逐个合并它们
您可以使用split方法拆分每个字符串,然后您就有了一个数组,可以从每个数组中打印出相应的字符。在
java
中直接使用split
函数on space“”
在c
中,在空间'
上使用strtok
,将它们放入2个整数数组中,并在其上循环。对于奇数
迭代的第一个数组,对于偶数
迭代的第二个数组,并打印这些数字这应该可以:
1,-2
-7,2
2,1
这涉及x和y大小不相同的情况
String[] splitX = x.split(" ");
String[] splitY = y.split(" ");
System.out.println(splitX[0]+","+splitY[0]);
System.out.println(splitX[1]+","+splitY[1]);
System.out.println(splitX[2]+","+splitY[2]);
String x=“1-7 2”;
字符串y=“-2 1”;
//拆线
字符串[]xSplit=x.split(\\s+);
字符串[]ySplit=y.split(\\s+);
//循环通过它们
for(int i=0;i
在C中执行此操作的简单方法:
String x = "1 -7 2";
String y = "-2 2 1";
// Split the strings
String[] xSplit = x.split("\\s+");
String[] ySplit = y.split("\\s+");
// Loop through them
for (int i = 0; i < xSplit.length; i++) {
System.out.print(xSplit[i] + " ");
if (i < ySplit.length)
System.out.print(ySplit[i] + " ");
}
// Print more y if needed
for (int i = xSplit.length; i < ySplit.length; i++) {
System.out.print(ySplit[i] + " ");
}
System.out.println();
在Java中,可以使用Scanner类
char * x = "1 -7 2";
char * y = "-2 2 1";
int xs[3], ys[3];
sscanf(x, "%d %d %d", xs, xs+1, xs+2);
sscanf(y, "%d %d %d", ys, ys+1, ys+2);
printf("%d, %d\n%d, %d\n%d, %d\n", xs[0], ys[0], xs[1], ys[1], xs[2], ys[2]);
在javadocs中查找:)
这三种语言真的很不同……请发布您目前掌握的代码,并解释哪些代码没有按照您的预期工作。拆分功能带来了魔力……您需要C、Java或VB.net方面的帮助吗?因此,请使用与语言无关的标记,而不是一些随机标记。
String integers = "1 -4 3";
Scanner sc = new Scanner(integers);
while(sc.hasNextInt())
{
System.out.println(sc.nextInt();
}