There is a string in which each character can only be 0 or 1. At most, we can choose to change k zeros into 1, and find the length of the longest continuous 1 substring after transformation.

The second question of winter vacation homework of programming in freshman semester

#include <stdio.h>
#include <math.h>
#include <string.h>
int numofone(char* num)
{
	int count = 0;
	int big = 0;
	for (int i = 0;i < strlen(num);i++)
	{
		if (num[i] == '1')
		{
			count++;
		}
		if (big < count)
		{
			big = count;
		}
		if (num[i] == '0')
		{
			count = 0;
		}
	}
	return big;//10010011110001010010010
}

bool oneexits(char* num, int i, int k)
{
	for (int i = 0;i < strlen(num);i++)
	{
		if (num[i] == '0' && k <= 0)
		{
			return false;
		}
	}
	return true;
}

void zerotoone(char* num, int k)
{
	int big = numofone(num);
	for (int i = 0;i < strlen(num);i++)
	{
		if (num[i] == '1')
		{
			int count = k;
			char newnum[100];
			int n = 0;
			for (int j = i;j<strlen(num);j++,n++)
			{
				if (num[j] == '1') {
					newnum[n] = num[j];
				}
				if (num[j] == '0')
				{
					if (count > 0) {
						count--;
						newnum[n] = '1';
					}
					else { newnum[n] = num[j]; }
				}
			}
			newnum[n] = '\0';
			int pro = numofone(newnum);
			if ( pro> big)
			{
				big = pro;
			}
		}
		if (num[i] == '0'&&oneexits(num,i,k))
		{
			int count = k;
			char newnum[100];
			int n = 0;
			for (int j = i;j < strlen(num);j++, n++)
			{
				if (num[j] == '1') {
					newnum[n] = num[j];
				}
				if (num[j] == '0')
				{
					if (count > 0) {
						count--;
						newnum[n] = '1';
					}
					else { newnum[n] = num[j]; }
				}
			}
			newnum[n] = '\0';
			int pro = numofone(newnum);
			if (pro > big)
			{
				big = pro;
			}
		}
	}
	printf("%d", big);
}

int main()
{
	char num[100];
	printf("Please enter your string:");
	scanf("%s", num);
	int k = 0;
	printf("Please enter the 0 you want to change->1 Number of:");
	scanf("%d", &k);
	zerotoone(num, k);
	return 0;
}
		
	


The code may not be completely correct. Several general examples have been tested and can only be used as reference answers.

This assignment still has a problem, that is, the middle discussion can be designed as a function, so that there will be no redundancy like the above code.

Here is my personal understanding of the problem:

If I want to find the longest continuous 1 string, of course, the string I'm looking for can't appear in a pile of zeros (unless the string is 0 or other extreme conditions), so I only need to consider two problems when traversing the string

1. If I check the i-th bit of this string and find that it is 1, I can create a new string and store it from the place of 1. If it is 1, it will be recorded directly. If it is 0, I will use my magic wand times of 0 - > 1 until I record the end of my original string.

         



            int count = k;
			char newnum[100];
			int n = 0;
			for (int j = i;j<strlen(num);j++,n++)
			{
				if (num[j] == '1') {
					newnum[n] = num[j];
				}
				if (num[j] == '0')
				{
					if (count > 0) {
						count--;
						newnum[n] = '1';
					}
					else { newnum[n] = num[j]; }
				}
			}
			newnum[n] = '\0';

2. If I detect the j-th bit of the original string and find that it is 0, I can't skip it at this time, for example: 11100011110, k=2

In this example, if I skip this 0, I will find that the output result is 5 instead of the 6 we see

When solving this problem, I designed a new function to find out whether it will appear 1 before my magic wand runs out, so that I can connect two consecutive strings of 1, or extend the super long 1 string at the end of an original string.

If it is true, I will redo the code in case 1 (it should be possible to write another function here, but copy and paste is faster)

If not, I will regard it as a cloud floating quietly in the sky.

bool oneexits(char* num, int i, int k)
{
	for (int i = 0;i < strlen(num);i++)
	{
		if (num[i] == '0' && k <= 0)
		{
			return false;
		}
	}
	return true;
}

Personally, I think the main contradiction to solve this problem is to grasp the above two situations. If there is anything wrong, please correct it.

A program problem that fully reflects the process programming.

Keywords: C

Added by electricblue on Sat, 12 Feb 2022 07:26:11 +0200