C 堆栈溢出。要存储的大号码
当我有20个as passlength(字符串行)和这些字符串中的26个字母时,我遇到了一个问题。现在它打印出了那个例子中的acaaaaaaaaaaaaa,26个字母是字母表。要存储的置换量太大了。他要求的最高排列为1048576排列。我想打印第n个(强度)排列C 堆栈溢出。要存储的大号码,c,C,当我有20个as passlength(字符串行)和这些字符串中的26个字母时,我遇到了一个问题。现在它打印出了那个例子中的acaaaaaaaaaaaaa,26个字母是字母表。要存储的置换量太大了。他要求的最高排列为1048576排列。我想打印第n个(强度)排列 如何避免跳过次数过大的堆栈溢出问题???我试着把int改成long。我已经使用printf来查看置换的数量,并且我得到了与int相同的数量 #include<stdio.h> #include<string.h>
如何避免跳过次数过大的堆栈溢出问题???我试着把int改成long。我已经使用printf来查看置换的数量,并且我得到了与int相同的数量
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define ALPHA 28
typedef struct {
int yourpass;
char letters[ALPHA];
int temp;
int skips;
} password;
//declare functions
void FindVariations(int passlength, int strength, password *combo, int j, int x);
void magical(password *combo, int passlength);
int main() {
int i, j, d, cases, passlength;
int strength;
password* combo;
combo = malloc(ALPHA*sizeof(password));
// enter number of passwords to compute
scanf("%d", &cases);
for (i=0; i<cases; i++) {
//enter number of strings/length of password
scanf(" %d ", &passlength);
// input the letters for password
for (j=0; j<passlength; j++) {
scanf(" %s ", combo[j].letters);
combo[j].temp = strlen(combo[j].letters);
}
scanf("%d", &strength);
// find the total nnumber of permutations
magical(combo, passlength);
// find the permutation to print
FindVariations( passlength, strength, combo, 1, 0);
// print the wanted password
for(d=0; d<passlength; d++) {
printf("%c", combo[d].letters[combo[d].yourpass]);
}
printf("\n");
}
free(combo);
return 0;
}
void magical(password *combo, int passlength) {
int i;
int total=0;
for(i=(passlength-1); i>=0; i--) {
if(i==(passlength-1)) {
combo[i].skips=1;
total=combo[i].temp;
}
else {
combo[i].skips=total;
total*= combo[i].temp;
}
}
}
void FindVariations(int passlength, int strength, password *combo, int j, int x) {
combo[x].yourpass = 0;
if(x==passlength){
return;
}
if (x<passlength) {
while(j<=strength-combo[x].skips) {
combo[x].yourpass+=1;
j+=combo[x].skips;
}
}
FindVariations( passlength, strength, combo, j, x+1);
}
#包括
#包括
#包括
#定义ALPHA 28
类型定义结构{
int yourpass;
字符字母[ALPHA];
内部温度;
int跳过;
}密码;
//声明函数
void find变量(int passlength、int strength、password*combo、int j、int x);
无效魔法(密码*组合,int passlength);
int main(){
int i,j,d,cases,passlength;
内在力量;
密码*组合;
combo=malloc(ALPHA*sizeof(密码));
//输入要计算的密码数
scanf(“%d”和案例);
对于(i=0;i你知道什么是堆栈溢出吗?在你的例子中,这不是存储一个大数字的问题,而是通过使用大量递归函数调用来使用一卡车内存的问题
每次进行函数调用时,编译器都会将函数的参数和内部变量的空间添加到程序的堆栈内存中。如果在函数调用中进行大量函数调用,这会使程序的堆栈内存超出允许的限制,从而产生堆栈溢出错误
然而,在您的程序中,一旦进行了FindVariations
函数调用,就不需要以前的变量值,因此实际上不需要递归函数调用导致的内存开销。这称为
防止堆栈溢出的最简单解决方案是将递归解决方案转换为迭代解决方案
void FindVariations(int passlength, int strength, password *combo, int j, int x)
{
for (combo[x].yourpass = 0; x < passlength; x++)
{
while(j <= strength-combo[x].skips)
{
combo[x].yourpass+=1;
j+=combo[x].skips;
}
}
}
void find变量(int passlength、int strength、password*combo、int j、int x)
{
对于(组合[x]。yourpass=0;x while(j)问题是什么?如何避免堆栈溢出?26*26*26…等是一个需要存储的大数字。因为只想打印一个给定的置换(int-strength)。您需要更新您的问题,了解您正试图做什么,您正面临什么问题,以及您试图解决这些问题的方法。