求高手帮忙解答!
编程结果是这样的:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
编程结果是这样的:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
解决方案
2
int n = 4; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { int k = i - j + 1; int value = i + (k - 1) * n - k * (k - 1) / 2; System.out.print(value + " "); } System.out.print("\n"); }
给你贴一个之前别人写的倒排的,觉得你这要求比这个简单多了
2
public static void main(String[] args) { int num = 1; for (int i = 1; i <= 5; i++) { for (int j = 0; j < i; j++) { System.out.print(num++); System.out.print("\t"); } System.out.print("\n"); } }
2
int n =28;
int i=1;
int rows=1;
while(i<=n){
for(int j=0;j<rows;j++ ){
if(i>n){
break;
}
System.out.print(i++ +”\t”);
int i=1;
int rows=1;
while(i<=n){
for(int j=0;j<rows;j++ ){
if(i>n){
break;
}
System.out.print(i++ +”\t”);
}
rows++;
System.out.println();
}
2
public class Test {
public static void main(String[] args) {
acc(33);
}
public static void acc(int n){
if(n<=0){
System.out.println(“您输入的数字不合法!”);
}else{
for (int i = 1; i <= n; i++) {
int m = i*(i-1)/2;
if(i*(i+1)<=2*n){
List<Integer> ls = new ArrayList<Integer>();
for (int j = 0; j < i; j++) {
ls.add(j+(m+1));
}
System.out.println(ls);
}else if(m<=n){
List<Integer> ls = new ArrayList<Integer>();
for (int j = 0; j < n-m; j++) {
ls.add(j+(m+1));
}
System.out.println(ls);
}
}
}
}
}
public static void main(String[] args) {
acc(33);
}
public static void acc(int n){
if(n<=0){
System.out.println(“您输入的数字不合法!”);
}else{
for (int i = 1; i <= n; i++) {
int m = i*(i-1)/2;
if(i*(i+1)<=2*n){
List<Integer> ls = new ArrayList<Integer>();
for (int j = 0; j < i; j++) {
ls.add(j+(m+1));
}
System.out.println(ls);
}else if(m<=n){
List<Integer> ls = new ArrayList<Integer>();
for (int j = 0; j < n-m; j++) {
ls.add(j+(m+1));
}
System.out.println(ls);
}
}
}
}
}
1
只需要一个循环也是可以的
public static void main(String[] args) { int len = 15, l = 1; for (int i = 1; i <= len; i++) { System.out.format("%-5d", i); if (l * (l + 1) >>> 1 == i) { System.out.println(); l++; } } }
1
public static void main(String[] args) { print(22); } public static void print(int n) { int r = 1; for (int i = 0; i < n;) { for (int j = 0; j < r; j++) { if (++i > n) { break; } System.out.print(i + "\t"); } r++; System.out.println(); } }
1
public class test {
public static void main(String[] args) {
int a = 1;
for(int x = 1; x < 6; x++) {
for(int y = 0; y < x; y++) {
System.out.print(a++ + ” “);
}
System.out.println();
}
}
}
public static void main(String[] args) {
int a = 1;
for(int x = 1; x < 6; x++) {
for(int y = 0; y < x; y++) {
System.out.print(a++ + ” “);
}
System.out.println();
}
}
}
1
public class MyTester {
public static void func(int n){
int j = 1;
int count = 0;
for(int i = 1; i <= n; i++){
System.out.print(i + ” “);
count++;
if(count == j ){
System.out.println();
count = 0;
j++;
}
}
}
public static void main(String[] args){
func(100);
}
}
public static void func(int n){
int j = 1;
int count = 0;
for(int i = 1; i <= n; i++){
System.out.print(i + ” “);
count++;
if(count == j ){
System.out.println();
count = 0;
j++;
}
}
}
public static void main(String[] args){
func(100);
}
}
2
public class TriangelPrint { public static void main(String[] args) { printTriangle(4); } private static void printTriangle(int level){ if(level <=0) throw new IllegalArgumentException("level must great than zero"); for(int i=1;i<=level;i++){ int end = i * (i +1) /2; int start = end - i + 1; for(int j=start;j<=end;j++){ System.out.print(j + " "); } System.out.println(); } } }
2
public class TriangelPrint { public static void main(String[] args) { printTriangle2(4); } private static void printTriangle2(int level){ if(level <=0) throw new IllegalArgumentException("level must great than zero"); int count = 0; for(int i=1;i<=level;i++){ for(int j=1;j<=i;j++){ System.out.print(++count + " "); } System.out.println(); } } }
2
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print(“输入N的值:”);
int n = in.nextInt();
int count = 1;
int sum = 1;
for(int i = 1; i <= n; i ++){
System.out.print(“”+i+”\t”);
if(sum == count){
System.out.println();
sum=1;
count++;
}
sum++;
}
}
Scanner in = new Scanner(System.in);
System.out.print(“输入N的值:”);
int n = in.nextInt();
int count = 1;
int sum = 1;
for(int i = 1; i <= n; i ++){
System.out.print(“”+i+”\t”);
if(sum == count){
System.out.println();
sum=1;
count++;
}
sum++;
}
}
2
int n=15;//要打印的n个数字 int sum=1;//用于每行要打印的个数 int num=0;//用于计数每行已打印的个数 for(int i=1;i<=n;i++){ System.out.print(i+" "); num++; if(num==sum){ sum++;//下一次换行+1个数 num=0; System.out.println();//换行 } }
1
public static void main(String[] args) { int num = 21; int temp = 1; int tag = 0; for (int i = 1; i <= num; i++) { System.out.print(i + " "); tag ++; if (temp == tag) { System.out.print("\n"); temp ++; tag = 0; } } }
1
这个比较清楚.
或判断每行最后一个值: row_num * (row_num +1) /2
嵌套循环的不是逻辑问题就是效率问题
3
![看了一些帖子突然想到之前有家面试出的一道编程题!愣是没想出来 看了一些帖子突然想到之前有家面试出的一道编程题!愣是没想出来](https://www.codebye.com/wp-content/uploads/2016/07/-4419580481236554310.jpg)
2
楼上的代码都已经贴的不少了,假如不是玩文字游戏,本人觉得这个题目应该不算难,题主需要多努力多动手编程,就能够解决的。看到有人提出这是一个9×9乘法表,本人去本人的电脑搜索了一下9×9乘法表,还真的发现用过c语言写过,java写过,javascript写过。这里贴出来,以后题主学js可以借鉴借鉴!
C语言算法
C语言算法
main(v,c)char**c; { for(v[c++]="Hello, world!\n)";(!!c)[*c]&&(v--||--c&&execlp(*c,*c,c[!!c]+!!c,!c));**c=!c)write(!!*c,*c,!!**c); }
这代码不是本人写的,是c板块推荐的用一句话就写出一个99乘法表的牛人写的。
java算法
public class Jiujiuchengfa{ public static void main(String[] args){ for(int i = 1; i <= 9; i++){ for(int j = 1; j <= 9 - i; j++){ System.out.print("\t"); } for(int j = i; j >= 1; j--){ System.out.print(i + "*" + j + "=" + (i*j) + "\t"); } System.out.println(); }//////////// } }
js算法
<script type="text/javascript"> function printTable(n) { for(var i = 1; i <= n; i++) { for(var j = 1; j <= i; j++) { document.writeln(j + "x" + i + "=" + (i*j)); } document.writeln("<br/>"); } } var n = parseInt(window.prompt("输入到第几个")); printTable(n); </script>
2
上一版是输入行数,这一版直接输入当前数
System.out.println(stringBuffer);
stringBuffer.delete(0, stringBuffer.length());
int row=1; int preTail=0;
for(int i=1;i<18;i++){
stringBuffer.append(i+” “);
if(i==(row*row-preTail)){
stringBuffer.append(“\n”);
row++;
preTail=i;
}
}
System.out.println(stringBuffer);
System.out.println(stringBuffer);
stringBuffer.delete(0, stringBuffer.length());
int row=1; int preTail=0;
for(int i=1;i<18;i++){
stringBuffer.append(i+” “);
if(i==(row*row-preTail)){
stringBuffer.append(“\n”);
row++;
preTail=i;
}
}
System.out.println(stringBuffer);
1
public class Demo3 {
public static void main(String[] args) {
boolean bool=true;//设置一个开关
int num=0;
int end=23;//一共要输出多少个
for(int i=0;;i++){//死循环,可以无限输出
for(int j=0;j<=i;j++){
System.out.print(++num+”\t”);//每一行都打印num的值
if(num==end){//判断相等,结束打印行的循环
bool=false;
break;
}
}
if(!bool){//假如bool是false,结束死循环
break;
}
System.out.println();//每次打印完一行换行
}
}
}