本文介绍了将C更改为C ++代码,任何人都可以提供帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

毫无疑问

毫无疑问

毫无疑问

毫无疑问

无疑问

毫无疑问

毫无疑问



我尝试过:



没有

no question
no question
no question
no question
no question
no question
no question

What I have tried:

nothing

推荐答案

#include <stdio.h>
#include <string.h>

// Prints the minimum number that can be formed from
// input sequence of I's and D's
void PrintMinNumberForPattern(const char * arr)
{
    // Initialize current_max (to make sure that
    // we don't use repeated character
    int curr_max = 0;

    // Initialize last_entry (Keeps track for
    // last printed digit)
    int last_entry = 0;

    int j;

		size_t length = strlen(arr);

    // Iterate over input array
		int i;
    for (i=0; i<length; i++)
    {
        // Initialize 'noOfNextD' to get count of
        // next D's available
        int noOfNextD = 0;

        switch(arr[i])
        {
        case 'I':
            // If letter is 'I'

            // Calculate number of next consecutive D's
            // available
            j = i+1;
            while (arr[j] == 'D' && j < length)
            {
                noOfNextD++;
                j++;
            }

            if (i==0)
            {
                curr_max = noOfNextD + 2;

                // If 'I' is first letter, print incremented
                // sequence from 1

								printf(" %d", ++last_entry);
								printf(" %d", curr_max);

                // Set max digit reached
                last_entry = curr_max;
            }
            else
            {
                // If not first letter

                // Get next digit to print
                curr_max = curr_max + noOfNextD + 1;

                // Print digit for I
                last_entry = curr_max;
								printf(" %d", last_entry);
            }

            // For all next consecutive 'D' print 
            // decremented sequence
						int k;
            for (k=0; k<noOfNextD; k++)
            {
								printf(" %d", --last_entry);
                i++;
            }
            break;

        // If letter is 'D'
        case 'D':
            if (i == 0)
            {
                // If 'D' is first letter in sequence
                // Find number of Next D's available
                j = i+1;
                while (arr[j] == 'D' && j < length)
                {
                    noOfNextD++;
                    j++;
                }

                // Calculate first digit to print based on 
                // number of consecutive D's
                curr_max = noOfNextD + 2;

                // Print twice for the first time

								printf(" %d %d", curr_max, (curr_max -1));

                // Store last entry
                last_entry = curr_max - 1;
            }
            else
            {
                // If current 'D' is not first letter

                // Decrement last_entry
								printf(" %d", last_entry -1);
                last_entry--;
            }
            break;
        }
    }
		printf("\n");
}


int main()
{
	PrintMinNumberForPattern("IIDDIIDDDDD");
	return 0;
}


// C++
cout << " " << ++last_entry;
cout << " " << curr_max;
cout << " " << curr_max << " " << curr_max - 1;
cout << endl;
// C
printf(" %d", ++last_entry);
printf(" %d", curr_max);
printf(" %d %d", curr_max, curr_max - 1);
printf("\n");



从我的解决方案中读取链接,了解C和C ++函数的工作原理。


Read the links from my solution to understand how the C and C++ functions work.


这篇关于将C更改为C ++代码,任何人都可以提供帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:02
查看更多