AOJ Introduction to Programming Topic # 7, Topic # 8

Topic # 7

ITP1_7_A

Grades

Python


def PointCheck(m, f, r):
    if m==-1 and f==-1 and r==-1:
        return 'End'
    elif m==-1 or f==-1:
        return 'F'
    
    if m==-1:
        m=0
    if f==-1:
        f=0

    if m+f>=80:
        return 'A'
    elif m+f>=65:
        return 'B'
    elif m+f>=50:
        return 'C'
    elif m+f>=30 and r>=50:
        return 'C'
    elif m+f>=30:  
        return 'D'
    else:
        return 'F'

while True:
    m, f, r = list(map(int, input().split()))

    ans = PointCheck(m, f, r)
        
    if ans == "End":
        break
    print(ans)

ITP1_7_B

Number of combinations

Python


while True:
    n, x = list(map(int, input().split()))
    
    if n==0 and x==0:
        break
    
    ans=0
    for i in range(1, n-1):
        for j in range(i+1, n):
            for k in range(j+1, n+1):
                if x == i+j+k:
                    ans+=1
    print(ans)

ITP1_7_C

Spreadsheet

Python


r, c = list(map(int, input().split()))

field = [[0 for i in range(c)] for j in range(r)]
ans = [[0 for i in range(c+1)] for j in range(r+1)]

for i in range(0, r):
    field[i] = list(map(int, input().split()))

    for j in range(0, c):
        ans[i][j] = field[i][j]
        ans[r][j] += field[i][j]

for i in range(0, r+1):
    for j in range(0, c+1):
        if j<c:
            print(f"{ans[i][j]} ", end='')
        elif j==c:
            print(f"{sum(ans[i])}")

ITP1_7_D

Matrix multiplication

Python


n, m, l = list(map(int, input().split()))

a = [[0 for i in range(m)] for j in range(n)]
b = [[0 for i in range(l)] for j in range(m)]
c = [[0 for i in range(l)] for j in range(n)]

for i in range(0, n):
    a[i] = list(map(int, input().split()))

for i in range(0, m):
    b[i] = list(map(int, input().split()))

for i in range(0, n):
    for j in range(0, l):
        ans = 0
        for k in range(0, m):
            ans += a[i][k] * b[k][j]
        c[i][j] = ans

for i in range(0, n):
    for j in range(0, l):
        if j < l-1:
            print(f"{c[i][j]} ", end='')
        elif j == l-1:
            print(f"{c[i][j]}")

Topic # 8

ITP1_8_A

Swapping uppercase and lowercase

C++


#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cctype>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll =long long;
using P = pair<int,int>;
 
int main(int argc, const char * argv[]) {
    
    string s;
    getline(cin , s);
    
    string ans="";
    rep(i, s.length()){
        if(isalpha(s[i])){
            if(islower(s[i])) ans += toupper(s[i]);
            else if(isupper(s[i])) ans += tolower(s[i]);
        }else{
            ans+=s[i];
        }
    }
    
    cout << ans << endl;

    return 0;
}

ITP1_8_B

Sum of numbers

C++


#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cctype>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll =long long;
using P = pair<int,int>;
 
int main(int argc, const char * argv[]) {
    
    while(true){
        string s;
        cin >> s;
        if(s=="0") break;
        
        int ans=0;
        rep(i, s.length()){
            ans += s[i] - '0';
        }
        cout << ans << endl;
    }

    return 0;
}

ITP1_8_C

Character count

C++


#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cctype>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll =long long;
using P = pair<int,int>;
 
int main(int argc, const char * argv[]) {
    
    int counter[26] = {0};
    
    char ch;
    while(cin >> ch){
        if(isalpha(ch)){
            ch = tolower(ch);
            counter[ch - 'a']++;
        }
    }
    
    rep(i, 26){
        cout << static_cast<char>('a'+i) << " : " << counter[i] << endl;
    }
    
    return 0;
}

ITP1_8_D

ring

C++


#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<utility>
#include<iomanip>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cctype>
 
#define rep(i,n) for(int i=0; i<(n); ++i)
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
 
using namespace std;
using ll =long long;
using P = pair<int,int>;
 
int main(int argc, const char * argv[]) {
    string s,p;
    cin >> s >> p;
    
    s = s + s;
    if(s.find(p) != string::npos) cout << "Yes" << endl;
    else cout << "No" << endl;
    
    return 0;
}

Recommended Posts

AOJ Introduction to Programming Topic # 1, Topic # 2, Topic # 3, Topic # 4
AOJ Introduction to Programming Topic # 7, Topic # 8
AOJ Introduction to Programming Topic # 5, Topic # 6
Introduction to AOJ Programming (ALDS1)-# 7 Tree Structure
An introduction to Python Programming
Introduction to MQTT (Introduction)
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Supervisor
Introduction to Tkinter 1: Introduction
Introduction to PyQt
Introduction to Scrapy (2)
[Linux] Introduction to Linux
Introduction to Scrapy (4)
Introduction to discord.py (2)
Introduction to discord.py
An introduction to object-oriented programming for beginners by beginners
Introduction to Programming (Python) TA Tendency for beginners
Introduction to Lightning pytorch
Introduction to Web Scraping
Introduction to Nonparametric Bayes
Introduction to EV3 / MicroPython
Introduction to Python language
Introduction to TensorFlow-Image Recognition
Introduction to OpenCV (python)-(2)
Introduction to PyQt4 Part 1
Introduction to Dependency Injection
Introduction to Private Chainer
Introduction to machine learning
Introduction to electronic paper modules
A quick introduction to pytest-mock
Introduction to dictionary lookup algorithm
[Learning memorandum] Introduction to vim
Introduction to PyTorch (1) Automatic differentiation
opencv-python Introduction to image processing
Introduction to Python Django (2) Win
Kubernetes Scheduler Introduction to Homebrew
An introduction to machine learning
[Introduction to cx_Oracle] Overview of cx_Oracle
[Introduction to pytorch-lightning] First Lit ♬
Introduction to Anomaly Detection 1 Basics
Introduction to RDB with sqlalchemy Ⅰ
[Introduction to Systre] Fibonacci Retracement ♬
Introduction to Nonlinear Optimization (I)
Introduction to serial communication [Python]
An introduction to functional programming to improve debugging efficiency in 1 minute
Programming problem collection (Q16 to Q20)
Introduction to Deep Learning ~ Learning Rules ~
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
[Introduction to cx_Oracle] (8th) cx_Oracle 8.0 release
Introduction to discord.py (3) Using voice
An introduction to Bayesian optimization
Deep Reinforcement Learning 1 Introduction to Reinforcement Learning
Super introduction to machine learning
Introduction to Ansible Part ③'Inventory'
Series: Introduction to cx_Oracle Contents
[Introduction] How to use open3d
Introduction to Python For, While
Introduction to Deep Learning ~ Backpropagation ~
Introduction to Ansible Part ④'Variable'