A - Your First Judge Editorial by physics0523
プログラムに対する入力を文字列 \(S\) として受け取り、 \(S\) が Hello,World!
と一致するかどうかを判定すればよいですが、ポイントとなる点が \(2\) 点あります。
・入力を受け取る/答えを出力する
あなたが用いるプログラミング言語によって、入力を \(1\) つの文字列として受け取ったり、答えを出力したりするときにとるべき方法は大きく異なります。また、一部のプログラミング言語にとってはこれは難しいことであるかもしれません。
標準入出力の方法を調べるためには、以下のような方法が考えられます。
- その言語に関するドキュメントや記事などを参照する。
- 過去に開催されたコンテストなどで、その言語で行われた提出を参照する。
・文字列同士を比較する
文字列同士の比較の方法も、プログラミング言語によって大きく異なります。文字列同士の比較演算子の定義が言語によって微妙に異なったり、そもそも文字列型が存在する言語と存在しない言語があったりもします。
しかし、大抵の言語には文字列を比較するためになんらかの方法が用意されているので、その使い方を理解しておくことが肝要です。
以下に様々な言語による実装例を示します。
一部の言語では copy ボタンで正常に copy&paste が行えません。ご注意ください。
C
#include<stdio.h>
int main(){
char s[32];
scanf("%s",s);
if(strcmp(s,"Hello,World!")==0){printf("AC\n");}
else{printf("WA\n");}
return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
if(s=="Hello,World!"){cout << "AC\n";}
else{cout << "WA\n";}
return 0;
}
Java
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s = sc.next();
String ac = new String("Hello,World!");
if(s.equals(ac)){System.out.println("AC");}
else{System.out.println("WA");}
}
}
Python
s = input()
if (s=="Hello,World!"):
print("AC")
else:
print("WA")
Bash
#!/bin/bash
read -r s
if [ $s = "Hello,World!" ]; then
echo AC
else
echo WA
fi
Awk
{print $1=="Hello,World!"?"AC":"WA"}
C#
using System;
class Program{
static void Main(string[] args){
string s = Console.ReadLine();
string ac = "Hello,World!";
if(s.Equals(ac)){Console.WriteLine("AC");}
else{Console.WriteLine("WA");}
}
}
Clojure
(println (if (= (read-line) "Hello,World!") "AC" "WA"))
Crystal
s = read_line
puts s=="Hello,World!" ? "AC" : "WA"
D
import std.stdio;
import std.string;
import std.conv;
void main(){
string s=chomp(readln());
writefln("%s",s=="Hello,World!"?"AC":"WA");
}
Dart
import 'dart:io';
main(List<String> args) {
String s = stdin.readLineSync();
if(s=="Hello,World!"){print("AC");}
else{print("WA");}
}
Erlang
-module('Main').
-export([main/1]).
calc("Hello,World!") -> true;
calc(_) -> false.
solve() ->
[S] = input("~s"),
io:format("~s~n", [case calc(S) of true -> "AC"; false-> "WA" end]),
ok.
main(_) ->
solve(),
halt().
input(Pat) ->
{ok, L} = io:fread("", Pat),
L.
Elixir
defmodule Main do
def main do
s = IO.read(:line) |> String.trim
if (s == "Hello,World!"), do: IO.puts("AC"), else: IO.puts("WA")
end
end
F#
open System
let s = stdin.ReadLine()
if (s = "Hello,World!") then "AC" else "WA"
|> printfn "%s"
Fortran
program abc215a
implicit none
character(32)::s
read(*, "(a)") s
if(s=="Hello,World!") then
print"(a)","AC"
else
print"(a)","WA"
end if
end program abc215a
Go
package main
import "fmt"
func main() {
var s string
fmt.Scan(&s)
if(s=="Hello,World!"){
fmt.Println("AC")
}else{
fmt.Println("WA")
}
}
Haskell
import Control.Applicative
main :: IO ()
main = do
s <- getLine
putStrLn $ if s=="Hello,World!" then "AC" else "WA"
Haxe
import haxe.io.Input;
class Main {
static function main() {
var input : Input = Sys.stdin();
var s = input.readLine();
if(s=="Hello,World!") Sys.print("AC") else Sys.print("WA");
}
}
JavaScript
function main(input) {
const S = input.split('\n')[0];
if(S == 'Hello,World!'){
console.log('AC');
}else{
console.log('WA');
}
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
Julia
s = readline()
if s=="Hello,World!"
println("AC")
else
println("WA")
end
Kotlin
fun main(args: Array<String>){
val s = readLine()!!
println(if(s=="Hello,World!") "AC" else "WA")
}
Lua
s = io.read()
if s=="Hello,World!" then
print("AC")
else
print("WA")
end
Nim
import strutils
var s = readLine(stdin)
if s=="Hello,World!":
echo "AC"
else:
echo "WA"
Objective-C
#import <Foundation/Foundation.h>
#import <stdarg.h>
@interface StdIO : NSObject
+(NSString*)stringFromStdIn;
+(void)printf:(NSString*)format, ...;
@end
@implementation StdIO
+(NSString*)stringFromStdIn
{
NSFileHandle *handle = [NSFileHandle fileHandleWithStandardInput];
NSMutableString *result = [NSMutableString string];
NSData *data = [handle availableData];
while ([data length] > 0) {
[result appendString:[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]];
data = [handle availableData];
}
NSUInteger length = [result length];
if (length >= 1 && [result characterAtIndex:length-1] == '\n') {
[result deleteCharactersInRange:NSMakeRange(length-1,1)];
}
return result;
}
+(void)printf:(NSString*)format, ...
{
va_list arguments;
va_start(arguments, format);
NSString *string = [[[NSString alloc] initWithFormat:format arguments:arguments] autorelease];
NSFileHandle *fileHandle = [NSFileHandle fileHandleWithStandardOutput];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
va_end(arguments);
}
@end
int main() {
@autoreleasepool {
NSString *S = [StdIO stringFromStdIn];
if([S isEqualToString:@"Hello,World!"]){[StdIO printf:@"AC"];}
else{[StdIO printf:@"WA"];}
}
}
Common Lisp
(defun solve(s)
(if (string= s "Hello,World!")
"AC"
"WA"))
(format t "~A~%" (solve(read-line)))
Ocaml
let () = Scanf.scanf "%s" (fun s -> print_endline @@ if s = "Hello,World!" then "AC" else "WA")
Octave
s=scanf('%s', 'C');
if strcmp(s,'Hello,World!')
printf('AC');
else
printf('WA');
end
Pascal
program abc215a;
uses SysUtils;
var
s: string;
begin
readln(s);
if s='Hello,World!' then writeln('AC') else writeln('WA');
end.
Perl
my $s=<>;
chomp($s);
print $s eq "Hello,World!"?"AC":"WA";
Raku
my $s;
$s=get;
say $s eq "Hello,World!"??"AC"!!"WA";
PHP
<?php
fscanf(STDIN, "%s", $s);
echo $s=="Hello,World!"?"AC":"WA"."\n";
?>
Prolog
get_string(String) :-
current_input(Input),
read_string(Input, ' \n', '', _, String).
main :-
get_string(S),
S == "Hello,World!" ->
write('AC');
write('WA').
:- main.
Racket
#lang racket
(define s (read-line))
(display (if (equal? s "Hello,World!") "AC" "WA"))
(newline)
Ruby
s = gets.chomp
puts (s=="Hello,World!") ? 'AC' : 'WA'
Rust
use proconio::input;
fn main() {
input! {
s: String,
}
let answer = if s=="Hello,World!" { "AC" } else { "WA" };
println!("{}", answer)
}
Scala
object Main {
def main(args:Array[String]) = {
val s = io.StdIn.readLine
println(if(s=="Hello,World!") "AC" else "WA")
}
}
Scheme
(define s (read-line))
(display (if (equal? s "Hello,World!") "AC" "WA"))
(newline)
Standard ML
val SOME s = TextIO.inputLine TextIO.stdIn
val () =
print(if s = "Hello,World!\n" then "AC\n" else "WA\n")
Swift
let s=readLine()!
print(s=="Hello,World!" ? "AC" : "WA")
Typescript
import * as fs from 'fs';
const input = fs.readFileSync("/dev/stdin", "utf8").split("\n");
const s = input[0];
const ac = new String("Hello,World!");
console.log(s==ac?"AC":"WA")
Visual Basic
module abc215a
sub Main(ByVal args() as String)
dim s as string
s=Console.ReadLine()
Console.Write(IF(s="Hello,World!","AC","WA"))
end sub
end module
Zsh
read s
if [ $s = "Hello,World!" ]; then
echo "AC"
else
echo "WA"
fi
posted:
last update: