Thursday, July 29, 2010

ACM-160 Factors and Factorials with C++

#include<iostream>
#include<stdio.h>
#include<cstdio>

using namespace std;

void funct(int a , int b);

int array[25]={ 2, 3, 5, 7,11,13,
  17,19,23,29,31,37,
  41,43,47,53,59,61,
  67,71,73,79,83,89,97};
  
  
int buffer;
int step;

int main(int argc, char* argv[])
{
 int a;
 int b;
 
 while(cin != NULL)
 {
  cin>>a;
  //store the input
  if(a != 0)
  {
   printf("%3d! =",a);
   b = 0;
   step = 0;
   buffer = 0;
   while(a >= array[b])
   {
    funct(a,b);
    b++;
    if(b == 25)
    break;
   }
  }else
  {
   break;
  }
  printf("\n");
 }
 return 0;
}


void funct(int a , int b)
{
 if(a >= array[b])
 {
  buffer += a/array[b];
  a = a/array[b];
  funct(a,b);
 }else
 {
  step++;
  if(step == 16)
  {
   printf("\n      ");
   step = 1;
  }
  printf("%3d",buffer);
  buffer = 0;
 }
}

No comments: