解释:
如果输入字符串是“hello worlds”,则输出将为 2。
“你好”这个词的长度 = 5
“世界”一词的长度 = 6
将它们的长度相加得到总长度 = 5+6 = 11
这不是一个数字,所以不断添加所有数字,直到我们得到一个数字,即 1+1=2 因此,单个数字是 = 2(作为答案/输出)。
我尝试使用我的代码如下:
import java.util .*;
class Codestring {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter word");
String word = sc.nextLine();
int len2 = 0, len1 = 0, count = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == ' ') {
len2 = count;
System.out.println(len2);
count = 0;
} else {
count++;
}
}
len1 = count;
System.out.println(len1);
int c = len1 + len2;
System.out.println(c);
ArrayList<Integer> array = new ArrayList<Integer>();
do {
array.add(c % 10);
c /= 10;
}
while (c > 0);
System.out.println(array);
while (array.size() >= 2) {
array = reduce(array);
return array;
}
}
private static ArrayList<Integer> reduce(ArrayList<Integer> array) {
for (int i = 0; i < array.size(); i++) {
array = array[i] + array[i + 1];
}
return array;
}
}
我达到了我的输出:
输入单词
你好世界
5
6
11
[1, 1]
相关分类