Esempio n. 1
0
    void nextPermutation(vector<int>& nums) 
    {
        if ( nums.size() <= 1)
        {
            return;
        }
        
        int len = nums.size();
     int i = len-2;
     while( i >= 0)
    {
        if ( nums[i] < nums[i+1])
        {
            break;
        }
        i--;
    }
 
     if ( i == -1)
    {
        reverseArr(nums, 0, len-1);
            return;
    }
 
     int j = len-1;
     while( nums[j] <= nums[i])
    {
        j--;
    }

    swap(nums, i, j);
    reverseArr(nums, i+1, len-1);
    }
Esempio n. 2
0
void main() 
{
  int arr[5];
  int len = sizeof(arr)/sizeof(arr[0]);

  fillArrayRandomly(arr, len, 1, 100);

  if (DEBUG)
    printArr(arr, len);

  insertionSort(arr, len);

  if (DEBUG)
    printArr(arr, len);

  reverseArr(arr, len);

  if (DEBUG)
    printArr(arr, len);

  moveArr(arr, len, 3);

  if (DEBUG)
    printArr(arr, len);
}
Esempio n. 3
0
//十进制转换为二进制 将整数n转换为字符串arr
void toBinaryByLoop(int n,char arr[]){
	int i = 0;
	while(n/2 || n%2){
		int remain = n%2;
		arr[i] = '0' + remain;
		n/=2;
		i++;
	}
	reverseArr(arr, i);
}
Esempio n. 4
0
//整数转换成字符串
void myitoa(int n,char str[]){
	int i = 0;
	int remain;
	while(n/10 || n%10){
		remain = n%10;
		str[i] = '0' + remain;
		n /= 10;
		i++;
	}
	reverseArr(str, i);
}
Esempio n. 5
0
//十进制转换为二进制 递归实现
void toBinaryByCursion(int n,char arr[],int i){
	if(n == 0){
		//递归的最后一次翻转数组
		reverseArr(arr, i);
		return;
	}
	if(n/2 || n%2){
		int remain = n%2;
		arr[i++] = '0' + remain;
		n/=2;
		toBinaryByCursion(n, arr, i);
	}
}
Esempio n. 6
0
void getCommand(double arr[], size_t size)
{
    char buffer[100];
    CONSOLE_SCREEN_BUFFER_INFO SBInfo;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &SBInfo);
    if(SBInfo.dwCursorPosition.Y > BOTBORDER - 3)
    {
        clearZone(0, 1, RIGHTBORDER - 1, BOTBORDER - 2, COLORDEF);
        setCursorPosition(0, 1);
    }
    printf(" >> ");
    fgets(buffer, 100,stdin);
    if(!strcmp(buffer, "help\n"))
    {
        if(SBInfo.dwCursorPosition.Y > BOTBORDER - 20)
        {
            clearZone(0, 1, RIGHTBORDER - 1, BOTBORDER - 2, COLORDEF);
            setCursorPosition(0, 1);
        }
        puts(HELPINFO);
    }
    else if(!strcmp(buffer, "nullify\n"))
    {
        nullifyArr(arr, size);
        printArr(arr, size);
    }
    else if(strstr(buffer, "change"))
    {
        unsigned int index;
        int status;
        double value;
        printArr(arr, size);
        status = sscanf(buffer, "change %u %lf", &index, &value);
        if(status==2 && index < size)
        {
            changeNumByInd(arr, size, index, value);
            printArrEl(arr, size, index);
        }
        else
        {
            setConsoleColor(COLORERROR);
            puts("Inappropriate format");
            setConsoleColor(COLORDEF);
        }

    }
    else if(strstr(buffer, "random"))
    {
        double from, to;
        int status = sscanf(buffer, "random %lf %lf", &from, &to);
        if(status==2)
        {
            initRandArray(arr, size, from, to);
            printArr(arr, size);
        }
        else
        {
            setConsoleColor(COLORERROR);
            puts("Inappropriate format");
            setConsoleColor(COLORDEF);
        }
    }
    else if(!strcmp(buffer, "reverse\n"))
    {
        int i;
        reverseArr(arr, size);
        clearZone(RIGHTBORDER, 0, LENGTH - 1, 0, COLORSELECT);
        setConsoleColor(COLORSELECT);
        setCursorPosition(RIGHTBORDER + 1, 0);
        printf("REVERSED array : ");
        printArr(arr, size);
        setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1);
        puts("Press anything to reverse back...");
        getch();
        setConsoleColor(COLORSELECT);
        clearZone(RIGHTBORDER, 0, LENGTH - 1, 0, COLORWORKBACK);
        setCursorPosition(RIGHTBORDER + (LENGTH - (RIGHTBORDER))/3, 0);
        setConsoleColor(COLORWORKBACK);
        printf("Array : ");
        clearZone(0, SBInfo.dwCursorPosition.Y + 1,RIGHTBORDER - 1, SBInfo.dwCursorPosition.Y + 1, COLORDEF);
        setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1);
        reverseArr(arr, size);
        printArr(arr, size);
    }
    else if(!strcmp(buffer, "sum\n"))
    {
        double sum = findSum(arr, size);
        clearZone(0, BOTBORDER + 3, LENGTH - 1, BOTBORDER + 3, COLORWORKCOM);
        setCursorPosition(LENGTH/4, BOTBORDER + 3);
        setConsoleColor(COLORWORKCOM);
        printf("The sum of the array is : %.2f", sum);
        setConsoleColor(COLORDEF);
        setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1);
    }
    else if(!strcmp(buffer, "nnegative\n"))
    {
        int nnegative = numOfNegElements(arr, size);
        clearZone(0, BOTBORDER + 3, LENGTH - 1, BOTBORDER + 3, COLORWORKCOM);
        setCursorPosition(LENGTH/4, BOTBORDER + 3);
        setConsoleColor(COLORWORKCOM);
        printf("The number of negative elements of the array is : %i", nnegative);
        setConsoleColor(COLORDEF);
        setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1);
    }
    else if(strstr(buffer, "shift"))
    {
        unsigned int nSH;
        int status = sscanf(buffer, "shift %u", &nSH);
        if(status)
        {
            shiftArrR(arr, size, nSH);
            printArr(arr, size);
        }
        else
        {
            setConsoleColor(COLORERROR);
            puts("Inappropriate format");
            setConsoleColor(COLORDEF);
        }
    }
    else if(strstr(buffer, "cyclesh"))
    {
        unsigned int nSH;
        int status = sscanf(buffer, "cyclesh %u", &nSH);
        if(status)
        {
            cycleShiftArrR(arr, size, nSH);
            printArr(arr, size);
        }
        else
        {
            setConsoleColor(COLORERROR);
            puts("Inappropriate format");
            setConsoleColor(COLORDEF);
        }
    }
    else if(strstr(buffer, "powall"))
    {
        double power;
        int status = sscanf(buffer, "powall %lf", &power);
        if(status)
        {
            powEachElement(arr, size, power);
            printArr(arr, size);
        }
        else
        {
            setConsoleColor(COLORERROR);
            puts("Inappropriate format");
            setConsoleColor(COLORDEF);
        }
    }
    else if(!strcmp(buffer, "firstmin\n"))
    {
        int imin = getMinimumIndex(arr, size);
        printArr(arr, size);
        printArrEl(arr, size, imin);
        clearZone(0, BOTBORDER + 3, LENGTH - 1, BOTBORDER + 3, COLORWORKCOM);
        setCursorPosition(LENGTH/4, BOTBORDER + 3);
        setConsoleColor(COLORWORKCOM);
        printf("The index and value of first minimum element is : %i, %.2f", imin, arr[imin]);
        setConsoleColor(COLORDEF);
        setCursorPosition(0, SBInfo.dwCursorPosition.Y + 1);
    }
    else if(!strcmp(buffer, "swapmaxmin\n"))
    {
        int swap1, swap2;
        printArr(arr, size);
        swapLastMaxAndMin(arr, size, &swap1, &swap2);
        printArrEl(arr, size, swap1);
        printArrEl(arr, size, swap2);
    }
    else if(!strcmp(buffer, "exit\n"))
    {
        exit(EXIT_SUCCESS);
    }
    else
    {
        puts("Command not found. Check help");
    }
    getCommand(arr, size);
}