6-2 找出矩陣中最大值所在的位置
將1個3*2的矩陣存入1個3*2的二維陣列中,找出最大值以及它的行下標和列下標,並輸出該矩陣
#include<stdio.h>
int main(void)
{
int col, i, j, row;
int a[3][2];
//將輸入的數存入二維陣列
for(i = 0; i < 3; i++)
for(j = 0; j < 2; j++)
scanf("%d",&a[i][j]);
//按矩陣的形式輸出二維陣列a
for(i = 0; i < 3; i++) {
for(j = 0; j < 2; j++)
printf("%4d",a[i][j]);
}
//遍歷二維陣列,找出最大值a[row][col]
row = col = 0;//先假設a[0][0]是最大值
for(i = 0; i < 3; i++)
for(j = 0; j < 2; j++)
if(a[i][j] > a[row][col]){ //如果a[i][j]比假設值大,再假設a[i][j]是新的最大值
row = i;
col = j;
}
printf("max = a[%d][%d] = %d\n",row,col,a[row][col]);
return 0;
}
執行結果:
3 2
10 -9
6 -1
max = a[1][0] = 10
相關推薦
6-2 找出矩陣中最大值所在的位置
將1個3*2的矩陣存入1個3*2的二維陣列中,找出最大值以及它的行下標和列下標,並輸出該矩陣 #include<stdio.h> int main(void) {int col, i, j, row;int a[3][2];//將輸入的數存入二維陣列 for(i = 0
(java)leetcode852 山脈陣列的封頂索引(二分查詢法找出陣列中最大值的下標)(Peak Index in a Mountain Array)
題目描述: 我們把符合下列屬性的陣列 A 稱作山脈: A.length >= 3 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A
用 prompt 輸入字串,建立陣列,找出陣列中最大值,問題闡述與解決。
用 prompt 輸入字串建立陣列。用三種方法找出陣列中最大值。問題闡述與解決。 實現目標:輸入一組數,並找出這組數中最大的值。 採用的方法: 雙 for迴圈,列出從小到大(從大到小)順序。 單 for 迴圈,一遍迴圈找出最大值。 使用 Math.max。
Problem B: 零起點學演算法81——找出陣列中最大元素的位置(下標值
#include<stdio.h> int main(void) { int n,a[10],i,max; while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) scanf("%d",
python利用pandas找出矩陣的最大值或最小值及其對應的位置
import pandas as pd text = pd.DataFrame([[21,45,78],[23,56,89],[14,25,36],[47,58,69]]) # 先建立一個矩陣 print(text) Out[1]: 0 1 2 0 21 45 78 1
找出陣列中最大元素的位置(下標值)
Description 找出陣列中最大的元素的下標。 Input 多組測試,每組先輸入一個不大於10的整數n 然後是n個整數 Output 輸出這n個整數中最大的元素及下標值 Sample Input 4 1 4 5 6 Sample Output 6 3 #i
轉 Python3找出List中最大/最小的N個數及索引
# -*- coding: utf-8 -*- import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 24, 37, 2] # 最大的3個數的索引 max_num_index_list = map(nums.index, heapq.nlarges
python 找出list中最大或者最小几個數的索引
nums = [1,8,2,23,7,-4,18,23,24,37,2] result = map(nums.index, heapq.nlargest(3, nums)) temp=[] Inf =
轉 Python3找出List中最大/最小的N個數及索引
# -*- coding: utf-8 -*- import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 24, 37, 2] # 最大的3個數的索引 max_num_index_list = map(nums.index, hea
堆&&堆排序&&N個數中找出K個最大值&&優先順序佇列
學習二叉樹後,有一個東西需要我們來關注下,就是堆,對於堆,來說我們可以把堆看作一顆完全二叉樹。這裡我們也可以叫做二叉堆。 二叉堆滿足二個特性: 1.父結點的鍵值總是大於或等於(小於或等於)任何一個子節點的鍵值。 2.每個結點的左子樹和右子樹都是一個二叉堆(
同時找出陣列的最大值和最小值,要求時間複雜度為o(n)
#include <stdio.h> void max_min(int A[],int n,int& max,int& min) { int i; if(n%2==0) { if(
【十一】找出一組數組中最大值、最大值的角標、最小值、最小值的角標及平均數
思路 原來 比較 快捷 兩個 span div 選擇排序 分數 需求:現有一組評委打分的數據。 求出他們的最大值與最小值。 最大值最小值的角標 去除最大值與最小值後的平均數 代碼: 1 <?php 2 $arr=array(1,2,3,4); 3
找出矩陣中含有0最多的一行(find the longest row of zero)
對於一個n*n的矩陣,其中只包含有0,1兩種元素且,所有的0都在1之前,請找出矩陣中0最多的一行。(Given an N-by-N matrix of 0s and 1s such that in each row no 0 comes before a 1, find
問題6: 找出字串中出現次數最多的字母及其出現的次數
這好像是華為機試的一道題目,我採用JAVA來完成,用JAVA也可以用其他方法。 我用一個比較簡單的方法,程式碼如下: public class Str{ public static void main(String[] args){ String str =
linux中在當前目錄下找出佔用空間最大的前10大檔案
首先要了解三個常用命令: du : 計算出單個檔案或者資料夾的磁碟空間佔用.sort : 對檔案行或者標準輸出行記錄排序後輸出.head : 輸出檔案內容的前面部分. du: -a:顯示目錄佔用空間的大小,還要顯示其下目錄佔用空間的大小 sort: -n : 按照字串表
java實現找出所有的最大連通子圖,並把連通子圖中所有頂點的集合合併為一個i額字串集合。
***************************************************************************************************
C語言:從p所指字符串中找出ASCII碼最大的字符,將其放在第一個位置上,並將該字符前的原字符向後順序移動。
lose 使用數組 max code 數據 scanf 打開文件 stdio.h ted //fun函數:從p所指字符串中找出ASCII碼最大的字符,將其放在第一個位置上,並將該字符前的原字符向後順序移動。 1 #include <stdio.h>
LeetCode:121. Best Time to Buy and Sell Stock (找出股票的最大收益)
Say you have an array for which the ith element is the price of a given stock on day i. If
LeetCode:155. Min Stack(找出棧中最小的那個值)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop()
LeetCode:53. Maximum Subarray(找出陣列中和最大的陣列)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the l