龙空技术网

编程挑战赛第一周:题目以及各种编程语言解答

启辰8 140

前言:

而今看官们对“atoipython”大致比较注重,朋友们都想要剖析一些“atoipython”的相关文章。那么小编同时在网摘上搜集了一些对于“atoipython””的相关文章,希望你们能喜欢,我们一起来学习一下吧!

挑战#1

编写一个脚本,将字符串“Perl Weekly Challenge”中的字符“e”替换为“E”。 打印字符“e”在字符串中出现的次数。

C语言

# include <stdlib.h># include <stdio.h># include <string.h>int main (void) {    char *  line    = NULL;    size_t  len     = 0;    size_t  strlen;    while ((strlen = getline (&line, &len, stdin)) != -1) {        char * line_ptr = line;        size_t e_count  = 0;        while (* line_ptr) {            if (* line_ptr == 'e') {                * line_ptr = 'E';                e_count ++;            }            line_ptr ++;        }        printf ("%s%zu\n", line, e_count);    }    free (line);    return (0);}

Go语言

package mainimport (    "fmt"    "bufio"    "os"    "strings")func main () {    var reader = bufio . NewReader (os. Stdin)    for {        var text, err = reader . ReadString ('\n')        if (err != nil) {            break        }        fmt . Print   (strings . Replace (text, "e", "E", -1))        fmt . Println (strings . Count (text, "e"))    }}

Java

import java.util.*;public class ch1 {    public static void main (String [] args) {        Scanner scanner = new Scanner (System . in);        while (scanner . hasNextLine ()) {            String line = scanner . nextLine ();            int count   = 0;            int index   = line . indexOf ('e');            while (index >= 0) {                count ++;                index = line . indexOf ('e', index + 1);            }            System . out . println (line . replaceAll ("e", "E"));            System . out . println (count);        }    }}

Lua

for line in io . lines () do    line, count = string . gsub (line, "e", "E");    io . write (line, "\n", count, "\n")end

Node.js

#!/usr/local/bin/node  require      ("fs"). readFileSync (0)               // 完整度祛. toString     ()                // 转换成字符串. split        ("\n")            // 按行分割. filter       (_ => _ . length) // 过滤空行. map          (_ => {    //    // replace() 返回修改后的字符串,所以我们做一个单独的匹配来得到 'e' 的实际数量    //    count = [... _ . matchAll (/e/g)] . length;    //    //进行替换,并打印结果。 还打印 'e' 出现的次数。    //    process . stdout . write (_ . replace (/e/g, "E") + "\n" + count + "\n")});

Pascal

Program ch1;uses    StrUtils, SysUtils;var    line: string;    count: LongInt;begin    while not eof do begin        readln (line);        count := 0;        line := StringReplace (line, 'e', 'E', [rfReplaceAll], count);        writeln (line);        writeln (count);    endend.

Perl

#!/opt/perl/bin/perluse 5.032;use strict;use warnings;no  warnings 'syntax';use experimental 'signatures';use experimental 'lexical_subs';while (<>) {    my $changes = y/e/E/;    say $_, $changes}

Python

#!/opt/local/bin/pythonimport fileinputfor line in fileinput . input ():    print (line . replace ("e", "E"), line . count ("e"))

R语言

#!/usr/local/bin/Rscriptstdin <- file ('stdin', 'r')repeat {    line <- readLines (stdin, n = 1)    if (length (line) == 0) {        break    }    cat (gsub ("e", "E", line), "\n")    cat (nchar (gsub ("[^e]", "", line)), "\n")}

Ruby

ARGF . each_line do |_|    puts _ . gsub "e", "E"    puts _ . count "e"end

Awk

#!/usr/bin/awk{    count = gsub ("e", "E")    print $0    print count}

Bash

#!/bin/shset -fwhile read linedo    echo "${line//e/E}"   # 将所有的'e'替换为'E'; 打印结果。      ees="${line//[^e]}"   # 删除任何不是“e”的。      echo "${#ees}"        # 打印 eesdone
挑战 #2

写一行代码来解决 FizzBuzz 问题并打印数字 1 到 20。但是,任何能被 3 整除的数字都应该用单词“fizz”代替,任何能被 5 整除的数字都应该用单词“buzz”代替。 那些既能被 3 又能被 5 整除的数字就变成了“fizzbuzz”。

C语言

# include <stdlib.h># include <stdio.h># include <string.h>int main (void) {    char *  line    = NULL;    size_t  len     = 0;    size_t  strlen;    while ((strlen = getline (&line, &len, stdin)) != -1) {        int max = atoi (line);        for (int i = 1; i <= max; i ++) {            if (i % 15 == 0) {printf ("%s\n", "fizzbuzz"); continue;}            if (i %  5 == 0) {printf ("%s\n",     "buzz"); continue;}            if (i %  3 == 0) {printf ("%s\n", "fizz");     continue;}            printf ("%d\n", i);        }    }    free (line);    return (0);}

Go语言

package mainimport (    "fmt")func main () {    for {        var max int        n, err := fmt . Scanf ("%d", &max)        if n != 1 || err != nil {            break        }        for i := 1; i <= max; i ++ {            if i % 15 == 0 {                fmt . Println ("fizzbuzz")                continue            }            if i %  5 == 0 {                fmt . Println (    "buzz")                continue            }            if i %  3 == 0 {                fmt . Println ("fizz"    )                continue            }            fmt . Println (i)        }    }}

Java

import java.util.*;public class ch2 {    public static void main (String [] args) {        Scanner scanner = new Scanner (System . in);        while (scanner . hasNextInt ()) {            int max = scanner . nextInt ();            for (int i = 1; i <= max; i ++) {                System . out . println (i % 15 == 0 ? "fizzbuzz"                                      : i %  5 == 0 ?     "buzz"                                      : i %  3 == 0 ? "fizz"                                      :                i);            }        }    }}

Lua

for line in io . lines () do    for i = 1, line do        if    i % 15 == 0        then            out = "fizzbuzz"        elseif i %  5 == 0        then            out =     "buzz"        elseif i %  3 == 0        then            out = "fizz"        else            out =  i        end        io . write (out, "\n")    endend

Node.js

#!/usr/local/bin/node  require      ("fs"). readFileSync (0)               // 完整读取.. toString     ()                //转换成字符串.. split        ("\n")            // 按行分割.. filter       (_ => _ . length) // 过滤空行. map          (_ => {    for (let i = 1; i <= +_; i ++) {        console . log (i % 15 == 0 ? "fizzbuzz"                     : i %  5 == 0 ?     "buzz"                     : i %  3 == 0 ? "fizz"                     : i)    }});

Pascal

Program XXX;var    i, max: integer;begin    while not eof do begin        readln (max);        for i := 1 to max do begin            if i mod 15 = 0 then begin                writeln ('fizzbuzz');                continue;            end;            if i mod  5 = 0 then begin                writeln (    'buzz');                continue;            end;            if i mod  3 = 0 then begin                writeln ('fizz'    );                continue;            end;            writeln (i);        end    endend.

Perl

#!/opt/perl/bin/perluse 5.032;use strict;use warnings;no  warnings 'syntax';use experimental 'signatures';use experimental 'lexical_subs';while (<>) {    foreach my $number (1 .. $_) {        say $number % 15 == 0 ? "fizzbuzz"          : $number %  5 == 0 ? "buzz"          : $number %  3 == 0 ? "fizz"          :                      $number    }}

Python

#!/opt/local/bin/pythonimport fileinputfor max in fileinput . input ():    for i in range (1, int (max) + 1):        print ("fizzbuzz" if i % 15 == 0 else\                   "buzz" if i %  5 == 0 else\               "fizz"     if i %  3 == 0 else\                i)

R语言

#!/usr/local/bin/Rscriptstdin <- file ('stdin', 'r')repeat {    max <- readLines (stdin, n = 1)    if (length (max) == 0) {        break    }    max = as.integer (max)    for (i in 1 : max) {        if (i %% 15 == 0) {            cat ("fizzbuzz\n")            next        }        if (i %%  5 == 0) {            cat (    "buzz\n")            next        }        if (i %%  3 == 0) {            cat ("fizz\n"    )            next        }        cat (i, "\n")    }}

Ruby

ARGF . each_line do |_|    for i in 1 .. _ . to_i do        puts i % 15 == 0 ? "fizzbuzz"           : i %  5 == 0 ?     "buzz"           : i %  3 == 0 ? "fizz"           : i    endend

Awk

#!/usr/bin/awk{    for (i = 1; i <= $0; i ++) {        print (i % 15 == 0) ? "fizzbuzz"  \            : (i %  5 == 0) ? "buzz"      \            : (i %  3 == 0) ? "fizz"      \            :                  i    }}

Bash

#!/bin/shwhile read maxdo  for ((i = 1; i <= $max; i ++))    do        out=$i        if (($i %  3 == 0))        then out="fizz"        fi        if (($i %  5 == 0))        then out="buzz"        fi        if (($i % 15 == 0))        then out="fizzbuzz"        fi        echo $out    donedone

标签: #atoipython