Project Euler Homework

015.cpp

Go to the documentation of this file.
00001 
00007 #include <iostream>
00008 using namespace std;
00009 
00010 
00011 /*
00012 Can use recursion but very inefficient:
00013 unsigned route(int a, int b)
00014 {
00015 if(a==0) return 1;
00016 if(b==0) return 1;
00017 return route(a-1,b)+route(a,b-1);
00018 }
00019 cout << route(20,20);
00020 */
00021 int 
00022 main(){
00023 
00024  int grid = 20;
00025   unsigned long long matrix[grid+1][grid+1];
00026   matrix[0][0] = 1;
00027   for (int i = 1; i <= grid; i++)
00028     {
00029       matrix[0][i] = 1;
00030       matrix[i][0] = 1;
00031     }
00032 
00033   for (int i = 1; i <= grid; i++)
00034     for (int j = 1; j <= grid; j++)
00035       matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1];
00036 
00037   cout << matrix[grid][grid] << endl;
00038   return 0;
00039 }
00040 
 All Classes Files Functions Variables