Medusa Cpp Study 숙제 ¶
정사각형..
~cpp #include <iostream> using std::cout; using std::cin; using std::endl; int main () { int num; cout << " 숫자를 입력하시오 :" ; cin >> num; int rows = num ; int cols = num ; for( int r=0; r!=rows; r++) { for (int c=0; c != cols; c++) { if(r==0 || r== rows-1 ) { cols=num*2-1; if( c%2==0 || c==cols-1) cout << "*"; else cout << " " ; } else if (c==0 || c==cols-1) cout <<"*"; else cout << " "; } cout << endl; } return 0; }
직사각형
~cpp #include <iostream> using std::cout; using std::cin; using std::endl; int main () { int width, length ; cout<< "두개의 숫자를 입력하세요 :" ; cin >> width >> length ; int rows = length ; int cols = width ; for( int r=0; r!=rows; r++) { for (int c=0; c != cols; c++) { if(r==0 || r== rows-1 ) { cols=width*2-1; if( c%2==0 || c==cols-1) cout << "*"; else cout << " " ; } else if (c==0 || c==cols-1) cout <<"*"; else cout << " "; } cout << endl; } return 0; }
정삼각형
~cpp #include <iostream> using std::cout; using std::cin; using std::endl; int main () { int num; cout << "숫자를 입력하세요 :" ; cin >> num ; int rows = num ; int cols = num ; int r=0; while(r!=rows) { for ( int c=0; c!=cols; c++) { if (r==0) { if(c==cols-1) cout << "*"; else cout << " "; } else if(r==rows-1) { cols=num*2-1; if(c%2==0) cout << "*"; else cout << " "; } else { if (c==cols-1) cout << "*"; else { if(c==num-r-1) cout << "*"; else cout << " "; } } } cout << endl; r++; cols++; } return 0; }
큰 수 4개
~cpp #include <iostream> #include <algorithm> #include <vector> using std::cout; using std::cin; using std::endl; using std::vector; using std::sort; int main() { cout << "숫자를 입력하세요 :"; vector<int> number; int num; while(cin >> num) number.push_back(num); int size= number.size(); sort(number.begin(), number.end()); if(size < 4) { int i = 0; while(i < number.size() && i < 4) { cout << number[i] << ", "; i++; } } else { cout << number[size - 4] << ", " << number[size - 3] << ", " << number[size - 2] << ", " << number[size - 1]; } cout << endl; return 0; }
문자열 입력 받아 가장 큰 거랑 가장 작은 거 길이 출력
~cpp #include <algorithm> #include <iostream> #include <string> #include <vector> using std::sort; using std::vector; using std::cin; using std::cout; using std::endl; using std::string; int main() { cout << "아무글이나 쓰시오 :"; string word; vector<int> nums; while(cin >> word) { if(word != ";") { int size = word.size(); nums.push_back(size); } else break; } sort(nums.begin(), nums.end()); cout << nums[0] << ", " << nums[nums.size() - 1] << endl; return 0; }
~cpp #include <iostream> #include <vector> using std::cin; using std::cout; using std::endl; using std::vector; int main() { int size ; cout << "숫자를 입력하세요 : "; cin >> size; while(size%2==0) { cout << "짝수입니다 . 다시 입력하세요 :"; cin >> size; } vector< vector<int> > board(size); for(int i = 0; i < size; i++) board[i].resize(size); int row=0; int col=size/2; board[row][col]=1; int num=2; while(num!=size*size+1) { row--; col++; if(board[row][col]==0) { if(row < 0) row = size-1; if(col > size-1) col = 0; if(row < 0 && col > size-1) { row = size-1; col = 0; } } else { row += 2; col--; } board[row][col]=num; num++; } for(int r = 0; r < size; r++) { for(int c = 0; c < size; c++) { cout << board[r][c] << "\t"; } cout << endl; } return 0; }