Tag: 算法

排序2个大型数组

我还没有采用数据结构和算法类,我在尝试做的事情上遇到了一些麻烦。 我有2个大数组,1个是大约80k-100k字的char ,第二个是具有相同整数量的int数组(例如,用words[502]写的words[502]其出现的数量用integers[502]写成integers[502] )。 我必须对它们进行排序,以便它的相应单词的最大整数是第一个,第二个第二个等等,是否可以不使用冒泡排序(这对于这些数字来说太慢)? void bubble_sort(int n) { int i,j,temp; char tempwordy[40]={0}; for(i=1;i< n;i++) { for(j=0;jcounters[j+1]) { temp=counters[j]; counters[j]=counters[j+1]; counters[j+1]=temp; strcpy(tempwordy,words[j]); strcpy(words[j],words[j+1]); strcpy(words[j+1],tempwordy); } } } }

不兼容的AES实施?

我已经从这个站点编译了一些AES实现代码,它应该是128位密钥加密。 我测试了一起工作正常的加密/解密程序。 但是,如果我用上面提到的代码加密任何东西,然后尝试通过linux内置的openssl工具解密它,我就是无法解密它,它甚至会记录我的错误幻数错误。 同样,如果我使用openssl加密任何东西并尝试使用代码解密将无法正常工作。 我试过用cbc ecb。 如果他们都在实施AES,它不应该以同样的方式工作吗?

最长增加子序列中的荒谬条件

/* A Naive recursive implementation of LIS problem */ #include #include /* To make use of recursive calls, this function must return two things: 1) Length of LIS ending with element arr[n-1]. We use max_ending_here for this purpose 2) Overall maximum as the LIS may end with an element before arr[n-1] max_ref is used this […]

指针指针的内存泄漏

我在这个网页上使用Dijkstra算法和邻接列表。 由于我想多次构建不同的图形并计算最短路径,因此我添加了一些“自由”命令来释放内存。 但是,在多次迭代后,内存使用量仍会增加 这是我修改过的代码: // C / C++ program for Dijkstra’s shortest path algorithm for adjacency // list representation of graph #include #include #include // A structure to represent a node in adjacency list struct AdjListNode { int dest; int weight; struct AdjListNode* next; }; // A structure to represent an adjacency liat struct AdjList […]

使用备用最小值和最大值对数组进行排序

给定一个数组,我被要求以某种方式对其进行排序,以便最小值首先是最大值第二,第二个最小值第三,依此类推。 但是,当我输入值时,我没有得到所需的输出。 我今天参加考试,对此表示赞赏。 #include int main() { int i,j,k,a[6],temp,min; for(i=0;i<6;i++) scanf("%d",&a[i]); for(j=0;j<6;j++) { if(j%2==0) { min=a[j]; for(k=j;k<6;k++) { if(a[k++]<min) min=a[k++]; } temp=a[j]; a[j]=min; min=temp; } else { min=a[j]; for(k=j;kmin) min=a[k++]; } temp=a[j]; a[j]=min; min=temp; } printf(“%d “,a[j]); } }

如何在PHP中用不到1分钟的时间计算0到100000000之间的素数?

请帮我计算介于0到100000000之间的素数因为我用来写但它的工作速度很慢: 这是我的代码: $n =100000000; $answer =0; for ($i = 2, $j = 2; $i <= $n; $i++) { for ($j = 2; $j < $i; $j++) { if ($i % $j == 0) { break; } } if ($j == $i) { $answer++; } } echo $answer . PHP_EOL;

在x86上以32位块实现类似学校的划分

假设我有两个大数字(定义如下),我想通过回退到x86 avaliable算法来实现它们的划分 0008768376 – 1653656387 – 0437673667 – 0123767614 – 1039873878 – 2231712290/0038768167 – 3276287672 – 1665265628 C = A / B 这些数字存储为32位无符号整数的向量。 第一个,A,是6-unsigned-int向量,B是3-unsigned-int长向量[这个字段中的每一个我自己命名为’digit’或’field’] 得到的C将是一些3-unsigned-int向量,但要计算它我需要回退到一些可用的x86(32位模式,虽然我也可以听到x64,但这是次要的)算术 告诉我如何计算至少第一个,最重要的C结果矢量字段.. 怎么做?

摩根和一个字符串算法

我正在尝试以下链接中的问题: https://www.hackerrank.com/challenges/morgan-and-a-string/problem 杰克和丹尼尔是朋友。 它们都像字母,特别是大写字母。 他们正在从报纸上剪下大写字母,并且每个人都将他们的信件集合存储在不同的堆栈中。 美好的一天,摩根访问了杰克和丹尼尔。 他看到了他们的藏品。 摩根想知道由这两个系列组成的字典最小字符串是什么。 当它位于堆栈顶部时,他可以从集合中收集一封信。 此外,摩根希望使用男孩collections中的所有字母。 我的代码如下: #include #include #include #include int main() { int flag,choice; long n,i,j=0,k=0,x,y; char *a,*b; a=(char*)malloc(sizeof(char)*100000); b=(char*)malloc(sizeof(char)*100000); scanf(“%ld”,&n); for(i=0;i<n;i++){ j=k=0; scanf("%s%s",a,b); x=strlen(a); y=strlen(b); while(x<y)a[x++]='Z'+1; while(y<x)b[y++]='Z'+1; a[x]=b[y]='\0'; while(j<x && k<y && (a[j]!=('Z'+1) && b[k]!=('Z'+1))){ if(strcmp(a+j,b+k)<0)printf("%c",a[j++]); else printf("%c",b[k++]); } while(j<x && a[j]!=('Z'+1))printf("%c",a[j++]); while(k<y && b[k]!=('Z'+1))printf("%c",b[k++]); printf("\n"); } return […]

如何改进基数排序的这种实现?

我正在实现一个2字节的基数排序。 概念是使用Counting Sort,对整数的低16位进行排序,然后对高16位进行排序。 这允许我在2次迭代中运行排序。 我的第一个概念是试图找出如何处理否定。 由于符号位将被翻转为负数,然后以hexforms,这将使负数大于正数。 为了解决这个问题,我在符号位为正时将其翻转,以使[0,2 bil] = [128 000 000 000,255 255 …)。 当它是负数时,我将所有位翻转,使其范围为(000 000 ..,127 255 ..)。 这个网站帮助我了解这些信息。 为了完成它,我会根据传递将整数分成顶部或底部16位。 以下是允许我这样做的代码。 static uint32_t position(int number, int pass) { int mask; if (number > 31) | 0x80000000; uint32_t out = number ^ mask; return pass == 0 ? out & 0xffff : (out >> […]

有没有办法改进这个函数,用malloc分配的字符串中的另一个字符串替换子串的出现?

我是C的新手,我决定创建一个名为str_replace的函数,它替换使用malloc创建的字符串内的字符串。 它似乎工作,但任何人都可以找到任何改进的余地。 任何建议将被认真考虑。 我想知道人们是否认为找出计算新字符串大小的出现次数是一个好主意,如果我使用指针有意义。 #include #include #include char * str_replace(char * string,char * find,char * replace){ //Replaces each occurence of a particular string inside a malloc-made string with another string char * pos = string; size_t replace_size = strlen(replace); size_t find_size = strlen(find); size_t excess = replace_size – find_size; //Get number of occurences int x […]