UVa 324

From Algorithmist
Jump to navigation Jump to search
  1. include <iostream>
  2. include <fstream>
  3. include <math.h>
  4. include <stdlib.h>
  5. include <stdio.h>
  6. include <string.h>

using namespace std;

int socantinh,dodaitam,hang; int boso[15]; int k,tam1[800],tam2[800],kq[1600]; int giaithua[800][1600];

void resetso(int a[]) { int j; for(j=0;j<11;j++) a[j]=0; }

void reset(int a[]) { int j; for(j=0;j<1600;j++) a[j]=0; }

void daochuoi(char n[]) { int j,tg,dodai; dodai=strlen(n); for(j=0;j<=dodai/2;j++) { tg=n[j]; n[j]=n[dodai-1-j]; n[dodai-1-j]=tg; } }

void nhan(int a[],int b[],int ketqua[],int dai1,int dai2) { int i,j; dodaitam=dai2; for(i=0;i<dai1;i++) { for(j=0;j<dai2;j++) { ketqua[i+j]=ketqua[i+j]+a[i]*b[j]; ketqua[i+j+1]=ketqua[i+j+1]+ketqua[i+j]/10; ketqua[i+j]=ketqua[i+j]%10; } dodaitam++; } }

void dao(int x,int a[],int dai) { int i; for(i=0;i<dai;i++) { a[i]=x%10; x=x/10; } }

int dodaichuoi(int b[]) { int i; for(i=800;i>=0;i--) if(b[i]!=0) return i+1; }

void gan1(int a[1600],int b[],int dai1) { int i; for(i=0;i<dai1;i++) tam2[i]=kq[i]; }

void gan2(int b[],int a[][1600],int hang2,int dai1) { int i; for(i=0;i<dai1;i++) giaithua[hang2-1][i]=kq[i]; }

void tinhgt() { int so,dd1,dd2; giaithua[0][0]=1; giaithua[1][0]=2; dd2=0; tam2[0]=2; for(so=3;so<=366;so++) { if(so<10 && so>0) dd1=1;

if(so>9 && so<100) dd1=2;

if(so>99 && so<1000) dd1=3;

dao(so,tam1,dd1); dd2=dodaichuoi(tam2); nhan(tam1,tam2,kq,dd1,dd2); gan1(kq,tam2,dodaitam); gan2(kq,giaithua,so,dodaitam); reset(kq); } }

void dem(int so0) { int i,dai;

for(i=1600;i>=0;i--) { if(giaithua[so0-1][i]!=0) { dai=i+1; break; } }

for(i=0;i<dai;i++) boso[giaithua[so0-1][i]]++; }

void output() { printf("%d! --\n",socantinh); printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d\n",boso[0],boso[1],boso[2],boso[3],boso[4]); printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d\n",boso[5],boso[6],boso[7],boso[8],boso[9]); }

void out2() { int x=1; int y=0; printf("%d! --\n",socantinh); printf(" (0)%5d (1)%5d (2)%5d (3)%5d (4)%5d\n",y,x,y,y,y); printf(" (5)%5d (6)%5d (7)%5d (8)%5d (9)%5d\n",y,y,y,y,y); }

int main() { //freopen("324.inp","r",stdin); //freopen("324.out","w",stdout); tinhgt(); while(scanf("%d",&socantinh)>0 && socantinh!=0) { if(socantinh>0) { if(socantinh<=3) { if(socantinh==1) boso[1]++; if(socantinh==2) boso[2]++; if(socantinh==3) boso[6]++; } else dem(socantinh); output(); resetso(boso); } else { out2(); } } return 0; }