落書きノート

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

C++で問題を解く

#include <iomanip>
#include <iostream>

using namespace std;

int main(void){
  int a[3] = {1, 2, 3};
  int b[2][3] = {
    {1,2,3},
    {4,5,6},
  };
  int c[3][2] = {
    {0},
    {2,3},
    {4},
  };
  int d[3][2] = {0,1,2};

  for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++){
    cout << "a[" << i << "] = " << a[i] << endl;
  }

  cout << "行列b" << endl;

  for(int i = 0; i < sizeof(b) / sizeof(b[0]); i++){
    for(int j = 0; j < sizeof(b[0]) / sizeof(b[0][0]); j++){
      cout << setw(4) << b[i][j];
    }
    cout << endl;
  }

  cout << "行列c" << endl;

  for(int i = 0; i < sizeof(c) / sizeof(c[0]); i++){
    for(int j = 0; j < sizeof(c[0]) / sizeof(c[0][0]); j++){
      cout << setw(4) << c[i][j];
    }
    cout << endl;
  }

  cout << "行列d" << endl;

  for(int i = 0; i < sizeof(d) / sizeof(d[0]); i++){
    for(int j = 0; j < sizeof(d[0]) / sizeof(d[0][0]); j++){
      cout << setw(4) << d[i][j];
    }
    cout << endl;
  }

  return 0;
}
#include <iostream>

using namespace std;

int sign_of(int n){
  int ans;

  if(n < 0)
    ans = -1;
  else if(n > 0)
    ans = 1;
  else
    ans = 0;

  return ans;
}

int main(void){
  int x, y;

  cout << "整数を2つ入力してください。" << endl;
  cin >> x >> y;

  cout << sign_of(x) << endl;
  cout << sign_of(y) << endl;
  
  return 0;
}
#include <iostream>

using namespace std;

int min(int a, int b, int c){
  int min = a;

  if(min > b) min = b;
  if(min > c) min = c;

  return min;
}

int main(void){
  int x, y, z;

  cout << "3つの整数値を入力してください。" << endl;
  cin >> x >> y >> z;

  cout << min(x, y, z) << endl;

  return 0;
}
#include <iostream>

using namespace std;

int med(int a, int b, int c){
  int ans = 0;

  if(a >= b && b >= c || a <= b && b <= c)
    ans = b;
  else if(b >= c && c >= a || b <= c && c <= a)
    ans = c;
  else if(c >= a && a >= b || c <= a && a <= b)
    ans = a;

  return ans;
}

int main(void){
  int x, y, z;

  cout << "3つの整数値を入力してください。" << endl;
  cin >> x >> y >> z;

  cout << med(x, y, z) << endl;

  return 0;
}

中央値の問題、やりづらい…。