for반복문 연습

computer_log·2023년 8월 22일

배열의 숫자만큼 반복해서 출력


#include <iostream>
using namespace std;

int arr[4] = { 1,2,3,4 };

int main() {

	//1
	//2 2
	//3 3 3
	//4 4 4 4

	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < arr[i]; j++) {
			cout << arr[i] << " ";
		}
		cout << "\n";
	}

	return 0;
}

[출력]
1
2 2
3 3 3
4 4 4 4

[출력]

4927
3253
7616

[수정출력]


#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> v(4);
vector<vector<int>> result(4);
int arr[4] = { 1,2,1,2 };
void rotate(vector<int>& arr) {
    vector<int> rotated(3);
    for (int x = 0; x < 3; x++) {
        rotated[x] = arr[(x + 2) % 3]; // Rotate the values
    }
    arr = rotated; // Update the original vector with the rotated values
}

int main() {
    v[0] = { 3, 7, 4 };
    v[1] = { 2, 6, 9 };
    v[2] = { 5, 1, 2 };
    v[3] = { 3, 6, 7 };

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 2; j++) {
            rotate(v[i]);
        }
    }

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            cout << v[i][j] << ' ';
        }
        cout << endl;
    }

    return 0;
}

profile
computer_log

0개의 댓글