⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timingarray.java

📁 国外的数据结构与算法分析用书
💻 JAVA
字号:
import java.io.*;

/**	A collection of functions and procedures for timing. */
public class TimingArray
{
	/**	A two-dimensional array (matrix). */
	public int[][] a;
	
	public static void main(String[] args) throws Exception
	{
		TimingArray c = new TimingArray();
	}

	/**	Main program. */
	public TimingArray() throws Exception
	{
		createArray();
		System.out.print("\nThe array is " + aString()
				+ "\nItem (1,1) is " + aItem(1, 1)
				+ "\nThe special sum is " + specialSum(a[0].length / 2)
				+ "\nThe different sum is " + differentSum()
				+ "\nThe big sum is " + bigSum() + "\n\n");
	}

	public int aItem(int i, int j)
	{
		return a[i][j];
	}

	public String aString()
	{
		String result = new String();
		for (int i = 0; i < a.length; i++)
			result += rowString(i);
		return result;
	}

	public String rowString(int i)
	{
		String result = "\nRow " + (i+1) + ": ";
		for (int j = 0; j < a[0].length; j++)
			result += a[i][j] + " ";
		return result;
	}

	public int bigSum()
	{
		int result = 0;
		int i, j, k;
		for (i = 0; i < a.length; i++)
			for (j = 0; j < a[0].length; j++)
				for (k = i; k < a.length; k++)
					result += a[k][j];
		return result;
	}

	public void createArray() throws Exception
	{
		int i, j, height, width;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Enter the height of the matrix: ");
		height = Integer.parseInt(br.readLine());
		System.out.print("Enter the width of the matrix: ");
		width = Integer.parseInt(br.readLine());

		a = new int[height][width];
	
		for (i = 0; i < a.length; i++)
			for (j = 0; j < a[0].length; j++)
				a[i][j] = i + 10 * j;
	}

	public int specialSum(int j)
	{
		int result = 0;
		for (int i = 1; i < a.length; i = 2*i)
			result += a[i][j];
		return result;
	}

	public int differentSum()
	{
		int i, j;
		int result = 0;
		for (i = 0, j = 0; i < a.length && j < a[0].length; i++, j++)
			result += a[i][0] * a[0][j];

		for (   ; i < a.length; i++)
			result += a[i][0] * a[i][0];

		for (   ; j < a[0].length; j++)
			result += a[0][j] * a[0][j];			

		return result;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -