这个程序该怎么样改进呢?求指点
给定表达式[x/2] + y + x * y, 其中x,y都是正整数。其中的中括号表示下取整,例如[3/2] = 1 , [5/2] = 2。
有些正整数可以用上述表达式表达出来,例如正整数2,当取x = y = 1时,可以把2表达出来
( 解释下:当x=y=1时, [x / 2] + y + x * y = [1 / 2] + 1 + 1 * 1 = 0+1+1 = 2 );
有些数可以有多种方式表达,例如13可以由 x = 2 y = 4 以及x = 3 y = 3来表示;
有些数无法用这个表达式表达出来,比如3。
从1开始第n个不能用这个表达式表示出来的数,我们叫做an,例如a1=1 a2=3,给定n,求an。
我的解答是:
#include <stdio.h>
#include <string.h>
#ifdef WIN32
typedef __int64 LL;
#else
typedef long long LL;
#endif
int givean(int n) {
int x=0,y=0;
int i=0,j=0;
int p=0,q=0;
LL answer;
LL count[10000000],a[60];
for(x=1;x<1000000007;x++)
for(y=1;y<1000000007;y++)
count[p++]=x%2+(x+1)*y;
for(i=1;i<10000000;i++)
{
while(1)
{
if(count[i]!=j)
{ a[q++]=j;
j++;
}
else
break;
}
}
answer=a[n];
}
win32
[解决办法]
你这方法也太狠了……
[解决办法]
坚决消灭零回复.
------解决方案--------------------
在占用内存空间较大的局部数组声明的前面加static将其从堆栈数据段挪到全局数据段即可避开因局部数组大小超过默认堆栈大小1MB造成程序不能正常运行的问题。
[解决办法]
LL count[10000000],a[60];
改为
static LL count[10000000],a[60];
再试试看。
[解决办法]
我就一句话。第40个数有600多万位。
[解决办法]
这个for真是n平方的复杂度啊,怎么得了
[解决办法]
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
bool IsAn(int an)
{
for(int x=2; x<an; x++)
{
for(int y=1; y<an; y++)
{
if((x/2 + (x+1)* y)==an)
return true;
}
}
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
for(int an=1; an!=100; an++)
{
if(IsAn(an))
continue;
else
cout<<an<<endl;
}
system("pause");
return 0;
}
1. 提供数字进行匹配,而不是将遍历后的结果保存,可以用容器保存。你直接保存长度不灵活
2. 一有匹配直接跳过,而不是继续比对