var getNumber=function(num){ var str=num+" "; var time=num/2-1; for(var i=0;i<time;i++){ var rm=num+(num-Math.pow(2,i+1)); str+=rm+" "; num=num+(num-Math.pow(2,i+1)); } return str; }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个奇数:"); int num = sc.nextInt(); int[] arr = new int[num / 2 + 1]; arr[0] = num; System.out.print(num + " "); for (int i = 1; i < arr.length; i++) { arr[i] = (int) Math.pow(2, i) * (num - i); System.out.print(arr[i] + " "); }
}}
package cn.feynman.csdn_ansawer;import java.util.Scanner;public class App { public static void main(String[] args) { System.out.println("please input a odd number"); Scanner sc = new Scanner(System.in); int odd = sc.nextInt(); //建立存储数组 int array[][] = new int[odd][odd]; //给数组赋值 for (int i = 0; i < odd; i++) { array[i][0] = i * 2 + 1; for (int j = 1; j <= i; j++) { array[i][j] = array[i][j - 1] + array[i - 1][j - 1]; } } //输出数组 for (int i = 0; i < array.length; i++) { for (int j = 0; j <= i; j++) { if (array[i][j] != 0) { System.out.print(array[i][j] + " "); } } System.out.print("\n"); } System.out.println("..."); } }
简化一下 package cn.feynman.csdn_ansawer;import java.util.Scanner;public class App { public static void main(String[] args) { System.out.println("please input a odd number"); Scanner sc = new Scanner(System.in); int odd = sc.nextInt(); //建立存储数组 int array[][] = new int[odd][odd]; //给数组赋值並输出 for (int i = 0; i < odd; i++) { array[i][0] = i * 2 + 1; System.out.print(array[i][0]+" "); for (int j = 1; j <= i; j++) { array[i][j] = array[i][j - 1] + array[i - 1][j - 1]; System.out.print(array[i][j]+" "); } System.out.print("\n"); } System.out.println("..."); } }
大道至简, 你们想复杂了...public static void printLine(int num){ int row = num/2+1; //行数 int[] line = new int[row];//奇数行数据 //生成行数据 for (int i = 0; i < row; i++) { line[i] = (int) (Math.pow(2, i)*(num-i)); } //打印行数据 for (int j : line) { System.out.println(j); } }
public class Logic { public static void main(String[] args) { System.out.println("请输入一个基数"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int line = num / 2 + 1; for (int j = 0; j < line; j++) { System.out.print((int) ((num - j) * Math.pow(2, j)) + "\t"); } } }
public static void main(String[] args) { System.out.println("please input the number:"); Scanner scanner = new Scanner(System.in); int row = scanner.nextInt(); if(row % 2 == 0 && row > 0) { System.out.println("bad input!"); return; //end the program } row = row / 2; //if there are n rows, row from 0 to n-1 //the first of the column is n*2<<(n-1), n from 0 to n StringBuffer stringBuffer = new StringBuffer(); int tempResult = 0; //temporary result, each number for(int i = 0 ; i < row ; i ++) { //from 0 to n-2 stringBuffer.append((((i + 1) * 1 << i) + (2 << i) * (row - i)) + ", "); //((i + 1) * 1 << i) is the first number in the column // (2 << i) * (row - i) is the addition } stringBuffer.append(((row + 1) * 1 << row)); System.out.println(stringBuffer.toString()); }public static void main(String[] args) { System.out.println("please input the number:"); Scanner scanner = new Scanner(System.in); int row = scanner.nextInt(); if(row % 2 == 0 && row > 0) { System.out.println("bad input!"); return; //end the program } row = row / 2; //if there are n rows, row from 0 to n-1 //the first of the column is n*2<<(n-1), n from 0 to n StringBuffer stringBuffer = new StringBuffer(); int tempResult = 0; //temporary result, each number for(int i = 0 ; i < row ; i ++) { //from 0 to n-2 stringBuffer.append((((i + 1) * 1 << i) + (2 << i) * (row - i)) + ", "); //((i + 1) * 1 << i) is the first number in the column // (2 << i) * (row - i) is the addition } stringBuffer.append(((row + 1) * 1 << row)); System.out.println(stringBuffer.toString()); }比较土的方法,,,见谅
Scanner sc = new Scanner(System.in); System.out.println("输入一个奇数"); int num = sc.nwxtInt(); int temp = 0; for(int i = 0;i < num;i++) { if(i == 0){ System.out.print(num + '\\t'); }else if(i == 1){ temp = num + num – 2; System.out.print(temp + '\\t'); }else { temp = temp + temp –(4*(i–1)); System.out.print(temp + '\\t'); } }
根据二楼的思路,改了一下。 public void test10(int obb){
int col = (obb/2)+1; int[] num=new int[col]; num[0]=obb; int temp=obb; for(int i=1;i<col;i++) { num[i]=(int) ((int)temp*2-Math.pow(2, i)); temp=num[i]; System.out.println(num[i]); } }
package com.test;class Demo { public static void main(String[] args) { Demo d=new Demo(); d.test10(9); } public void test10(int obb){
int col = (obb/2)+1; int[] num=new int[col]; num[0]=obb; int temp=obb; for(int i=1;i<col;i++) { num[i]=(int) ((int)temp*2-Math.pow(2, i)); temp=num[i]; // System.out.println(num[i]); } for(int temp2:num) { System.out.print(temp2+" "); } } }
public static void main(String[] args) { int num = Integer.parseInt(args[0]); //行数 int row = num / 2 + 1; int[][] datas = new int[row][]; for (int i = 0; i < row; i++) { //每行列数 int column = i + 1; datas[i] = new int[column]; for (int j = 0; j < column; j++) if (j == 0) datas[i][j] = 2 * column - 1; else datas[i][j] = datas[i - 1][j - 1] + datas[i][j - 1]; } for (int[] arr : datas) //只输出给定行 if (arr[0] == num) for (int data : arr) System.out.print(data + " "); }2楼大佬的int column = i + 1; 这一句应该改成为int column = i * 1; 这个应该是乘积吧!
System.out.println("请输入大于零的要打印的行数num : "); Scanner scan = new Scanner(System.in); int num = scan.nextInt();
for(int[] a : arr){ for(int res : a){ if(res!=0){ System.out.print(res+" "); } } System.out.println(); }
亲测,可用。 static void Main(string[] args) { Console.WriteLine("请输入一个奇数"); int n = Convert.ToInt32(Console.ReadLine()); int num = (n + 1) / 2; List<int> m = get(num); for (int i = 0; i < num; i++) { Console.Write(m[i] + " "); } Console.ReadLine();
} public static List<int> get(int i) //给定当前行的数,获取当前一行的数组 { List<int> m = new List<int>();
m.Add(2 * i - 1);
if (i > 1) { for (int x = 1; x < i; x++) { List<int> z = get(i - 1); m.Add(m[x - 1] + z[x - 1]);//i[x-1]=get(i-1) } } return m; }
/** * 根据当前数组,计算下一个数组 * @param arr * @return */ public static int[] nextArr(int... arr) { int[] res = new int[arr.length + 1]; for(int i = 0, len = res.length; i < len; ++i){ if(i == 0) { res[i] = arr[i] + 2; } else { res[i] = res[i - 1] + arr[i - 1]; } } return res; }
还没有学数组,看你们都用数组,但是我这个好像也可以实现 import java.util.Scanner;public class CSDN { public static void main(String[] args) { Scanner input = new Scanner(System.in); int i = input.nextInt(); for (int x = 1; x <= (i + 1) * 0.5; x++) { int s = (int) Math.pow(2, (x - 1)) * (i - x + 1); System.out.print(s + "\t"); } }}
递归思想最简单public class Dome { public static void getOneRow(int start) { for (int i = 1; (i - 1) * 2 < start; i++) { System.out.print(getOne(start, i) + ","); } } public static int getOne(int start, int n) { if (n == 1) { return start; } else { return getOne(start, n - 1) + getOne(start - 2, n - 1); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入首项:"); int start = sc.nextInt(); getOneRow(start); } }
@Test
public void test10(){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个奇数:");
int a=sc.nextInt();
int num=(a/2)+1;
int[] b=new int[num];//存放需要打印的数据
b[0]=a;//第1个值
for(int i=1;i<num;i++){
b[i]=(int) (b[i-1]+b[i-1]-Math.pow(2, i));
}
for(int i=0;i<num;i++){
System.out.print(b[i]+" ");//打印
}
System.out.println();
}
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
//行数
int row = num / 2 + 1;
int[][] datas = new int[row][];
for (int i = 0; i < row; i++) {
//每行列数
int column = i + 1;
datas[i] = new int[column];
for (int j = 0; j < column; j++)
if (j == 0)
datas[i][j] = 2 * column - 1;
else
datas[i][j] = datas[i - 1][j - 1] + datas[i][j - 1];
}
for (int[] arr : datas)
//只输出给定行
if (arr[0] == num)
for (int data : arr)
System.out.print(data + " ");
}
int a = 0;
while(a!=-1){
a = scanner.nextInt();
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
if(a%2==1){
//行数
for(int i = 1; i<=a; i+=2){
//列值
List<Integer> list = new ArrayList<Integer>();
for(int j=0; j<=i/2;j++){
if(j==0){
list.add(i);
}else{
list.add(map.get(i-2).get(j-1)+list.get(j-1));
}
}
map.put(i, list);
}
for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
if(entry.getKey() == a){
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}else{
System.out.println("输入的数字不是奇数,请重新输入!");
}
}
2.第二次分析:得出输入奇数num,该奇数所在行的数字个数为(num+1)/2
3.所需实现方法:循环
// 1
// 3, 4
// 5, 8,12
// 7,12,20,32
// 9,16,28,48,80
//
// 根据数据完全解剖找规律
// arr[0]=9,arr[1]=9+7,arr[2]=16+12,arr[3]=28+20,arr[4]=48+32
// arr[0]=9,arr[1]=9+(9-2),arr[2]=9+(9-2)+(9-2)+(9-2-2),arr[3]=...每个数都=前个数+前个数的上面一个数
// arr[0]=a,arr[1]=2a-2,arr[2]=4a-8,arr[3]=6a-24,arr[4]=8a-64
// arr[0]=a,arr[1]=2^1a-1*2^1,arr[2]=2^2a-2*2^2,arr[3]=2^3a-3*2^3,arr[4]=2^4a-4*2^4
// arr[i]=2^i(a-i)
// arr[0]=9;arr[1]=16;arr[2]=28;arr[3]=48;arr[4]=80
//
// 数组长度:i<(a+1)/2给你我的分析过程
var str=num+" ";
var time=num/2-1;
for(var i=0;i<time;i++){
var rm=num+(num-Math.pow(2,i+1));
str+=rm+" ";
num=num+(num-Math.pow(2,i+1));
}
return str;
}
*题目:
*1
*3 4
*5 8 12
*7 12 20 32
*9 16 28 48 80
*根据输入的奇数,打印此图形
*思路:
*除第一行和列外,从第二行第二列开始每一个元素者是它左边和左上方元素之和
*故可考虑定义一个二维数组,需要的赋值,其它赋零,
*输出时遇零输出回车。
*/
public class MTest {//public可不写 public static void main(String[] args) {
int num=21;
if(num==0)
return;
int[][] s=getArray(num);
printArray(s);
} public static void printArray(int[][] s) {
for(int i=0;i<s.length;i++){
for(int j=0;j<s[0].length;j++){
if(s[i][j]!=0)
System.out.print(s[i][j]+"\t");
else{
System.out.println();
break;//为了对该元素后的也为零的元素不判断,使用break跳出内层循环。
}
}
}
} public static int[][] getArray(int num) {
int arraylength=(num+1)/2;
int[][] myarray=new int[arraylength][arraylength];
for(int i=0;i<arraylength;i++){
myarray[i][0]=i*2+1;
for(int j=1;j<=i;j++){
myarray[i][j]=myarray[i-1][j-1]+myarray[i][j-1];
}
}
return myarray;
}}
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个大于零的整数。");
int a=sc.nextInt();
int count=1;
for(int i=1;i<a;i+=2){
System.out.print(i+" ");
for(int j=1;j<count;j++){
if(j-2<0){
System.out.print(2*(i-j)+" ");
}else{
int x=4*(i-j)*(int)Math.pow(2, j-2);
System.out.print(x+" ");
}
}
count++;
System.out.println();
}
}
import java.util.Scanner;public class Test{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
String str = keyboard.nextLine().trim();
assert str.matches("^\\d*[13579]$");
int line = Integer.valueOf(str);
int[] array = new int[line];
int[] result = new int[line]; for(int i = 0; i < line ; i ++){
result = new int[line];
result[0] = 1 + i * 2;
for(int j = 1 ; j <= i ; j ++){
result[j] = result[j - 1] + array[j - 1];
}
array = result;
} for(int i : result){
System.out.printf("%-4d",i);
}
}
}
3 4
5 8 12
7 12 20 32
9 16 28 48 80
11 20 36 64 112 192*1 *2 *4 *8 *16 *32
1
3 2
5 4 3
7 6 5 4
9 8 7 6 5
11 10 9 8 7 6设横轴为M[1,],纵轴为N[1,],有f(M,N) =(2(N-1)-M)*(2^(M-1))
不好意思,应该是
设纵轴为N[1,],横轴为M[1,N],当M<=N时,有
f(M,N) =(2N-M)*(2^(M-1))
规律找出来后发现还是没啥亮点,并没有用到什么算法。代码也贴出来吧
int len = 6;//第几行 1...n
int array[] = new int[len];
array[0] = (len-1)*2+1;
for(int i=1;i<len;i++){
array[i] = (int) ((array[i-1]-Math.pow(2, i-1))*2);
}
System.out.println(Arrays.toString(array));
public static int f(int k,int n) {
if(n==1)
return 2*k-1;
return f(k,n-1)+f(k-1,n-1);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int m=sc.nextInt()/2+1;
for(int i=1;i<=m;i++)
System.out.print(f(m,i)+" ");
}
* 打印
* 1
* 3 4
* 5 8 12
* 7 12 20 32
* 9 16 28 48 80
* .....
* 输入任意一个奇数,输出那一行的数据
* 分析:
* a[0] = sc.nextInt; a[1] = 2*2
* 3
* 5 a[1] = 2*(5 - 1); a[2] = 2*2*(5 - 2)
* 7 a[1] = 2*(7 - 1); a[2] = 2*2*(7 - 2);a[3] = 2*2*2*(7 - 3)
* 9 a[1] = 2*(9 - 1); a[2] = 2*2*(9 - 2);a[3] = 2*2*2*(9 - 3);a[4] = 2*2*2*2(9-4);
* a.length = a[0] / 2 + 1
*
*/public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个奇数:");
int num = sc.nextInt();
int[] arr = new int[num / 2 + 1];
arr[0] = num;
System.out.print(num + " ");
for (int i = 1; i < arr.length; i++) {
arr[i] = (int) Math.pow(2, i) * (num - i);
System.out.print(arr[i] + " ");
}
}}
public static void main(String[] args) {
System.out.println("please input a odd number");
Scanner sc = new Scanner(System.in);
int odd = sc.nextInt();
//建立存储数组
int array[][] = new int[odd][odd];
//给数组赋值
for (int i = 0; i < odd; i++) {
array[i][0] = i * 2 + 1;
for (int j = 1; j <= i; j++) {
array[i][j] = array[i][j - 1] + array[i - 1][j - 1];
}
}
//输出数组
for (int i = 0; i < array.length; i++) {
for (int j = 0; j <= i; j++) {
if (array[i][j] != 0) {
System.out.print(array[i][j] + " ");
}
}
System.out.print("\n");
}
System.out.println("...");
}
}
package cn.feynman.csdn_ansawer;import java.util.Scanner;public class App {
public static void main(String[] args) {
System.out.println("please input a odd number");
Scanner sc = new Scanner(System.in);
int odd = sc.nextInt();
//建立存储数组
int array[][] = new int[odd][odd];
//给数组赋值並输出
for (int i = 0; i < odd; i++) {
array[i][0] = i * 2 + 1;
System.out.print(array[i][0]+" ");
for (int j = 1; j <= i; j++) {
array[i][j] = array[i][j - 1] + array[i - 1][j - 1];
System.out.print(array[i][j]+" ");
}
System.out.print("\n");
}
System.out.println("...");
}
}
int row = num/2+1; //行数
int[] line = new int[row];//奇数行数据
//生成行数据
for (int i = 0; i < row; i++) {
line[i] = (int) (Math.pow(2, i)*(num-i));
}
//打印行数据
for (int j : line) {
System.out.println(j);
}
}
public class Logic {
public static void main(String[] args) {
System.out.println("请输入一个基数");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int line = num / 2 + 1;
for (int j = 0; j < line; j++) {
System.out.print((int) ((num - j) * Math.pow(2, j)) + "\t");
}
}
}
System.out.println("please input the number:");
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt();
if(row % 2 == 0 && row > 0) {
System.out.println("bad input!");
return; //end the program
}
row = row / 2;
//if there are n rows, row from 0 to n-1
//the first of the column is n*2<<(n-1), n from 0 to n
StringBuffer stringBuffer = new StringBuffer();
int tempResult = 0; //temporary result, each number
for(int i = 0 ; i < row ; i ++) { //from 0 to n-2
stringBuffer.append((((i + 1) * 1 << i) + (2 << i) * (row - i)) + ", ");
//((i + 1) * 1 << i) is the first number in the column
// (2 << i) * (row - i) is the addition
}
stringBuffer.append(((row + 1) * 1 << row));
System.out.println(stringBuffer.toString());
}public static void main(String[] args) {
System.out.println("please input the number:");
Scanner scanner = new Scanner(System.in);
int row = scanner.nextInt();
if(row % 2 == 0 && row > 0) {
System.out.println("bad input!");
return; //end the program
}
row = row / 2;
//if there are n rows, row from 0 to n-1
//the first of the column is n*2<<(n-1), n from 0 to n
StringBuffer stringBuffer = new StringBuffer();
int tempResult = 0; //temporary result, each number
for(int i = 0 ; i < row ; i ++) { //from 0 to n-2
stringBuffer.append((((i + 1) * 1 << i) + (2 << i) * (row - i)) + ", ");
//((i + 1) * 1 << i) is the first number in the column
// (2 << i) * (row - i) is the addition
}
stringBuffer.append(((row + 1) * 1 << row));
System.out.println(stringBuffer.toString());
}比较土的方法,,,见谅
System.out.println("输入一个奇数");
int num = sc.nwxtInt();
int temp = 0;
for(int i = 0;i < num;i++) {
if(i == 0){
System.out.print(num + '\\t');
}else if(i == 1){
temp = num + num – 2;
System.out.print(temp + '\\t');
}else {
temp = temp + temp –(4*(i–1));
System.out.print(temp + '\\t');
}
}
public void test10(int obb){
int col = (obb/2)+1;
int[] num=new int[col];
num[0]=obb;
int temp=obb;
for(int i=1;i<col;i++)
{
num[i]=(int) ((int)temp*2-Math.pow(2, i));
temp=num[i];
System.out.println(num[i]);
}
}
{
public static void main(String[] args) {
Demo d=new Demo();
d.test10(9);
}
public void test10(int obb){
int col = (obb/2)+1;
int[] num=new int[col];
num[0]=obb;
int temp=obb;
for(int i=1;i<col;i++)
{
num[i]=(int) ((int)temp*2-Math.pow(2, i));
temp=num[i];
// System.out.println(num[i]);
}
for(int temp2:num)
{
System.out.print(temp2+" ");
}
}
}
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
//行数
int row = num / 2 + 1;
int[][] datas = new int[row][];
for (int i = 0; i < row; i++) {
//每行列数
int column = i + 1;
datas[i] = new int[column];
for (int j = 0; j < column; j++)
if (j == 0)
datas[i][j] = 2 * column - 1;
else
datas[i][j] = datas[i - 1][j - 1] + datas[i][j - 1];
}
for (int[] arr : datas)
//只输出给定行
if (arr[0] == num)
for (int data : arr)
System.out.print(data + " ");
}2楼大佬的int column = i + 1; 这一句应该改成为int column = i * 1; 这个应该是乘积吧!
Scanner scan = new Scanner(System.in);
int num = scan.nextInt();
int arr[][] = new int[num][num];
for(int i = 0 ; i< num ; i++){
arr[i][0] =2*i+1;
for(int j = 0 ; j<= i;j++){
if(j>=1){
arr[i][j] = arr[i-1][j-1]+arr[i][j-1];
}
}
}
for(int[] a : arr){
for(int res : a){
if(res!=0){
System.out.print(res+" ");
}
}
System.out.println();
}
{
Console.WriteLine("请输入一个奇数");
int n = Convert.ToInt32(Console.ReadLine());
int num = (n + 1) / 2;
List<int> m = get(num);
for (int i = 0; i < num; i++)
{
Console.Write(m[i] + " ");
}
Console.ReadLine();
}
public static List<int> get(int i) //给定当前行的数,获取当前一行的数组
{ List<int> m = new List<int>();
m.Add(2 * i - 1);
if (i > 1)
{
for (int x = 1; x < i; x++)
{
List<int> z = get(i - 1);
m.Add(m[x - 1] + z[x - 1]);//i[x-1]=get(i-1)
}
}
return m;
}
* 根据当前数组,计算下一个数组
* @param arr
* @return
*/
public static int[] nextArr(int... arr) {
int[] res = new int[arr.length + 1];
for(int i = 0, len = res.length; i < len; ++i){
if(i == 0) {
res[i] = arr[i] + 2;
} else {
res[i] = res[i - 1] + arr[i - 1];
}
}
return res;
}
/**
* 格式化输出
* @param arr
*/
public static void formatPrintln(int[] arr){
Arrays.stream(arr).forEach(d -> System.out.print(d + " "));
System.out.println();
}
@Test
public void test02(){
int[] arr = {1}; //原始数组
for(int i = 0; i < 6; i++){
formatPrintln(arr); //打印当前
arr = nextArr(arr); //获取下一行
}
}
我认为,不必使用递规,也就是不要分析本层数据跟上层的关系。
正确的分析,应该是 输入的数字跟行号和列号之间的关系。
分析如下:import java.util.Scanner;
/**
题目:
打印
1
3 4
5 8 12
7 12 20 32
9 16 28 48 80
..... 输入任意一个奇数,输出那一行的数据
思路分析:提取因式
2^0 ^1 ^2 ^3 ^4 ^5
___________________
| *1 *2 *4 *8 *16 *32
|___________________
1| 1
2| 3 2
3| 5 4 3
4| 7 6 5 4
5| 9 8 7 6 5
6| 11 10 9 8 7 6
n n-1 n-2 n-3
n/2+1 */
public class Main { public static void main(String[] args) {
while (true) {
System.out.println("\n请输入一个整数:");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int line = number / 2 + 1;
for (int i = 0; i < line; i++) {
System.out.print((int) ((number - i) * Math.pow(2, i)) + "\t");
}
} }
}结果:
请输入一个整数:
3
3 4
请输入一个整数:
5
5 8 12
请输入一个整数:
7
7 12 20 32
请输入一个整数:
9
9 16 28 48 80
/**
*
给定一个正整数n,打印第n行,每行的规律如下:
1
3 4
5 8 12
7 12 20 32
9 16 28 48 80
两条规律:
1.第i个数字 = 第i-1个数字 + 上一行第i-1个数字
2.第i个数字 - 上一行第i个数字 = 2 * (第i-1个数字 - 上一行第i-1个数字)
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int cur = 2 * n - 1, pre = cur - 2, diff = 2;
System.out.print(cur + "\t");
for (int i = 1; i < n; i++) {
cur += pre;
pre = cur - 2 * diff;
// 缓存第i个数与上一行第i个数的差,这样可以避免使用pow()
diff = cur - pre;
System.out.print(cur + "\t");
}
}
}
* 1 (0) (4) (-16) (-48)
* 3 4
* 5 8 12
* 7 12 20 32
* 9 16 28 48 80
* ..........
**/
//分析:第n行:有i个数(第列都是个等差数列a(n)=ai+(n-i)d)
//第1个数为:1+(n-1)*2 = 2*n-1---(最后的数为1*1)---------
//第2个数为:4+(n-2)*4 = 4*n-4---(最后的数为2*2)
//第3个数为12+(n-3)*8 = 8*n-12---(最后的数为4*3)
//第4个数为:32+(n-4)*16 =16*n-32 ---(最后的数为8*4)
//第5个数为:80+(n-5)*32=32*n-80---(最后的数为16*5)
//...第n行的第i个数通项公式为:a(n) = (2^i)*n-(2^(i-1))*i,共展示n个数
@Test
public void test(){ //Math.pow(a,b)指的是a的b次方
int n=5,val;
for(int i=1; i<=n; i++){
val = ((int) Math.pow(2, i))*n-(int) Math.pow(2, i-1)*i;
System.out.print(val+" ");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个奇数: ");
int a = sc.nextInt();
if(a%2 == 1) {
int leng = (a+1) / 2;
int[] shu = new int[leng];
shu[0] = a;
for(int i=1; i<leng; i++) {
shu[i] = (int)(Math.pow(2,i)*a - i*Math.pow(2,i));
}
for(int i=0; i<leng; i++) {
System.out.print(shu[i] + " ");
}
System.out.println();
}
else {
System.out.println("输入错误");
}
}
}
int[] old=new int[num];
int[] ns=new int[num];
for(int i=1;i<num+1;i++){
ns[0]=2*i-1;
System.out.print(ns[0]+" ");
for(int j=1;j<i;j++){
int re=old[j-1]+ns[j-1];
ns[j]=re;
System.out.print(re+" ");
}
System.out.println();
old=ns;
ns=new int[num];
}
System.out.println("====================");
for(int i:old){
System.out.print(i+" ");
}
}
那么,f(0)=k , f(1)= 2* f(0)-2^1,..... f(n)=2*f(n-1)-2^n, 其中 2^n表示2的n次方
代码就我不写了, 递推公式出来后面代码就很好写的了
import java.util.Scanner;public class CSDN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i = input.nextInt();
for (int x = 1; x <= (i + 1) * 0.5; x++) {
int s = (int) Math.pow(2, (x - 1)) * (i - x + 1);
System.out.print(s + "\t");
}
}}
public static void getOneRow(int start) {
for (int i = 1; (i - 1) * 2 < start; i++) {
System.out.print(getOne(start, i) + ",");
}
} public static int getOne(int start, int n) {
if (n == 1) {
return start;
} else {
return getOne(start, n - 1) + getOne(start - 2, n - 1);
}
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入首项:");
int start = sc.nextInt();
getOneRow(start);
}
}