배열 요소를 왼쪽으로 회전하고 다른 배열에 저장하는 C # 함수를 만들려고합니다.
inputl = 회전 된 배열이 저장되어야하는 배열,
inputr = 회전 할 배열
shift = 교대 횟수.
어떤 값 으로든 시프트 값을 전달하더라도 회전은 한 번만 발생합니다. 도와주세요.
public static void LeftShift(int[] inputl,int[] inputr, int shift)
{
for (int j = 0; j < shift; j++)
{
int temp = inputr[0];
var last = inputr.Length - 1;
for (int i = 0; i < last; i++)
{
inputl[i] = inputr[i+1];
}
inputl[last] = temp;
}
}
}
문제에 대한 해결책은 다음과 같습니다.
public static void shift(int[]al, int[]ar,int shift)
{
shift=ar.Length-shift%ar.Length;
for (int i = 0; i < ar.Length; i++)
al[(i+shift)%al.Length]=ar[i];
}
시프트 메서드 내에서 첫 번째 줄을 제거하면 출력이 오른쪽 시프트가됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다