Three, PTA experiment Work (Array)

Source: Internet
Author: User

7-2 Maximum value and subscript (20 points)

The main requirement is to write a program that finds the maximum value in the given n number and its corresponding minimum subscript (the subscript starts at 0).

Input format:

The input gives a positive integer N (1<n≤10) in the first row. The second line enters n integers, separated by a space.

Output format:

The minimum subscript for the maximum and maximum values is output in a row, separated by a space in the middle.

1. PTA Submission List

2. Design ideas (including flowchart), mainly describe the problem algorithm

The order of the arrays and the subsequent numbers are compared over and over, until the maximum number is found.

The final code is as follows

#include <stdio.h>intMain () {intn,i,j; scanf ("%d",&N); inta[ -];  for(i=0; i<n;i++) {scanf ("%d",&A[i]); }     for(j=0; j<n;j++){      if(a[0]<A[j]) {a[0]=A[j]; I=J; }      if(a[0]==A[j]) {a[0]=A[j]; if(i>j) {i=J; }}} printf ("%d%d", A[i],i); return 0;}
7-4 finding integers (10 points)

The subject asks for a given x from the input n integers. If found, the position of the output x (starting from 0), if not found, outputs "not Found".

Input format:

The input gives two positive integers n (≤20) and X in the first row, and the second line gives n integers. Numbers do not exceed long integers, separated by spaces.

Output format:

Prints the position of x in a row, or "not Found".

Input Sample 1:
5 73 5 7 1 9
Output Example 1:
2
Input Sample 2:
5 73 5 8 1 9
Output Example 2:
Not Found

1. PTA Submission List

2. Design ideas (including flowchart), mainly describe the problem algorithm

set an array, and then use a loop to put the number of n into the array, Then use if to find the number if the range beyond the array is not found and output not FOUND

The final code is as follows

#include <stdio.h>#defineN 20intMainvoid) {      LongA[n]; intI, n, x; scanf ("%d%d", &n, &x);  for(i =0; I < n; ++i) scanf ("%ld", &A[i]);  for(i =0; I < n; ++i) {if(A[i] = =x) {printf ("%d\n", i);  Break; }      }      if(I >=N) printf ("Not found\n"); return 0; }  

7-3 Place the number in the array in reverse order (20 points)

This requires writing a program, storing the given n integers in an array, holding the number of n in an array in reverse order, and then outputting the elements in the array sequentially.

Input format:

The input gives a positive integer N (1≤n≤10) in the first row. The second line enters n integers, separated by a space.

Output format:

Outputs the result of the processing of these n integers in a row, separated by a space in the middle of the adjacent number, with no extra space at the end of the line.

Input Sample:
410 8 1 2
Sample output:
2 1 8 10

1. PTA Submission List

2. Design ideas (including flowchart), mainly describe the problem algorithm

The final code is as follows

#include <stdio.h>intMain () {intI, n, swap, array[Ten]; scanf ("%d", &N);  for(i =0; I < n; i++) scanf ("%d", &Array[i]);  for(i =0; I < n/2; i++) {Swap=Array[i]; Array[i]= array[n-1-i]; Array[n-1-I.] =swap; }     for(i =0; I < n1; i++) printf ("%d", Array[i]); printf ("%d\n", array[n-1]); return 0;}

Final PTA Rankings

Three, PTA experiment Work (Array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.