Monday, 4 May 2009

POJ 1220


以前屡试不爽的大数类在这里扛不住超内存了,于是发现了一个更有效的方式——简单说就是一边乘一边除。
其实下面的代码还很有改进空间,不过思想也就只这样的了。
#include <iostream>
#include <algorithm>
using namespace std;
inline char to_char(int ch) {
static const char table[]=
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
return table[ch];
}
inline int from_char(char ch) {
static const int table[]= {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 63, 255, 62, 255, 255,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, 255, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 255, 255, 255, 255, 255, 255, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255,
255
};
return table[int(ch)];
}

int convert(char* dest, char* src, int from, int to) {
int mem[2048]={0},*tmp,*buff,len,i,j,carry;
tmp=mem; buff=mem+1024;
for(i=0;src[i];++i)
tmp[i]=from_char(src[i]);
for(len=i,j=0;len;++j){
carry=0;
for(i=0;i<len;++i){
carry*=from;
carry+=tmp[i];
buff[i]=carry/to;
carry%=to;
}
dest[j]=to_char(carry);
for(i=0;i<len&&buff[i]==0;++i,--len);
buff+=i;
swap(tmp,buff);
}
dest[j]=0;
return j;
}

int main(){
int n,f,t,len;
char str[1024],dest[1024];
for(cin>>n;n--;){
cin>>f>>t>>str;
cout<<f<<" "<<str<<endl<<t<<" ";
len=convert(dest,str,f,t);
for(--len;len>=0;--len) cout<<dest[len];
cout<<"\n\n";
}
return 0;
}

POJ 3332


这题完全应该用 java + regex 做的。
#include <iostream>
#include <cctype>
using namespace std;
bool ok(char* x){
if(*x=='+'||*x=='-')
if (!isdigit(*++x)) return false;
if(!isdigit(*x)) return false;
for(;isdigit(*x);++x);
if (*x==0) return true;
else if (*x=='.') {
if (!isdigit(*++x)) return false;
for(;isdigit(*x);++x);
}
if (*x=='e'||*x=='E') {
++x;
if (*x=='+'||*x=='-') ++x;
if(!isdigit(*x)) return false;
for(;isdigit(*x);++x);
}
return *x==0;
}
int main() {
int n; char x[1024];
for(cin>>n;n--;cout<<(ok(x)?"LEGAL":"ILLEGAL")<<endl)cin>>x;
return 0;
}

POJ 1565


#include <iostream>
#include <string.h>
using namespace std;
typedef unsigned int uint;
uint de(char* x){
uint n=1,r=0;
char *p;
for(p=x;*p;++p);
for(--p;p>=x;--p)r+=((n<<=1)-1)*(*p-'0');
return r;
}
int main(){
for(char x[1024];cin>>x;cout<<de(x)<<endl)
if(!strcmp(x,"0"))break;
return 0;
}

POJ 2602


#include <stdio.h>
int main() {
char a[1000001],b[1000000],c=0;
int N,i;
for(scanf("%d\n",&N),i=0;i<N;++i)
a[i]=getchar(),getchar(),b[i]=getchar(),getchar();
for(--i;i>=0;--i){
a[i]+=b[i]-'0'+c;
if(c=a[i]>'9')
a[i]-=10;
}
for(i=0;i<N;++i)
putchar(a[i]);
return 0;
}

POJ 1658


#include <iostream>
using namespace std;
int next(int* arr){
bool p=true,q=true;
int P=arr[1]-arr[0],Q=arr[1]/arr[0],i;
for(i=2;i<4;++i) {
if(arr[i]-arr[i-1]!=P)p=false;
if(arr[i]/arr[i-1]!=Q)q=false;
}
return p?arr[3]+P:arr[3]*Q;
}

int main() {
int N,arr[4],i;
for(cin>>N;N--;){
for(i=0;i<4;++i){
cin>>arr[i];
cout<<arr[i]<<" ";
}
cout<<next(arr)<<endl;
}
return 0;
}

POJ 2406


#if 0
#include <stdio.h>
#define XSIZE 1000001
char str[XSIZE];
int next[XSIZE],len;
int kmpNext(char*x,int*next){
int i,j;
for(i=0,j=next[0]=-1;x[i];){
for(;j>-1&&x[i]!=x[j];j=next[j]);
++i;++j;
next[i]=x[i]==x[j]?next[j]:j;
}
return i;
}
int main() {
for(;scanf("%str",str),*str!='.';printf("%d\n",len%(next[0]=len-next[len])?1:len/next[0]))
len=kmpNext(str,next);
return 0;
}
#else
char s[1000001];n[1000001],i,j;main(){for(;gets(s),*s-'.';printf("%d\n",i%(j=i-n[i])?1:i/j))for(i=0,j=*n=-1;s[i];++i,++j,n[i]=s[i]==s[j]?n[j]:j)for(;j>-1&&s[i]!=s[j];j=n[j]);}
#endif

POJ 1517


#include <iostream>
int main(){
std::cout<<
"n e\n"
"- -----------\n"
"0 1\n"
"1 2\n"
"2 2.5\n"
"3 2.666666667\n"
"4 2.708333333\n"
"5 2.716666667\n"
"6 2.718055556\n"
"7 2.718253968\n"
"8 2.71827877\n"
"9 2.718281526";
return 0;
}

POJ 2656


n,m,d,a,b,i;main(){for(;scanf("%d",&n),n;){for(m=i=0;n-i;a+b>m&&(m=a+b,d=i))scanf("%d%d",&a,&b),++i;m<=8?puts("0"):printf("%d\n",d);}}

POJ 1731


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main() {
char str[256];
int len;
cin>>str;
len=strlen(str);
sort(str,str+len);
do cout<<str<<endl; while (next_permutation(str,str+len));
return 0;
}

POJ 1218


#include <iostream>
#include <cmath>
using namespace std;
int main() {
int N,n;
for(cin>>N;N--;cout<<int(sqrt(double(n)))<<endl)cin>>n;
return 0;
}