繁体   English   中英

根据输入打印“x”n次

[英]printing “x” n times according to input

为简单的问题道歉。 我想打印“*”,N次,N根据用户输入而变化,在Java中。 预期结果,用户输入:4,打印:****。 下面显然是一个错误。 太感谢了。

Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
String str = "*";
while (a;) {
System.out.print(str);

正如@Elliot 所指出的,您可以通过一个简单for循环来实现这一点:

for (int i = 0; i < a; i++) {
  System.out.print(str);
}

在数组 x,y 中找到两个数字,其中 x <y, x repeats at least n 3 times and y 4 times< div><div id="text_translate"><p> 我一直在努力解决线性时间的数组问题,问题是:假设我们有一个数组 A [1...n] 编写一个返回 true 的算法,如果:数组 x,y 中有两个数字有以下内容:</p><ol><li> x < y</li><li> x 重复 n/3 次以上</li><li> y 重复 n/4 次以上</li></ol><p>我尝试编写以下 java 程序来执行此操作,假设我们有一个排序数组,但我认为这不是最好的实现。</p><pre> public static boolean solutionManma(){ int [] arr = {2,2,2,3,3,3}; int n = arr.length; int xCount = 1; int yCount = 1; int maxXcount= xCount,maxYCount = yCount; int currX = arr[0]; int currY = arr[n-1]; for(int i = 1; i < n-2;i++){ int right = arr[n-2-i+1]; int left = arr[i]; if(currX == left){ xCount++; } else{ maxXcount = Math.max(xCount,maxXcount); xCount = 1; currX = left; } if(currY == right){ yCount++; } else { maxYCount = Math.max(yCount,maxYCount); yCount = 1; currY = right; } } return (maxXcount > n/3 && maxYCount > n/4); }</pre><p> 如果有人对这类问题有算法想法(最好是 O(n)),我将不胜感激,因为我被这个问题困住了。</p></div></y,>

[英]Find two numbers in array x,y where x<y, x repeats at least n/3 times and y at least n/4 times

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Java 中使用递归 function 打印 N 次“a”和 N 次“b” n边滚动m模x次 打印N x N表时出现无限循环 循环输入一次输入X次 Java - 打印 n 次包含在对象 ArrayList 中的字符串 如何执行活动 N 次,其中 N 是用户输入 如何根据测试用例的值取java中的n个输入? 在数组 x,y 中找到两个数字,其中 x <y, x repeats at least n 3 times and y 4 times< div><div id="text_translate"><p> 我一直在努力解决线性时间的数组问题,问题是:假设我们有一个数组 A [1...n] 编写一个返回 true 的算法,如果:数组 x,y 中有两个数字有以下内容:</p><ol><li> x < y</li><li> x 重复 n/3 次以上</li><li> y 重复 n/4 次以上</li></ol><p>我尝试编写以下 java 程序来执行此操作,假设我们有一个排序数组,但我认为这不是最好的实现。</p><pre> public static boolean solutionManma(){ int [] arr = {2,2,2,3,3,3}; int n = arr.length; int xCount = 1; int yCount = 1; int maxXcount= xCount,maxYCount = yCount; int currX = arr[0]; int currY = arr[n-1]; for(int i = 1; i < n-2;i++){ int right = arr[n-2-i+1]; int left = arr[i]; if(currX == left){ xCount++; } else{ maxXcount = Math.max(xCount,maxXcount); xCount = 1; currX = left; } if(currY == right){ yCount++; } else { maxYCount = Math.max(yCount,maxYCount); yCount = 1; currY = right; } } return (maxXcount > n/3 && maxYCount > n/4); }</pre><p> 如果有人对这类问题有算法想法(最好是 O(n)),我将不胜感激,因为我被这个问题困住了。</p></div></y,> 在2个输入文件上使用方法-2次打印多次-Java 根据用户输入多次打印2D阵列
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM