iT邦幫忙

0

程式註解

  • 分享至 

  • xImage

各位大老 小弟想請問這是做正整數分解的程式功能是比如輸入4就會把分解數量加起來如下圖
輸入是範圍是(20>n>1)
4
3 1
2 2
2 1 1
1 1 1 1
所以輸出為5
可以幫忙看這程式如何做註解嗎感謝各位

package test1;
import java.util.*;  
import java.lang.*;  
import java.io.*;
import java.util.Scanner;
public class test8 {
	public static void main(String[] args) 
    {  
        Scanner aa = new Scanner(System.in);  
        while(aa.hasNext())  
        {  int n = aa.nextInt();  
            System.out.println(f(n, 1));
        }
    }
        private static int f(int n, int f)  
        {  
            if(n==f || n < 2*f)  
            {  
            	return 1;   
            }                    
            else  
            {
            int num = 0;  
            	for(int i = f;i < n;i++)  
                {
            		if(n - i >= i)   
                    {   
                	num = num + f(n-i, i);
                	}
                }   
            	return num + 1; }
            }
        }
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
3
0
YC
iT邦研究生 2 級 ‧ 2022-05-27 22:54:02

好的程式/參數命名,不太需要註解。

test8/aa/f 不知道是在幹嘛。
main/Scanner/hasNext/nextInt/println 一看就知道/猜到是幹嘛用的。

    public static void main(String[] args) {
        Scanner 掃描 = new Scanner(System.in);
        while(掃描.hasNext()) { 
            int 輸入 = 掃描.nextInt();
            System.out.println(正整數分解(輸入, 1));
        }
    }
    private static int 正整數分解(int 整數, int f) {
        if(整數 == f || 整數 < 2f) {
            return 1;
        } else {
            int 次數 = 0;
            for(int i = f; i < 整數; i++) {
                if(整數 - i >= i) {
                    次數 = 次數 + 正整數分解(整數-i, i);
                }
            }
            return 次數 + 1; 
        }
    }

目前還沒想到f要怎麼命名

0
rofellos
iT邦新手 2 級 ‧ 2022-05-30 09:42:01

你把你的題目當註解就好了

我要發表回答

立即登入回答