Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-

Introduction

Hello. It's chewy and chewy. We will solve the introduction to AOJ's algorithms and data structures. It's easy to keep a record of what you've learned.

It's been less than half a year since I started programming myself AtCoder is green, so I'm not a strong man. Let's work hard together.

Ah, let's go

table of contents

This time is PART1: Introduction. I want to do my best and do it to the end.

ALDS1_1_A: Insertion sort ALDS1_1_B: Greatest common divisor ALDS1_1_C: Prime number ALDS1_1_D: Maximum profit

ALDS1_1_A: Insertion sort

Insertion sort

n = int(input())
A = list(map(int,input().split()))
print(*A)
for i in range(1,n):
    v = A[i]
    j = i-1
    while j >= 0 and A[j]>v:
        A[j+1] = A[j]
        j -= 1
        
    A[j+1] = v
    print(*A)

ALDS1_1_B: Greatest common divisor

The greatest common divisor is calculated by Euclid's mutual aid method.

 def gcd(a,b):
    while b:
        a, b = b, a%b
    return a



x,y = map(int,input().split())
print(gcd(x,y))

ALDS1_1_C: Prime number

It is O (n ** 0.5) to judge whether it is a prime number

n = int(input())
input_list = []

for _ in range(n):
    a = int(input())
    input_list.append(a)

def prime(n):
    if n==1:
        return False
    else:
        for i in range(2,int(n**0.5)+1):
            if n%i==0:
                return False 
        else:
            return True


ans = 0
for i in input_list:
    if prime(i):
        ans += 1

print(ans)

ALDS1_1_D: Maximum profit

Stock Forecast No Absolute

n = int(input())
a = []
for _ in range(n):
    b = int(input())
    a.append(b)

minv = a[0]
maxv = -10**18

for i in range(1,n):
    b = a[i]
    maxv = max(maxv,b-minv)
    minv = min(minv,b)
    
print(maxv)

in conclusion

If you have a wrong answer, please contact Goto

p.s.p I've never received a Qitta like guy We are looking forward to the first memorable relatives.

Recommended Posts

Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
[Introduction to cx_Oracle] (Part 9) DB and Python data type mapping (version 8 or later)
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Introduction to Python scikit-learn, matplotlib, single-layer algorithm (~ towards B3 ~ part3)
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Introduction to Python3 Day 1] Programming and Python
Sorting algorithm and implementation in Python
Introduction to Python Hands On Part 1
Hashing data in R and Python
An introduction to statistical modeling for data analysis (Midorimoto) reading notes (in Python and Stan)
processing to use notMNIST data in Python (and tried to classify it)
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
[Introduction to Python] How to use class in Python?
AM modulation and demodulation in Python Part 2
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
Ant book in python: Sec. 2-4, data structures
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Easily graph data in shell and Python
Compress python data and write to sqlite
How to use is and == in Python
"Introduction to data analysis by Bayesian statistical modeling starting with R and Stan" implemented in Python
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Effectiveness Verification Chapter 1 in Python
[Introduction to Data Scientists] Basics of Python ♬
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
How to generate permutations in Python and C ++
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Introduction to effectiveness verification Chapter 3 written in Python
Python variables and data types learned in chemoinformatics
Receive and display HTML form data in Python
tse --Introduction to Text Stream Editor in Python
[Python] Swapping rows and columns in Numpy data
[Python] How to read data from CIFAR-10 and CIFAR-100
I wrote "Introduction to Effect Verification" in Python
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
Send messages to Skype and Chatwork in Python
[Introduction to Python] How to handle JSON format data
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Algorithm] Find the shortest path [Python3]
Introduction to Effectiveness Verification Chapter 2 Written in Python
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
[Python] How to name table data and output it in csv (to_csv method)
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
Convert timezoned date and time to Unixtime in Python2.7
Summary of tools needed to analyze data in Python
Full-width and half-width processing of CSV data in Python
[Introduction to Reinforcement Learning] part.1-Epsilon-Greedy Algorithm in Bandit Game