| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为什么我的程序会消耗那么多内存!?有什么办法么?各位大侠帮帮忙吧……谢谢了!!^_^#include<iostream>
#include<cstring>
#include<stdlib.h>
using namespace std;
void invent(char *, char *);
int isInv(int);
char num[16][81];
void main()
{
int i, stat = 0;
cin >> num[0];
while( strcmp(num[0], "-1") )
{
for( i = 0; i < 15; i ++ )
{
invent(num[i], num[i+1]);
stat = isInv(i+1);
if( !stat )
break;
}
if(stat)
cout << num[0] << " can not be classified after 15 iterations" << endl;
for( i = 0; i < 16; i ++ )
strcpy( num[i], "\0" );
cin >> num[0];
}
}
void invent(char *a, char *b)
{
int i;
int temp[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int len = strlen(a);
int blen = strlen(b);
for( i = 0; i < len; i ++ )
temp[ a[ i ] - '0' ] ++;
for( i = 0; i < 10; i ++ )
{
if( !temp[ i ] )
continue;
char *t1 = new char[2];
char *t2 = new char[3];
sprintf(t1, "%d", i);
sprintf(t2, "%d", temp[ i ]);
strcat(t2, t1);
strcat(b, t2);
}
}
int isInv(int pos)
{
int i, j;
for( i = 1; i <= pos; i ++ )
{
if( strcmp(num[1], num[0]) == 0 )
{
cout << num[0] << " is self-inventorying" << endl;
return 0;
}
if( strcmp(num[i], num[i-1]) == 0 )
{
cout << num[0] << " is self-inventorying after " << i-1 << " steps" << endl;
return 0;
}
for( j = 0; j < i; j ++ )
{
if( strcmp(num[i], num[j]) == 0 )
{
cout << num[0] << " enters an inventory loop of length " << i - j << endl;
return 0;
}
}
}
return 1;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator