네이버 지식인 c++ 문제 풀이
url : http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=289101637
// ConsoleApplication5.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
#include "stdafx.h"
#define ARR_SIZE_R 3
#define ARR_SIZE_C 4
class Mat{
public:
int A_RRY[ARR_SIZE_R][ARR_SIZE_C];
int B_RRY[ARR_SIZE_R][ARR_SIZE_C];
int sum_RRY[ARR_SIZE_R][ARR_SIZE_C];
Mat(); // 생성자
void summation();
};
Mat::Mat() { // 데이터 초기화
this->A_RRY[ARR_SIZE_R][ARR_SIZE_C] = { 0, };
this->B_RRY[ARR_SIZE_R][ARR_SIZE_C] = { 0, };
this->sum_RRY[ARR_SIZE_R][ARR_SIZE_C] = { 0, };
}
void Mat::summation() {
for (int R = 0; R < ARR_SIZE_R; R++) {
for (int C = 0; C < ARR_SIZE_C; C++) {
sum_RRY[R][C] = A_RRY[R][C] + B_RRY[R][C];
}
}
}
int main(void) {
Mat* s = NULL;
s = new Mat;
//std::cout << "s => " << s << std::endl;
// 값 입력
// A 행렬
for (int _R = 0; _R < ARR_SIZE_R; _R++) {
//std::cout << "test" << std::endl;
for (int _C = 0; _C < ARR_SIZE_C; _C++) {
std::cout << "s-> A_RRY [" << _R << "][" << _C << "] => ";
std::cin >> s->A_RRY[_R][_C];
}
}
// B 행렬
for (int _R = 0; _R < ARR_SIZE_R; _R++) {
for (int _C = 0; _C < ARR_SIZE_C; _C++) {
std::cout << "s-> B_RRY [" << _R << "][" << _C << "] => ";
std::cin >> s->B_RRY[_R][_C];
}
}
s->summation(); // _________________ f: summation call
for (int _R = 0; _R < ARR_SIZE_R; _R++) {
for (int _C = 0; _C < ARR_SIZE_C; _C++) {
std::cout.setf(std::ios::right);
std::cout << std::setw(3) << s->sum_RRY[_R][_C];
if (_C == ARR_SIZE_C - 1) {
std::cout << std::endl;
}
else {
std::cout << " ";
}
}
}
delete s;
return 0;
}
'언어 > c++' 카테고리의 다른 글
c++, for (0) | 2017.07.08 |
---|---|
c++, swap (0) | 2017.07.02 |
문자열 escape (0) | 2017.07.02 |
c++ , size, length (0) | 2017.07.02 |
c++ 문자열 파싱, substr (0) | 2017.07.02 |
# include <iostream>
# include <string>
int main(void) {
std::string s = "hello world";
for (unsigned int i = 0; i < s.length(); i++) {
std::cout << s[i] << std::endl;
}
std::cout << std::endl;
for (char data : s) {
std::cout << data << std::endl;
}
}
'언어 > c++' 카테고리의 다른 글
네이버 지식인 c++ 문제 풀이 (0) | 2017.11.26 |
---|---|
c++, swap (0) | 2017.07.02 |
문자열 escape (0) | 2017.07.02 |
c++ , size, length (0) | 2017.07.02 |
c++ 문자열 파싱, substr (0) | 2017.07.02 |
/*.swap : 문자열을 서로 바꾼다*/
# include <iostream>
# include <string>
int main(void)
{
std::string s1 = "hello";
std::string s2 = "world";
s1.swap(s2);
std::cout << "s1 => " << s1 << " s2 => " << s2 << std::endl;
return 0;
}
'언어 > c++' 카테고리의 다른 글
네이버 지식인 c++ 문제 풀이 (0) | 2017.11.26 |
---|---|
c++, for (0) | 2017.07.08 |
문자열 escape (0) | 2017.07.02 |
c++ , size, length (0) | 2017.07.02 |
c++ 문자열 파싱, substr (0) | 2017.07.02 |
문자열 escape
# include <iostream>
# include <string>
int main(void) {
std::string sData = "\'Hello\'";
std::cout << sData << std::endl;
return 0;
}
'언어 > c++' 카테고리의 다른 글
c++, for (0) | 2017.07.08 |
---|---|
c++, swap (0) | 2017.07.02 |
c++ , size, length (0) | 2017.07.02 |
c++ 문자열 파싱, substr (0) | 2017.07.02 |
c++ 동적할당 (0) | 2017.07.01 |
c++ , size, length
# include <iostream>
# include <string>
int main(void) {
std::string s("My name is Kim Jun Hyeon");
int s_(s.size());
int l_(s.length());
int i; // index
for (i = 0; i < l_; i++) {
std::cout << i << " => " << s[i] << std::endl;
}
std::cout << s_ << std::endl;
std::cout << l_ << std::endl;
return 0;
}
[c언어] ====================================================
# include <stdio.h>
# include <string.h>
int main(void) {
char s[] = "hello world";
int length_ = strlen(s);
printf("%d \n", length_);
return 0;
}
'언어 > c++' 카테고리의 다른 글
c++, swap (0) | 2017.07.02 |
---|---|
문자열 escape (0) | 2017.07.02 |
c++ 문자열 파싱, substr (0) | 2017.07.02 |
c++ 동적할당 (0) | 2017.07.01 |
c++ 행렬 (0) | 2016.05.21 |
c++ 문자열 파싱, substr
# include <iostream>
# include <string>
int main(void)
{
std::string num;
std::string result;
std::cin >> num;
result += num.substr(0, 6);
result += num.substr(7, 15);
std::cout << result << std::endl;
return 0;
}
'언어 > c++' 카테고리의 다른 글
문자열 escape (0) | 2017.07.02 |
---|---|
c++ , size, length (0) | 2017.07.02 |
c++ 동적할당 (0) | 2017.07.01 |
c++ 행렬 (0) | 2016.05.21 |
각 자리 숫자 더하기 (0) | 2016.03.23 |
# include <iostream>
int main(void)
{
// 인스턴스만 동적으로 생성하는 경우
int* pData = new int;
*pData = 10;
// 초깃값을 기술하는 경우
int* pNewData = new int(1000);
std::cout << "*pData => " << *pData << std::endl;
std::cout << "*pNewData => " << *pNewData << std::endl;
delete pData;
delete pNewData;
return 0;
}
'언어 > c++' 카테고리의 다른 글
c++ , size, length (0) | 2017.07.02 |
---|---|
c++ 문자열 파싱, substr (0) | 2017.07.02 |
c++ 행렬 (0) | 2016.05.21 |
각 자리 숫자 더하기 (0) | 2016.03.23 |
stu1 (0) | 2016.02.25 |
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#define _ROW_ 2
#define _COL_ 2
#define MAT int
using namespace std;
class MATRIX {
public:
MAT A_rray[_ROW_][_COL_];
MAT B_rray[_ROW_][_COL_];
MAT A_plus_B[_ROW_][_COL_];
MAT A_sub_B[_ROW_][_COL_];
MAT A_mul_B[_ROW_][_COL_];
public:
MATRIX();
void dataInput();
void addition();
void subtraction();
void multiplication();
};
int main(void) {
MATRIX* M = NULL;
M = new MATRIX;
M->dataInput();
M->addition();
M->subtraction();
M->multiplication();
return 0;
}
MATRIX::MATRIX() {
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
this->A_rray[i][j] = 0;
this->B_rray[i][j] = 0;
this->A_plus_B[i][j] = 0; // _'+'_
this->A_sub_B[i][j] = 0; // _'-'_
this->A_mul_B[i][j] = 0; // _'X'_
}
}
cout << " ================================================== " << endl;
// [ A_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_rray[" << i << "]" << "[" << j << "] = "
<< setw(2) << this->A_rray[i][j] << " ";
}
cout << endl;
}
cout << endl;
// [ B_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " B_rray[" << i << "]" << "[" << j << "] = "
<< setw(2) << this->B_rray[i][j] << " ";
}
cout << endl;
}
cout << endl;
// [ A+B_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_plus_B[" << i << "]" << "[" << j << "] = "
<< setw(2) << this->A_plus_B[i][j] << " ";
}
cout << endl;
}
cout << endl;
// [ A-B_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_sub_B[" << i << "]" << "[" << j << "] = "
<< setw(2) << this->A_sub_B[i][j] << " ";
}
cout << endl;
}
cout << endl;
// [ AXB_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_mul_B[" << i << "]" << "[" << j << "] = "
<< setw(2) << this->A_mul_B[i][j] << " ";
}
cout << endl;
}
cout << " ================================================== " << endl;
} // END OF MATRIX()
void MATRIX::dataInput() {
// [ A_RRAY ] DATA SETTING
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_rray[" << i << "]" << "[" << j << "] = ";
cin >> this->A_rray[i][j];
}
}
// [ A_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_rray[" << i << "]" << "[" << j << "] = "
<< this->A_rray[i][j] << " ";
}
cout << endl;
}
cout << endl;
// [ B_RRAY ] DATA SETTING
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " B_rray[" << i << "]" << "[" << j << "] = ";
cin >> this->B_rray[i][j];
}
}
// [ B_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " B_rray[" << i << "]" << "[" << j << "] = "
<< this->B_rray[i][j] << " ";
}
cout << endl;
}
}// END OF dataInput()
void MATRIX::addition() {
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
this->A_plus_B[i][j] = this->A_rray[i][j] + this->B_rray[i][j];
}
}
cout << " ==================[ Addition ]==================== " << endl;
// [ A+B_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_plus_B[" << i << "]" << "[" << j << "] = "
<< setw(2) << this->A_plus_B[i][j] << " ";
}
cout << endl;
}
cout << " ================================================== " << endl;
}
void MATRIX::subtraction() {
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
this->A_plus_B[i][j] = this->A_rray[i][j] - this->B_rray[i][j];
}
}
cout << " ================[ Subtraction ]================== " << endl;
// [ A+B_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_sub_B[" << i << "]" << "[" << j << "] = "
<< setw(2) << this->A_sub_B[i][j] << " ";
}
cout << endl;
}
cout << " ================================================== " << endl;
}
void MATRIX::multiplication() {
int sum = 0;
for (int k = 0; k < 2; k++){
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
sum += this->A_rray[k][j] * this->B_rray[j][i];
}
this->A_mul_B[k][i] = sum;
sum = 0;
}
}
cout << " ===============[ Multiplication ================= " << endl;
// [ A+B_RRAY ] test printf
for (int i = 0; i < _ROW_; i++) {
for (int j = 0; j < _COL_; j++) {
cout << " A_sub_B[" << i << "]" << "[" << j << "] = "
<< setw(2) << this->A_mul_B[i][j] << " ";
}
cout << endl;
}
cout << " ================================================== " << endl;
}
'언어 > c++' 카테고리의 다른 글
c++ 문자열 파싱, substr (0) | 2017.07.02 |
---|---|
c++ 동적할당 (0) | 2017.07.01 |
각 자리 숫자 더하기 (0) | 2016.03.23 |
stu1 (0) | 2016.02.25 |
변형 시저 (0) | 2016.02.08 |
각 자리 숫자 더하기
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string number;
cout << " number input >>> ";
cin >> number;
int number_data = 0;
int sum = 0;
for (int i = 0; i < int(number.length()); i++) {
number_data = int(number[i])-48;
cout << number_data << endl;
sum += number_data;
}
cout <<"sum = >"<< sum << endl;;
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
int main(void)
{
int length = 0;
int* arr = NULL;
arr = new int[length];
cout<<" array size ???? ";
cin>> length;
// initiallize
for(int k = 0; k<length; k++)
arr[k] = 0;
//test data insert
arr[0] = 1;
arr[1] = 2;
for(int j = 0; j<length; j++)
cout<<arr[j]<<endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<int> v; //[v는 빈 컨테이너이다.]
v.reserve(10); //벡터 메모리 공간 & 예약
//데이터 삽입
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
v.push_back(60);
for (vector<int>::size_type i = 0; i< v.size(); i++)
cout << v[i] <<endl;
return 0;
}
'언어 > c++' 카테고리의 다른 글
c++ 문자열 파싱, substr (0) | 2017.07.02 |
---|---|
c++ 동적할당 (0) | 2017.07.01 |
c++ 행렬 (0) | 2016.05.21 |
각 자리 숫자 더하기 (0) | 2016.03.23 |
변형 시저 (0) | 2016.02.08 |