习题
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;
}