向C++、C语言高手询问----程序中的ansNum去哪了?
问题描述:程序前面一片和谐,答案是对了,结果也求了出来。但是在最后sort排序的时候却发现ansNum归零了!求高手指教为什么会这样?代码:#include<bits/s...
问题描述:程序前面一片和谐,答案是对了,结果也求了出来。但是在最后sort排序的时候却发现ansNum归零了!求高手指教为什么会这样?
代码:
#include <bits/stdc++.h>
using namespace std;
#define MAXL 100
struct LAMP {
int light[MAXL];
LAMP() {
memset(this->light, 0, sizeof(this->light));
}
};
int n;
int c;
LAMP needAns;
LAMP ans[MAXL];
int ansNum;
int DFS(int, LAMP);
bool camp(LAMP, LAMP);
bool same(LAMP, LAMP);
int main() {
int x;
scanf("%d%d", &n, &c);
memset(needAns.light, -1, sizeof(needAns.light));
while (scanf("%d", &x) == 1 && x != -1) needAns.light[x] = 1;
while (scanf("%d", &x) == 1 && x != -1) needAns.light[x] = 0;
LAMP begin;
for (int i = 1; i <= n; i++) begin.light[i] = 1;
DFS(0,begin);
printf("%d\n", ansNum);
sort(ans + 1, ans + ansNum + 1, camp);
printf("%d\n", ansNum);
return 0;
}
int DFS(int t, LAMP now) {
if (t == c) {
for (int i = 1; i <= n; i++) {
if (needAns.light[i] != -1 && needAns.light[i] != now.light[i]) return 0;
}
ans[++ansNum] = now;
return 0;
}
LAMP go = now;
for (int i = 1; i <= n; i++) go.light[i] = !go.light[i];
DFS(t + 1, go);go = now;
for (int i = 1; i <= n; i += 2) go.light[i] = !go.light[i];
DFS(t + 1, go);go = now;
for (int i = 2; i <= n; i += 2) go.light[i] = !go.light[i];
DFS(t + 1, go);go = now;
for (int i = 1; i <= n; i += 3) go.light[i] = !go.light[i];
DFS(t + 1, go);
}
bool camp(LAMP x, LAMP y) {
for (int i = 1; i <= n; i++) {
if (x.light[i] != y.light[i]) return x.light[i] < y.light[i];
}
return true;
}
bool same(LAMP x, LAMP y) {
for (int i = 1; i <= n; i++) {
if (x.light[i] != y.light[i]) return false;
}
return true;
}
缩进粘贴后就没了,不要建议。 展开
代码:
#include <bits/stdc++.h>
using namespace std;
#define MAXL 100
struct LAMP {
int light[MAXL];
LAMP() {
memset(this->light, 0, sizeof(this->light));
}
};
int n;
int c;
LAMP needAns;
LAMP ans[MAXL];
int ansNum;
int DFS(int, LAMP);
bool camp(LAMP, LAMP);
bool same(LAMP, LAMP);
int main() {
int x;
scanf("%d%d", &n, &c);
memset(needAns.light, -1, sizeof(needAns.light));
while (scanf("%d", &x) == 1 && x != -1) needAns.light[x] = 1;
while (scanf("%d", &x) == 1 && x != -1) needAns.light[x] = 0;
LAMP begin;
for (int i = 1; i <= n; i++) begin.light[i] = 1;
DFS(0,begin);
printf("%d\n", ansNum);
sort(ans + 1, ans + ansNum + 1, camp);
printf("%d\n", ansNum);
return 0;
}
int DFS(int t, LAMP now) {
if (t == c) {
for (int i = 1; i <= n; i++) {
if (needAns.light[i] != -1 && needAns.light[i] != now.light[i]) return 0;
}
ans[++ansNum] = now;
return 0;
}
LAMP go = now;
for (int i = 1; i <= n; i++) go.light[i] = !go.light[i];
DFS(t + 1, go);go = now;
for (int i = 1; i <= n; i += 2) go.light[i] = !go.light[i];
DFS(t + 1, go);go = now;
for (int i = 2; i <= n; i += 2) go.light[i] = !go.light[i];
DFS(t + 1, go);go = now;
for (int i = 1; i <= n; i += 3) go.light[i] = !go.light[i];
DFS(t + 1, go);
}
bool camp(LAMP x, LAMP y) {
for (int i = 1; i <= n; i++) {
if (x.light[i] != y.light[i]) return x.light[i] < y.light[i];
}
return true;
}
bool same(LAMP x, LAMP y) {
for (int i = 1; i <= n; i++) {
if (x.light[i] != y.light[i]) return false;
}
return true;
}
缩进粘贴后就没了,不要建议。 展开
1个回答
展开全部
应该不会出问题的呀?要不然可能看错了。我试了一下,没问题。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <algorithm>
using namespace std;
int n=10;
int a[22]={0,10,9,8,7,6,5,4,3,2,1};
int main()
{
printf("%d\n", n);
sort(a+1,a+n+1);
printf("%d\n", n);
return 0;
}
追问
这样写是没有问题,但是我的那一个程序不知道为什么就出问题了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询