iT邦幫忙

0

ITSA 2024五月程式能力線上自我評量problem2

  • 分享至 

  • xImage

題目
https://ithelp.ithome.com.tw/upload/images/20240713/20153147cqWgHEYHtF.png
我的程式碼

import java.util.*;
public class Main
{
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		int[] ans = {1,2,3,4};
		int ch;
		for(int x = 0; x < n; x++){
		    int[] a = {1,2,3,4};
    		int[] b = {1,2,3,4};
    		int[] c = {1,2,3,4};
    		int[] d = {1,2,3,4};
		    for(int i = 0; i < 4; i++){
		        char str = input.next().charAt(0);
		        for(int j = 0; j < 4; j++){
		            ch = input.nextInt();
		            if(ch == 0){
		                break;
		            }
    		        if(str == 'A'){
    		                for(int s = 0; s < 4; s++){
    		                    if(ch == ans[s]){
    		                        a[s] = 0;
    		                        break;
    		                    }
    		                }
    		        }
    		        if(str == 'B'){
    		                for(int s = 0; s < 4; s++){
    		                    if(ch == ans[s]){
    		                        b[s] = 0;
    		                        break;
    		                    }
    		                }
    		        }      
    		        if(str == 'C'){
    		                for(int s = 0; s < 4; s++){
    		                    if(ch == ans[s]){
    		                        c[s] = 0;
    		                        break;
    		                    }
    		                }
    		        }
    		        if(str == 'D'){
    		                for(int s = 0; s < 4; s++){
    		                    if(ch == ans[s]){
    		                        d[s] = 0;
    		                        break;
    		                    }
    		                }
    		        }
    		    }
		    }
    		 int[] sol = {0,0,0,0};
    		 int flag = 0, cnt = 0;
    		 for(int i1 = 0; i1 < 4; i1++){
    		     if(a[i1] == 0){
    		         continue;
    		     }
    		     sol[0] = a[i1];
    		     for(int j1 = 0; j1 < 4; j1++){
    		         if(b[j1] == 0){
    		             continue;
    		         }
    		         if(b[j1] != sol[0]){
    		            sol[1] = b[j1];
    		         }
    		         else{
    		                 continue;
    		         }
    		         for(int k1 = 0; k1 < 4; k1++){
    		             if(c[k1] == 0){
    		                 continue;
    		             }
    		             if(c[k1] != sol[0] && c[k1] != sol[1]){
    		                 sol[2] = c[k1];
    		             }
    		             else{
    		                 continue;
    		             }
    		             for(int x1 = 0; x1 < 4; x1++){
    		                 if(d[x1] == 0){
    		                     continue;
    		                 }
    		                 if(d[x1] != sol[0] && d[x1] != sol[1] && d[x1] != sol[2]){
    		                     sol[3] = d[x1];
    		                     if(cnt == 0){
    		                         System.out.println("ABCD");
    		                         cnt++;
    		                     }
    		                     System.out.println(sol[0]*1000+sol[1]*100+sol[2]*10+sol[3]);
    		                     flag = 1;
    		                 }
    		                 else{
    		                     continue;
    		                 }
    		             }
    		             
    		         }
    		     }
    		     
    		 }
    		 if(flag == 0){
    		     System.out.println("No");
    		 }
    		 
         System.out.println();
		 }
	}
}

以上是我的程式碼,但是跑程式有幾筆測資不通過,不知道怎麼修正QQ
https://ithelp.ithome.com.tw/upload/images/20240713/201531473Mpap4fxuN.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
海綿寶寶
iT邦大神 1 級 ‧ 2024-07-13 21:12:11

不知道測資的情況下
只能說設計測資的人「猜中」你程式的bug

就算能從你程式裡肉眼看出bug
也不保證就是測資沒通過的原因

我替你問Google Gemini
以下是他的建議

I can help you understand the potential issues in the provided Java code:

**1. Redundant Array Creation:**

The code creates four new integer arrays (`a`, `b`, `c`, and `d`) inside the inner loop `for(int x = 0; x < n; x++)`  for every iteration. This is inefficient as it creates unnecessary objects and consumes memory. You can consider using a single array and modifying its elements within the loop.

**2. Nested Loops and Early Termination:**

The code uses four nested loops (`for(int i = 0; i < 4; i++)` etc.) to iterate through the elements of arrays `a`, `b`, `c`, and `d`.  However, the inner `if(ch == 0)` statement within each loop exits the loop immediately if `ch` is 0. This might lead to unnecessary iterations within the outer loops. 

**3. Looping through Fixed-Size Array:**

The loop conditions `for(int i = 0; i < 4; i++)` iterate four times regardless of the actual size of the arrays (`a`, `b`, `c`, and `d`). It might be better to use the array's length (`a.length`) for a more flexible approach.

**4. Solution Array and Flag:**

The code uses an array `sol` to store the solution and a flag `flag` to indicate if a solution is found.  There might be a more efficient way to represent the solution without an extra array.

**5. Unnecessary String Conversion:**

The code uses `input.next().charAt(0)` to convert the input from the user (which might be a number) to a character. It seems the code expects a single character ('A', 'B', 'C', or 'D') but converting a number to a character might not be necessary depending on the intended input format.

These are some potential areas for improvement in the code.  For a more specific analysis, it would be helpful to know the intended functionality of the code and the expected input format. 

另外有一種笨解法
一開始把所有排列組合產生成一個陣列
然後跑迴圈,逐項檢查A,B,C,D的限制
都符合者才顯示(合法答案)

好的我在嘗試看看!謝謝你

0

2024 年 5 月 ITSA 程式設計能力線上評估問 poppy playtime chapter 3 題重點在於特定的編碼問題,該問題要求參與者管理陣列並實現邏輯以根據使用者輸入過濾和輸出結果。

我要發表回答

立即登入回答