2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AOJ: Introduction To Programming I の 40 問を C++ で解いた

Posted at

注意点

コンパイラは C++17 に設定して実施
done is better than perfect の精神で挑戦。
AC コードだが、非効率なところもあるかも。
気になれば後日修正(するかもw)

コメント、アドバイス、大歓迎!!

[第1問 Hello World]

ITP1_1_A.cpp
#include <iostream>
using namespace std;

int main(){
    cout << "Hello World\n";
    return 0;
}

[第2問 X Cubic]

ITP1_1_B.cpp
#include <iostream>
using namespace std;

int main(){
    int N;cin>>N;
    cout << N*N*N << "\n";
    return 0;
}

[第3問 Rectangle]

ITP1_1_C.cpp
#include <iostream>
using namespace std;

int main(){
    int a,b;cin>>a>>b;
    cout << a*b <<" " <<2*a+2*b<<"\n";
    return 0;
}

[第4問 Watch]

ITP1_1_D.cpp
#include <iostream>
using namespace std;

int main(){
    int s;cin>>s;
    int H = (s/3600);
    int M = (s%3600)/60;
    int S = (s%3600)%60;
    cout << H << ":" << M << ":" << S<<"\n";
    return 0;
}

[第5問 Small,Learge,or Equal]

ITP1_2_A.cpp
#include <iostream>
using namespace std;

int main(){
    int a,b;cin>>a>>b;
    if(a==b) cout << "a == b\n";
    else if(a>b) cout << "a > b\n";
    else if(a<b) cout << "a < b\n";
    return 0;
}

[第6問 Range]

ITP1_2_B.cpp
#include <iostream>
using namespace std;

int main() {
    int a, b, c; cin >> a >> b >> c;
    if (a < b  && b < c) cout << "Yes" << "\n";
    else cout << "No" << "\n";
    return 0;
}

[第7問 Sorting Three Numbers]

ITP1_2_C.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    vector<int>A(3,0);
    cin>>A[0]>>A[1]>>A[2];
    sort(A.begin(),A.end());
    cout << A[0] <<" "<< A[1] <<" "<< A[2]<<"\n";
    return 0;
}

[第8問 Circle in a Rectangle]

ITP1_2_D.cpp
#include <iostream>
using namespace std;

int main(){
    int W,H,x,y,r;cin>>W>>H>>x>>y>>r;
    if(x>=r && y >= r && r+x <= W && r+y <= H) cout << "Yes\n";
    else cout << "No\n";
    return 0;
}

[第9問 Print Many Hello World]

ITP1_3_A.cpp
#include <iostream>
using namespace std;

int main(){
    for(int i=0;i<1000;i++){
        cout << "Hello World\n";
    }
    return 0;
}

[第10問 Print Test Cases]

ITP1_3_B.cpp
#include <iostream>
#include <vector>
using namespace std;

int main(){
    vector<int>A(10000,0);
    for(int i=0;i<10000;i++){
        cin>>A[i];
        if(A[i]==0)break;
    }
    for(int i=0;i<10000;i++){
        if(A[i] != 0){
            cout << "Case "<<i+1<<": "<<A[i]<<"\n"; 
        }
        else break;
    }
    return 0;
}

[第11問 Swapping Two Numbers]

ITP1_3_C.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int a, b;
    vector<pair<int, int>>num;
    while (true) {
        cin >> a >> b;
        if (a == 0 && b == 0) break;
        else num.push_back(pair(min(a, b),max(a,b)));
    }
    for (auto x : num) {
        cout << x.first << " " << x.second << "\n";
    }
    //while (1) {}
    return 0;
}

[第12問 Swapping Two Numbers]

ITP1_3_D.cpp
#include <iostream>
#include <vector>
using namespace std;

int main(){
    int a,b,c;cin>>a>>b>>c;
    
    int cnt=0;
    for(int i=1;i<=c;i++){
        if(c%i == 0 && a<=i && i<=b) cnt++;
    }
    cout << cnt << "\n";
    return 0;
}

[第13問 A/B Problem]

ITP1_4_A.cpp
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int a, b; cin >> a >> b;
    cout << a / b << " " << a % b << " ";
    cout << fixed << setprecision(5) << double(a)/double(b)<<"\n";
    //while (1) {}
    return 0;
}

*参考 URL for setprecision =>

[第14問 Circle]

ITP1_4_B.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#define PI 3.14159265359

int main() {
    double r; cin >> r;
    cout << fixed << setprecision(5) << PI*r*r<<" ";
    cout << fixed << setprecision(5) << PI*2*r<<"\n";
    //while (1) {}
    return 0;
}

[第15問 Simple Calculator]

ITP1_4_C.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int a, b; char op;
	vector<int>buf;
	while(true){
		cin >> a >> op >> b;
		if (op == '?') break;
		else if (op == '+') buf.push_back(a + b);
		else if (op == '-') buf.push_back(a - b);
		else if (op == '*') buf.push_back(a * b);
		else if (op == '/') buf.push_back(a / b);
	}
	for (auto x : buf)
		cout << x << "\n";
	return 0;
}

[第16問 Min,Max and Sum]

ITP1_4_D.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int N; cin >> N;
	vector<int>A(N, 0);

	int MIN = 1000000, MAX = -1000000;long long SUM = 0;
	for (int i = 0; i < N; i++){
		cin >> A[i];
		SUM += A[i]; 
		MIN = min(MIN, A[i]);
		MAX = max(MAX, A[i]);
	}
	cout << MIN << " " << MAX << " " << SUM << "\n";
	//while (1) {}
	return 0;
}

[第17問 Print a Rectangle]

入力を受けきってから出力しないと WA となるようです。

ITP1_5_A.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int H, W;
	vector<pair<int, int>>num;
    //---> input
	while (true) {
		cin >> H >> W;
		if (H == 0 && W == 0) 
			break;
		num.push_back(pair(H, W));
	}

    //----> output
	for (auto n : num) {
		vector<string>S; string s;
		for (int h = 0; h < n.first; h++) {
			for (int w = 0; w < n.second; w++) {
				s += "#";
			}
			S.push_back(s);
			s = "";
		}
        //---> print 
		for (auto z : S)
			cout << z << "\n";
		cout << "\n";
	}

	//while (1) {}
	return 0;
}

[第18問 Print a Frame]

問17 の解をカスタム

ITP1_5_B.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int H, W;
	vector<pair<int, int>>num;
	while (true) {
		cin >> H >> W;
		if (H == 0 && W == 0) 
			break;
		num.push_back(pair(H, W));
	}

	for (auto n : num) {
		vector<string>S; string s;
		for (int h = 0; h < n.first; h++) {
			for (int w = 0; w < n.second; w++) {
				if ((h == 0 || h == n.first - 1))       //←custom point
					s += "#";                           //←
				else if ((w == 0 || w == n.second - 1)) //←
					s += "#";                           //←
				else                                    //←
					s += ".";                           //←
			}
			S.push_back(s);
			s = "";
		}
		for (auto z : S)
			cout << z << "\n";
		cout << "\n";
	}

	while (1) {}
	return 0;
}

[第19問 Print a Chessboard]

問17をカスタム

ITP1_5_C.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int H, W;
	vector<pair<int, int>>num;
	while (true) {
		cin >> H >> W;
		if (H == 0 && W == 0) 
			break;
		num.push_back(pair(H, W));
	}

	for (auto n : num) {
		vector<string>S; string s;
		for (int h = 0; h < n.first; h++) {
			for (int w = 0; w < n.second; w++) {
				if (h % 2 == 0) {               //<=custom point
					if (w % 2 == 0)             //<=
						s += "#";               //<=
					else                        //<=
						s += ".";               //<=
				}                               //<=
				else {                          //<=
					if (w % 2 == 0)             //<=
						s += ".";               //<=
					else                        //<=
						s += "#";               //<=
				}                               //<=
			}
			S.push_back(s);
			s = "";
		}
		for (auto z : S)
			cout << z << "\n";
		cout << "\n";
	}

	while (1) {}
	return 0;
}

[第20問 Structured Programming]

goto 文の理解は cout でコメントを置きながら読むと、つかみ易い。

ITP1_5_D.cpp
#include <iostream>
using namespace std;

int main() {
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        int N = i;
        if (N % 3 == 0)
            cout << " " << i;
        else {
            while (N > 0) {
                if (N % 10 == 3){
                    cout << " " << i;
                    break;
                }
                N /= 10;
            }
        }
    }
    //while (1) {}
    return 0;
}

[第21問 Reversing Numbers]

sort(**begin(),**end(),greater()) が通用しない

ITP1_6_A.cpp
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    int N; cin >> N;
    vector<int>A(N, 0);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    reverse(A.begin(), A.end());
    //sort(A.begin(), A.end(),greater<int>());
    for (int i = 0; i < N; i++) {
        if (i != N - 1) {
            cout << A[i] << " ";
        }
        else {
            cout << A[i]<<"\n";
        }
    }
    //while (1) {}
    return 0;
}

[第22問 Finding Missing Cards]

ITP1_6_B.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int N; cin >> N;
	vector<vector<int>>cards(4, vector<int>(14, 0));
	char K; int num;

	for (int i = 0; i < N; i++) {
		cin >> K >> num;
		if (K == 'S')
			cards[0][num] = 1;
		else if(K == 'H')
			cards[1][num] = 1;
		else if (K == 'C')
			cards[2][num] = 1;
		else if (K == 'D')
			cards[3][num] = 1;
	}


	//cout << "\n";

	/*
	for (int i = 1; i < 14; i++)
		cout << cards[0][i] << " ";
	cout << "\n";
	*/

	
	for (int i = 0; i < 4; i++) {
		for (int l = 1; l <= 13; l++) {
			if (cards[i][l] == 0) {
				if (i == 0)
					cout << "S " << l << "\n";
				else if (i == 1)
					cout << "H " << l << "\n";
				else if (i == 2)
					cout << "C " << l << "\n";
				else if (i == 3)
					cout << "D " << l << "\n";
			}
				
		}
	}
	
	//while (1) {}
	return 0;

}

[第23問 Official House]

ITP1_6_C.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int N; cin >> N;
	vector<vector<int>>map(12, vector<int>(10, 0));
	int b, f, r, v;
	for (int i = 0; i < N; i++) {
		cin >> b >> f >> r >> v;
		b = (b - 1) * 3;
		map[b + f-1][r - 1] += v;
	}

	//cout << "\n";

	for (int h = 0; h < 12; h++) {
		for (int w = 0; w < 10; w++) {
			cout << " " << map[h][w];
		}
		cout << "\n";
		if (h == 2 || h == 5 || h == 8)
			cout << "####################\n";
	}

	//while (1) {}
	return 0;

}

[第24問 Matrix Vector Multiplication]

ITP1_6_D.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n, m; cin >> n >> m;

	vector<vector<int>>A(n,vector<int>(m,0));
	for (int h = 0; h < n; h++) {
		for (int w = 0; w < m; w++) {
			cin >> A[h][w];
		}
	}
	vector<int>B(m, 0);
	for (int i = 0; i < m; i++)
		cin >> B[i];

	for (int h = 0; h < n; h++) {
		int ans = 0;
		for (int w = 0; w < m; w++) {
			ans += A[h][w]*B[w];
		}
		cout << ans << "\n";
	}
	//while (1) {}
	return 0;
}

[第25問 Grading]

ITP1_7_A.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int m, f, r;
	vector<string>rank;
	while (true) {
		cin >> m >> f >> r;
		if (m == -1 && f == -1 && r == -1) break;
		if (m == -1 || f == -1) rank.push_back("F");
		else if (m + f >= 80) rank.push_back("A");
		else if (65 <= m + f && m + f < 80) rank.push_back("B");
		else if (50 <= m + f && m + f < 65) rank.push_back("C");
		else if (30 <= m + f && m + f < 50) {
			if (r >= 50) rank.push_back("C");
			else rank.push_back("D");}
		else if (m + f < 30) rank.push_back("F");

	}
	for (auto r : rank)
		cout << r << "\n";

	//while (1) {}
	return 0;
}

[第26問 How many ways?]

ITP1_7_B.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n,x,ans;
	vector<int>N;
	while (true) {
		cin >> n>>x;

		if (n == 0 && x == 0) break;
		ans = 0;
		for (int i = 1; i <= n - 2; i++) {
			for (int j = i + 1; j <= n - 1; j++) {
				for (int k = j + 1; k <= n; k++) {
					if (i + j + k == x) {
						//cout << i << " " << j << " " << k << "\n";
						ans++; }
				}
			}
		}
		N.push_back(ans);
	}
	for (auto n : N)
		cout << n << "\n";

	//while (1) {}
	return 0;
}

[第27問 Spreadsheet]

ITP1_7_C.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int H, W; cin >> H >> W;
	vector<vector<int>>S(H + 1, vector<int>(W + 1, 0));

	for (int h = 0; h < H; h++) {
		for (int w = 0; w < W; w++) {
			cin >> S[h][w];
		}
	}

	for (int h = 0; h < H ; h++) {
		int Wsum = 0;
		for (int w = 0; w < W; w++)
			Wsum += S[h][w];
		S[h][W] = Wsum;
		Wsum = 0;
	}

	for (int w = 0; w <= W; w++) {
		int Hsum = 0;
		for (int h = 0; h < H; h++) 
			Hsum += S[h][w];
		S[H][w] = Hsum;
		Hsum = 0;
	}
	
	for (int h = 0; h <= H; h++) {
		for (int w = 0; w <= W; w++) {
			if(w==W)
				cout << S[h][w] << "\n";
			else
				cout << S[h][w] << " ";
		}
	}

	//while (1) {}
	return 0;
}

[第28問 Matrix Multiplication]

ITP1_7_D.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n, m, l; cin >> n >> m >> l;
	vector<vector<int>>A(n, vector<int>(m, 0));
	vector<vector<int>>B(m, vector<int>(l, 0));
	vector<vector<long long>>C(n, vector<long long>(l, 0));

	for (int h= 0; h < n; h++) {
		for (int w = 0; w < m; w++)
			cin >> A[h][w];
	}

	for (int h = 0; h < m; h++) {
		for (int w = 0; w < l; w++)
			cin >> B[h][w];
	}

		for(int bw = 0;bw<l;bw++){
			for(int ah=0;ah<n;ah++){
				for (int bh = 0; bh < m; bh++) {
					C[ah][bw] += B[bh][bw] * A[ah][bh];
				}
			}
		}
	
	for (int h = 0; h < n; h++) {
		for (int w = 0; w < l; w++) {
			if(w != l-1) cout << C[h][w] << " ";
			else cout << C[h][w] << "\n";
		}
	}

	//while (1) {}

	return 0;

}

[第29問 Toggling Cases]

ITP1_8_A.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
	
	string s;
	getline(cin, s);

	for (auto x : s) {
		if (islower(x)) 
			x = toupper(x); 
		else if (isupper(x)) 
			x = tolower(x);
		cout << x;
	}cout << "\n";

	//while (1) {}
	return 0;
}

[第30問 Sum of Numbers]

ITP1_8_B.cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
	string n; int sum = 0;
	vector<int>num;

	while(true){
		cin >> n;
		if (n !="0") {
			for (int i = 0; i < n.size(); i++) {
				sum += int(n[i] - '0');
			}
			num.push_back(sum);
			sum = 0;
		}
		else
			break;
	}

	for (auto x : num)
		cout << x << "\n";

	//while (1) {}
	return 0;
}

[第31問 Counting Characters]

VScode で事前確認してからブラウザに入力している。
VScode の場合は コードを F5 で実行 => enter=> ctrl+z(EOF) で
初めて動作確認できる。

ITP1_8_C.cpp
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

int main() {
    
    string S,a;
    while (cin>>S) {
        a+=S;
    }
    transform(a.begin(), a.end(),a.begin(), ::tolower);
    map<char,int>n;
    for (char c = 'a'; c <= 'z'; c++) n[c] = 0;
    for (int i = 0; i < a.size(); i++) {
        if(n.count(a[i])!=0) n[a[i]]++;
    }
    for (auto x:n)
        cout << x.first << " : " << x.second <<"\n";

    //while (1) {}
    return 0;
}

[第32問 Ring]

ITP1_8_D.cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
	string S, P; cin >> S >> P;
	
	bool flag = false;
	for (int i = 0; i < S.size() - P.size()+1; i++) {
		//cout << S.substr(i, P.size()) << "\n";
		if (S.substr(i, P.size()) == P)
			flag = true;
	}
	//cout << "\n";
	string M;
	for (int i = S.size() - P.size() + 1; i < S.size(); i++) {
		M = S.substr(i, S.size() - i) + S.substr(0, P.size() - (S.size() - i));
		//cout << M << "\n";
		if (M == P)
			flag = true;
	}
	if (flag) cout << "Yes\n";
	else cout << "No\n";

	//while (1) {}
	return 0;
}

[第33問 Finding a Word]

ITP1_9_A.cpp
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;

int main() {
	string W,S; cin >> W;
	map<string, int>nums;

	while (true) {

		cin >> S;
		if (S != "END_OF_TEXT") {
			transform(S.begin(), S.end(), S.begin(), ::tolower);
			nums[S]++;
		}
		else
			break;

	}

	cout << nums[W] <<"\n";

	//while (1) {}
	return 0;
}

[第34問 Shuffle]

ITP1_9_B.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
	string S; int m, h;
	vector<string>buff;

	while(true){
		cin >> S; if (S == "-") break;
		cin >> m;

		for (int i = 0; i < m; i++) {
			cin >> h;
			//cout << S.substr(h, S.size() - h) << " " << S.substr(0, h) << "\n";
			S =  S.substr(h, S.size() - h) + S.substr(0, h);
		}
		buff.push_back(S);
	}

	for (auto x : buff)
		cout << x << "\n";

	//while (1) {}
	return 0;
}

[第35問 Card Game]

ITP1_9_C.cpp
#include <iostream>
using namespace std;

int main() {
	int N; cin >> N;
	int SA = 0, SB = 0;
	string cA, cB;

	for (int i = 0; i < N; i++) {
		cin >> cA >> cB;
		SA += 3*(cA > cB) + (cA == cB);
		SB += 3*(cB > cA) + (cA == cB);
	}

	cout << SA << " " << SB << "\n";
	//while (1) {}
	return 0;
}

[第36問 Transformation]

勉強になりました。

ITP1_9_D.cpp
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

int main() {
	string S; int N; cin >> S >> N;
	string CMD, word; int st, ed;
	vector<string>ans;
	for (int i = 0; i < N; i++) {
		cin >> CMD >> st >> ed;
		if (CMD == "replace") {
			cin >> word;
			S =S.substr(0, st) + word + S.substr(ed+1, S.size() - ed-1);
			//cout << S << "\n";
		}
		
		else if (CMD == "reverse") {
			reverse(S.begin()+st, S.end()-(S.size()-ed-1));
			//cout << S << "\n";
		}
		
		else {
			ans.push_back(S.substr(st, ed-st+1));
			//cout << S.substr(st, ed+1) << "\n";
		}
		
	}

	//cout << "\n";
	for (auto x : ans)
		cout << x << "\n";

	//while (1) {}
	return 0;
}

[第37問 Distance]

ITP1_10_A.cpp
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int main() {
	double x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;

	double ans = pow((pow((x1 - x2),2) + pow((y1 - y2),2)),0.5);
	cout << fixed << ans << setprecision(5);
	//while (1) {}
	return 0;
}

[第38問 Traiangle]

ITP1_10_B.cpp
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
#define PI 3.14159265359

int main() {
	double a, b, C; cin >> a >> b >> C;
	double S = (a * sin((PI / 180) * C) * b) / 2;
	double L = a + b + pow(a * a + b * b - 2 * a * b * cos((PI / 180) * C),0.5);
	double h = b * sin((PI / 180) * C);

	cout << fixed << S << "\n" << L << "\n" << h <<setprecision(5) << "\n";
	//while (1) {}
	return 0;
}

[第39問 Standard Deviation]

ITP1_10_C.cpp
#include <iostream>
#include <iomanip>
#include <vector>
#include <math.h>
using namespace std;
#define PI 3.14159265359

int main() {
	double n;
	vector<double>buff;
	while (true) {
		cin >> n;
		if (n == 0)break;

		double m = 0; vector<double>s(n, 0);
		for (int i = 0; i < n; i++) {
			cin >> s[i];
			m += s[i];
		}m /= n;

		double num = 0;
		for (auto x : s)
			num += pow((x - m), 2);
		num /= n;
		buff.push_back(num);
	}
	//cout << "\n";
	for(auto y:buff)
		cout << fixed << pow(y, 0.5) << setprecision(4) << "\n";
	
//	while (1) {}
	return 0;
}

[第40問 Distance II]

※math.h よりも cmath の方が安定。

ITP1_10_D.cpp
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>//    <= ※
using namespace std;

int main() {
	int n; cin >> n;
	vector<double>x(n, 0);
	vector<double>y(n, 0);
	for (int i = 0; i < n; i++) cin >> x[i];
	for (int i = 0; i < n; i++) cin >> y[i];

	double p1 = 0, p2 = 0, p3 = 0 , pm = 0;
	for (int i = 0; i < n; i++) {
		p1 += abs(x[i] - y[i]);
		p2 += pow(abs(x[i] - y[i]), 2);
		p3 += pow(abs(x[i] - y[i]), 3);
		pm = max(pm, abs(x[i] - y[i]));
	}
	p2 = pow(p2, (double)1/(double)2);
	p3 = pow(p3, (double)1/(double)3);
	cout << fixed << setprecision(5) << p1 << "\n" << p2 << "\n" << p3 << "\n" << pm << "\n";

	//while (1) {}
	return 0;
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?