LoginSignup
0
0

More than 3 years have passed since last update.

問題集への解答

Posted at

C016 文字列の置き換え

置き換え前 置き換え後
A 4
E 3
G 6
I 1
O 0
S 5
Z 2
    $input_line = fgets(STDIN);
    $array=array(
        "A"=>   4,
        "E"=>   3,
        "G"=>   6,
        "I"=>   1,
        "O"=>   0,
        "S"=>   5,
        "Z"=>   2  
        );

    $newword=$input_line;
    for ($i = 0; $i < strlen($input_line); $i++) {
        foreach ($array as $key=>$value) {
            if($newword[$i]==$key){
                $newword[$i]=$value;
            }
        }
    }
    echo $newword."\n";

C019

<?php
$count=(int)fgets(STDIN);
for ($i=0; $i <$count ; $i++) {
    print res((int)fgets(STDIN));
}
function res($num)
{
    $sum=0;

    for ($i=1; $i <$num ; $i++) {
        if ($num%$i==0) {
            $sum+=$i;
        }
    }
    switch ($sum) {
      case $num:
      print "perfect\n";
      break;
      case abs($num-1):
      print "nearly\n";
      break;

      default:
      print "neither\n";
      break;
    }
}

C024

<?php

function toArray($input){
  $s=explode(' ',trim($input));
  return $s;
}

$n=(int)fgets(STDIN);
$x1=0;
$x2=0;
for ($i=0; $i <$n ; $i++) {
  $query=toArray(fgets(STDIN));
  switch ($query[0]) {
    case 'SET':
      if($query[1]==1){
        $x1=$query[2];
      }else {
        $x2=$query[2];
      }
      break;
      case 'ADD':
        $x2=$x1+$query[1];
        break;
        case 'SUB':
          $x2=$x1-$query[1];
          break;
  }
}
print $x1.' '.$x2."\n";

C034

<?php
function toArray($input){
  $input=trim($input);
  return explode(' ',$input);
}
// $input='3 + 1 = x';
// $nums=toArray(fgets(STDIN));
$input=toArray(fgets(STDIN));


$x=array_search('x', $input );
$i=1;
if($input[1]=='-')  $i=-1;



switch ($x) {
  case 0:
    $x=$input[4]-$input[1]*$i;
    break;
    case 2:
      $x=$input[4]*$i-$input[0]*$i;
      break;
      case 4:
        $x=$input[0]+$input[2]*$i;
        break;


}
print $x;

C035

<?php
function toArray($input){
  $input=trim($input);
  return explode(' ',$input);
}
// $input='3 + 1 = x';
// $nums=toArray(fgets(STDIN));
$count=0;
$n=fgets(STDIN);
for ($i=0; $i <$n ; $i++) {
  $res=[];
  $s=explode(" ",trim(fgets(STDIN)));
  $key=$s[0];
  unset($s[0]);
  $res[$key]=$s;


  $num=0;
  if(array_sum($res[$key])>=350){
    // print_r($res);

    if($key=='l')$num=2;
      $sum=$res[$key][2+$num]+$res[$key][3+$num];  
      if($sum>=160){
        $count++;
        // print "\n\n\n\n".($i+1)."\n";
      }
  }
}
print $count."\n";

C043

<?php
$n=(int)fgets(STDIN);

$array=toArray(fgets(STDIN));

$array=array_count_values($array);




  foreach ($array as $key=>$value) {
    if(max($array)==$value){
      $res[]=$key;
    }
  }

  for ($i=0; $i <count($res) ; $i++) {
    print $res[$i];
    if(count($res)-$i==1){
      print "\n";
    }else{
      print " ";
    }
  }



function toArray($input){

  $s=explode(' ',trim($input));
  return $s;
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0