落書きノート

ふと自分が気になった事を書いてます

C++で問題を解く

#include <iostream>

using namespace std;

void hello(){
  cout << "こんにちは。" << endl;
}

int main(void){
  int count;

  cout << "挨拶の回数は:";
  cin >> count;

  for(int i = 0; i < count; i++){
    hello();
  }

  return 0;
}




#include <iostream>

using namespace std;

void print_season(int m){
  switch(m){
    case 3: case 4: case 5: cout << "春" << endl; break;
    case 6: case 7: case 8: cout << "夏" << endl; break;
    case 9: case 10: case 11: cout << "秋" << endl; break;
    case 1: case 2: case 12: cout << "冬" << endl; break;
    default: break;
  }
}

int main(void){
  int m;
  cout << "何月ですか(1~12)";
  cin >> m;

  print_season(m);

  return 0;
}




#include <iostream>

using namespace std;

void put_nchar(int n, char c){
  for(int i = 0; i < n; i++){
    for(int j = 0; j < i + 1; j++){
      cout << c;
    }
    cout << endl;
  }
}

void put_stars(int n){
  put_nchar(n, '*');
}

int main(void){
  int line;

  cout << "左下直角の三角形を表示します。" << endl;
  cout << "段数は:";
  cin >> line;

  put_stars(line);

  return 0;
}




// 別解
#include <iostream>

using namespace std;

void put_nchar(int n, char c){
  while(n-- > 0)
    cout << c;
}

void put_stars(int n){
  put_nchar(n, '*');
}

int main(void){
  int n;

  cout << "左下直角の三角形を表示します。" << endl;
  cout << "段数は:";
  cin >> n;

  for(int i = 1; i <= n; i++){
    put_stars(i);
    cout << endl;
  }

  return 0;
}