欧拉工程第11题
来源:互联网 发布:剑灵但丁捏脸数据 编辑:程序博客网 时间:2024/05/18 03:06
题目链接:https://projecteuler.net/problem=11
package projecteuler11to20;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.StringTokenizer;class level11{ void solve() throws NumberFormatException, IOException { String fileName="src\\projecteuler11to20\\p11.csv"; int ncol=20; int nrow=20; int[][] numbers=new int[nrow][ncol]; numbers=CSVFileReader(fileName,nrow,ncol); int max=caculate(numbers,nrow,ncol); System.out.println(max); } int caculate(int[][] a,int nrow,int ncol){ int max=0; for(int i=0;i<nrow;i++){ for(int j=0;j<ncol;j++){ //横 +4 int mul=a[i][j]; for(int k=1;k<4 && (4+j)<ncol;k++){ mul*=a[i][j+k]; } //System.out.print(mul+" "+max+","); if(mul>max) max=mul; //竖 +4 mul=a[i][j]; for(int k=1;k<4 &&(4+i)<nrow;k++){ mul*=a[i+k][j]; } //System.out.print(mul+" "+max+","); if(mul>max) max=mul; //右下 +4 mul=a[i][j]; for(int k=1;k<4 &&(4+j)<ncol&&(4+i)<nrow;k++ ){ mul*=a[i+k][j+k]; } //System.out.print(mul+" "+max+","); if(mul>max) max=mul; //右上 +4 mul=a[i][j]; for(int k=1;k<4&&(i+4)<ncol&&(j-4)>=0;k++){ mul*=a[i+k][j-k]; } // System.out.print(mul+" "+max+","); if(mul>max) max=mul; //System.out.println(); } } return max; } int[][] CSVFileReader(String fileName,int nrow,int ncol) throws NumberFormatException, IOException { File file = new File(fileName); int [][] numbers = new int [nrow][ncol]; StringBuilder sb = new StringBuilder(); String line = null; int row = 0; int col = 0; BufferedReader br = null; br = new BufferedReader(new FileReader(file)); while( (line = br.readLine()) != null && row<nrow) { StringTokenizer st = new StringTokenizer(line,","); while (st.hasMoreTokens()) { //get next token and store it in the array numbers[row][col] = Integer.parseInt(st.nextToken());//字符转换成数字 col++; } col = 0; row++; } br.close(); return numbers; }}public class Problem11 { /** * @param args * @throws IOException * @throws NumberFormatException */ public static void main(String[] args) throws NumberFormatException, IOException { // TODO Auto-generated method stub new level11().solve(); }}
0 0
- 欧拉工程第11题
- 欧拉工程第12题
- 欧拉工程第13题
- 欧拉工程第14题
- 欧拉工程第15题
- 欧拉工程第16题
- 欧拉工程第17题
- 欧拉工程第18题
- 欧拉工程第19题
- 欧拉工程第20题
- 欧拉工程第21题:Amicable numbers
- 欧拉工程第22题:Names scores
- 欧拉工程第23题:Non-abundant sums
- 欧拉工程第24题:Lexicographic permutations
- 欧拉工程第25题:1000-digit Fibonacci number
- 欧拉工程第26题:Reciprocal cycles
- 欧拉工程第27题:Quadratic primes
- 欧拉工程第28题:Number spiral diagonals
- python numpy 总结
- Android PagerAdapter notifyDatasetChanged
- 对汇编中循环程序结构的认识
- sqlite数据库简介
- FFmpeg的Android平台移植—编译篇
- 欧拉工程第11题
- Sublime Text 3 支持的热门插件推荐
- VC++6.0调试篇:变量的观察--watch窗口的技巧
- 数组
- [LeetCode] Next Permutation
- Spring注解@Component、@Repository、@Service、@Controller区别
- SVM(四)为何需要核函数
- 黑马程序员——正则表达式
- Servlet的生命周期