落書きノート

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

C/C++で不定期練習 AIZU ONLINE JUDGE

// Simple Calculator

#include <iostream>

using namespace std;

int main(void) {
  int a,b;
  char op;
  while(cin >> a >> op >> b) {
    if(op == '+')
      cout << a + b << endl;
    else if(op == '-')
      cout << a - b << endl;
    else if(op == '*')
      cout << a * b << endl;
    else if(op == '/')
      cout << a / b << endl;
    else if(op == '?')
      break;
  }
  return 0;
}


// Min, Max and Sum (C言語)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void) {
  int n,p = 0,i;
  char buf[1000000];
  char *tok,ch;
  int min,max;
  long long sum;
  scanf("%d", &n);
  while((ch = fgetc(stdin)) != '\n') {};
  int a[n];
  fgets(buf, sizeof(buf), stdin);
  tok = strtok(buf, " ");
  while(tok != NULL) {
    a[p++] = atoi(tok);
    tok = strtok(NULL, " ");
  }
  min = a[0];
  max = a[0];
  sum = 0;
  for(i = 0; i < n; i++) {
    if(min > a[i])
      min = a[i];
    if(max < a[i])
      max = a[i];
    sum += a[i];
  }
  printf("%d %d %lld\n", min, max, sum);
  return 0;
}



// Min, Max and Sum (C++)

#include <iostream>
#include <cstdlib>
#include <sstream>

using namespace std;

int main(void) {
  int n,j = 0;
  string s, item;
  int min,max;
  long long sum;
  cin >> n;
  cin.ignore();
  int a[n];
  getline(cin, s);
  stringstream ss(s);
  while(getline(ss, item, ' '))
    a[j++] = atoi(item.c_str());
  min = a[0];
  max = a[0];
  sum = 0;
  for(int i = 0; i < n; i++) {
    if(min > a[i])
      min = a[i];
    if(max < a[i])
      max = a[i];
    sum += a[i];
  }
  cout << min << ' ' << max << ' ' << sum << endl;
  return 0;
}