第二章:算法

习题

2.先在磁盘里用二分查找,分成0和1两部分,之后必然有一份数量较少,此部分缺少元素,在此部分再次二分查找,选择较少的部分继续,直到较少的一份达到内存要求,进入内存,利用位图

3.在于发现循环移动的不变性可以看作是两部分交换的通用情况,简单的两部分交换,两部分的大小一致,可以直接交换,通用情况是两部分大小不一致,因此需要利用不变性转化为通用情况,最后在进行两部分的交换

#include <stdio.h>

char str[] = "abcdefg";
void swap(int a, int b, int m)
{
	int i = 0;
	int tmp;
	for (i; i < m; i++)
	{
		tmp = str[a+i];
		str[a+i] = str[b+i];
		str[b+i] = tmp;
	}
}
int main()
{
	printf("%p\n", str);
	int rotdist;
	int n = 7;
	scanf("%d", &rotdist);
	if (rotdist == 0 || rotdist == n)
		return 0; //we won't change the row
	int p = rotdist % n;
	int i = p;
	int j = n - p;
	//as we need to use AB changes to BA, we need to from the normal case to that special case
	//** i and j refers to the two parts' lengths
	//we always use the small parts the exchange in order to be safe
	//the two parts we will change is decided by the invarient of such a cycle operation
	// we end when we get the spacial case
	while (i != j)
	{

		if (i > j)
		{
			swap(p-i, p, j);//j part is safe, we exchange the length of j
			i -= j;
		}
		else
		{
			swap(p-i, p+j-i, i);//i part is safe, we exchange the length of i
			printf("%s\n", str);
			j -= i;
		}
		
	}
	swap(p-i, p, i);//this is where we find the 
	printf("%s", str);
}

7

File is a chunk of memory, we cannot change it line by line.

#include <stdio.h>
#include <stdio.h>
int main ( void )
{
    static const char filename[] = "m.txt";
    FILE *file = fopen ( filename, "r" );
	FILE *ff = fopen("new.txt", "w");
    int i = 0;
    if ( file != NULL )
    {
        char line [ 128 ]; /* or other suitable maximum line size */
        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {
			fprintf(ff, "%d %d %s", i % 3 + 1, i / 3 + 1, line);
			i++;
        }
    fclose ( file );
    }
    else
    {
        perror ( filename ); /* why didn't the file open? */
    }
    return 0;
}

8

nlogk解法是分为多个k元集合,分别排序

补充数组拷贝

int main()
{
  // This works in C and C++
  int a[] = { 1, 2, 3, 4 };
  int b[4];
  memcpy(b, a, 4*sizeof(int)); // int is a POD

  // This is the preferred method to copy raw arrays in C++ and works with all types that can be copied:
  std::copy(a, a+4, b);

  // In C++11, you can also use this:
  std::copy(std::begin(a), std::end(a), std::begin(b));

  // use of vectors
  std::vector<int> va(a, a+4); // copies the content of a into the vector
  std::vector<int> vb = va;    // vb is a copy of va

  // this initialization is only valid in C++11:
  std::vector<int> vc { 5, 6, 7, 8 }; // note: no equal sign!

  // assign vc to vb (valid in all standardized versions of C++)
  vb = vc;

  //alternative assignment, works also if both container types are different
  vb.assign(vc.begin(), vc.end());

  std::vector<int> vd; // an *empty* vector

  // you also can use std::copy with vectors
  // Since vd is empty, we need a `back_inserter`, to create new elements:
  std::copy(va.begin(), va.end(), std::back_inserter(vd));

  // copy from array a to vector vd:
  // now vd already contains four elements, so this new copy doesn't need to
  // create elements, we just overwrite the existing ones.
  std::copy(a, a+4, vd.begin());

  // C++11 only: Define a `std::array`:
  std::array<int, 4> sa = { 9, 10, 11, 12 };

  // create a copy:
  std::array<int, 4> sb = sa;

  // assign the array:
  sb = sa;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值