다음의 SPOJ에 관한 문제를 해결하고 있습니다. 간단한 삽입 정렬 알고리즘입니다. 내 자바 코드가 작동하지만 C 코드가 잘못된 대답을주고있다. 내가 뭘 잘못하고 있니?C 코드가 틀린 답을 주지만, 자바 코드가 spoj에 정확한 답을줍니다.
도와주세요 감사 많은 ... :)
자바 코드
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t > 0) {
int n = Integer.parseInt(br.readLine());
String str = br.readLine();
String arr[] = str.split(" ");
int inputArr[] = new int[n];
for (int i = 0; i < n; i++) {
inputArr[i] = Integer.parseInt(arr[i]);
}
int key = 0;
int count = 0;
for(int i = 1; i < n; i++) {
key = inputArr[i];
int j = i - 1;
while (j >= 0 && inputArr[j] > key) {
inputArr[j + 1] = inputArr[j];
j = j - 1;
count++;
}
inputArr[j + 1] = key;
}
System.out.println(count);
t--;
}
}
}
C 코드
#include<stdio.h>
int main() {
int t=0;
scanf("%d",&t);
while(t > 0) {
int n=0;
scanf("%d",&n);
int arr[n];
int key=0;
for(int i=0; i<n; i++) {
scanf("%d",&arr[i]);
}
int count=0;
int j=0;
for(int i=1; i<n; i++) {
key = arr[i];
j = i - 1;
while(j>=0&&arr[j]>key) {
arr[j+1]=arr[j];
count++;
j = j-1;
}
arr[j+1]=key;
}
printf("%d",count);
t--;
}
return 0;
}
무엇이 잘못 되었나요? 뭐가 맞습니까? 당신의 의견은 무엇이며 expected ouput은 무엇입니까? 너는 실제로 무엇을 보느냐? –
스왑 조작이 삽입 정렬로 수행 된 횟수 –
Java 코드는 개행을 추가하는'println'을 사용하지만 C 코드에는 출력 'printf'에'\ n '이 없습니다. – aragaer