0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニングレベルアップ問題集の【文字列】をやってみた。

Posted at

paizaラーニングレベルアップ問題集のCランクレベルアップメニュー【文字列】をやってみました。


問題
整数と文字列

部分文字列

数字の文字列操作(基本)

数字の文字列操作(0埋め)

数字の文字列操作(時刻1)

数字の文字列操作(時刻2)

文字列


最終問題で、時刻$H$時$M$分の$h$時間$m$分後の時刻は、以下のようにして求めることができます。(Rubyと大文字・小文字が逆ですが)

  • $M$に$m$を足します
  • $H$に$h$と$\lfloor\frac{M}{60}\rfloor$を足します
  • $H$を$24$で割った余りを$H$に代入します
  • $M$を$60$で割った余りを$M$に代入します

その他、$T=60\times\left(H+h\right)+M+m$を

  • $60$で割った商を$24$で割った余り、または$1440$で割った余りを$60$で割った商が「時」の答え
  • $60$で割った余りが「分」の答え

という方法もあります。


C
整数と文字列
#include <stdio.h>
#include <string.h>

int main() {
	int n;
	scanf("%d", &n);
	while (n--) {
		char a[6];
		scanf("%s", a);
		printf("%d\n", (int) strlen(a));
	}
	return 0;
}
部分文字列
#include <stdio.h>
#include <string.h>

int main() {
	char a = getchar();
	getchar();
	char s[12];
	fgets(s, sizeof(s), stdin);
	puts(strchr(s, a) ? "YES" : "NO");
	return 0;
}
数字の文字列操作(基本)
#include <stdio.h>

int main() {
	char s[5];
	scanf("%s", s);
	int a = (s[0] - '0') + (s[3] - '0');
	int b = (s[1] - '0') + (s[2] - '0');
	printf("%d%d\n", a, b);
	return 0;
}
数字の文字列操作(0埋め)
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	printf("%03d\n", n);
	return 0;
}
数字の文字列操作(時刻1)
#include <stdio.h>

int main() {
	int h, m;
	scanf("%d:%d", &h, &m);
	printf("%d\n", h);
	printf("%d\n", m);
	return 0;
}
数字の文字列操作(時刻2)
#include <stdio.h>

int main() {
	int h, m;
	scanf("%d:%d", &h, &m);
	m += 30;
	h += m / 60;
	m %= 60;
	printf("%02d:%02d\n", h, m);
	return 0;
}
文字列
#include <stdio.h>

int main() {
	int n;
	scanf("%d", &n);
	while (n--) {
		char t[6];
		int h, m;
		scanf("%s %d %d", t, &h, &m);
		int hh, mm;
		sscanf(t, "%d:%d", &hh, &mm);
		mm += m;
		hh += h + mm / 60;
		hh %= 24;
		mm %= 60;
		printf("%02d:%02d\n", hh, mm);
	}
	return 0;
}

C++
整数と文字列
#include <iostream>
using namespace std;

int main() {
	int n;
	cin >> n;
	while (n--) {
		string a;
		cin >> a;
		cout << a.size() << endl;
	}
	return 0;
}
部分文字列
#include <iostream>
using namespace std;

int main(void){
	char a;
	cin >> a;
	string s;
	cin >> s;
	cout << (s.find(a) == string::npos ? "NO" : "YES") << endl;
}

別解

#include <iostream>
using namespace std;

int main(void){
	char a;
	cin >> a;
	string s;
	cin >> s;
	cout << (find(s.begin(), s.end(), a) == s.end() ? "NO" : "YES") << endl;
}
数字の文字列操作(基本)
#include <iostream>
using namespace std;

int main() {
	string s;
	cin >> s;
	int a = (s[0] - '0') + (s[3] - '0');
	int b = (s[1] - '0') + (s[2] - '0');
	cout << a << b << endl;
	return 0;
}
数字の文字列操作(0埋め)
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int n;
	cin >> n;
	cout << setw(3) << setfill('0') << n << endl;
	return 0;
}
数字の文字列操作(時刻1)
#include <iostream>
using namespace std;

int main() {
	int h, m;
	char _;
	cin >> h >> _ >> m;
	cout << h << endl;
	cout << m << endl;
	return 0;
}
数字の文字列操作(時刻2)
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int h, m;
	char _;
	cin >> h >> _ >> m;
	m += 30;
	h += m / 60;
	m %= 60;
	cout << setw(2) << setfill('0') << h << ':' << setw(2) << setfill('0') << m << endl;
	return 0;
}
文字列
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	int n;
	cin >> n;
	while (n--) {
		int hh, mm, h, m;
		char _;
		cin >> hh >> _ >> mm >> h >> m;
		mm += m;
		hh += h + mm / 60;
		hh %= 24;
		mm %= 60;
		cout << setw(2) << setfill('0') << hh << ':' << setw(2) << setfill('0') << mm << endl;
	}
	return 0;
}

C#
整数と文字列
using System;

class Program
{
	public static void Main()
	{
		for (int n = int.Parse(Console.ReadLine()); n > 0; n--) {
			Console.WriteLine(Console.ReadLine().Length);
		}
	}
}
部分文字列
using System;

class Program
{
	public static void Main()
	{
		char a = Console.ReadLine()[0];
		string s = Console.ReadLine();
		Console.WriteLine(s.IndexOf(a) == -1 ? "NO" : "YES");
	}
}
数字の文字列操作(基本)
using System;

class Program
{
	public static void Main()
	{
		string s = Console.ReadLine();
		int a = (s[0] - '0') + (s[3] - '0');
		int b = (s[1] - '0') + (s[2] - '0');
		Console.WriteLine("" + a + b);
	}
}
数字の文字列操作(0埋め)
using System;

class Program
{
	public static void Main()
	{
		Console.WriteLine(int.Parse(Console.ReadLine()).ToString("D3"));
	}
}
数字の文字列操作(時刻1)
using System;

class Program
{
	public static void Main()
	{
		string t = Console.ReadLine();
		int[] hm = Array.ConvertAll(t.Split(":"), int.Parse);
		int h = hm[0];
		int m = hm[1];
		Console.WriteLine(h);
		Console.WriteLine(m);
	}
}
数字の文字列操作(時刻2)
using System;

class Program
{
	public static void Main()
	{
		string t = Console.ReadLine();
		int[] hm = Array.ConvertAll(t.Split(":"), int.Parse);
		int m = hm[1] + 30;
		int h = hm[0] + m / 60;
		m %= 60;
		Console.WriteLine($"{h:00}:{m:00}");
	}
}
文字列
using System;

class Program
{
	public static void Main()
	{
		for (int n = int.Parse(Console.ReadLine()); n > 0; n--) {
			string[] thm = Console.ReadLine().Split();
			int[] hhmm = Array.ConvertAll(thm[0].Split(":"), int.Parse);
			int hh = hhmm[0];
			int mm = hhmm[1];
			int h = int.Parse(thm[1]);
			int m = int.Parse(thm[2]);
			mm += m;
			hh += h + mm / 60;
			hh %= 24;
			mm %= 60;
			Console.WriteLine($"{hh:00}:{mm:00}");
		}
	}
}

Go
整数と文字列
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var a string
		fmt.Scan(&a)
		fmt.Println(len(a))
	}
}
部分文字列
package main

import (
	"fmt"
	"strings"
)

func main() {
	var a rune
	fmt.Scanf("%c\n", &a)
	var s string
	fmt.Scan(&s)
	if strings.IndexRune(s, a) == -1 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}
数字の文字列操作(基本)
package main
import "fmt"

func main() {
	var s string
	fmt.Scan(&s)
	a := (s[0] - '0') + (s[3] - '0')
	b := (s[1] - '0') + (s[2] - '0')
	fmt.Printf("%d%d\n", a, b)
}
数字の文字列操作(0埋め)
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	fmt.Printf("%03d\n", n)
}
数字の文字列操作(時刻1)
package main
import "fmt"

func main() {
	var h, m int
	fmt.Scanf("%d:%d", &h, &m)
	fmt.Println(h)
	fmt.Println(m)
}
数字の文字列操作(時刻2)
package main
import "fmt"

func main() {
	var h, m int
	fmt.Scanf("%d:%d", &h, &m)
	m += 30
	h += m / 60
	m %= 60
	fmt.Printf("%02d:%02d", h, m)
}
文字列
package main
import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		var hh, mm, h, m int
		fmt.Scanf("%d:%d %d %d", &hh, &mm, &h, &m)
		mm += m
		hh += h + mm / 60
		hh %= 24
		mm %= 60
		fmt.Printf("%02d:%02d\n", hh, mm)
	}
}

Java
整数と文字列
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for (int n = sc.nextInt(); n > 0; n--)
			System.out.println(sc.next().length());
		sc.close();
	}
}
部分文字列
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		char a = sc.nextLine().charAt(0);
		String s = sc.nextLine();
		sc.close();
		System.out.println(s.indexOf(a) == -1 ? "NO" : "YES");
	}
}
数字の文字列操作(基本)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		sc.close();
		int a = (int) (s.charAt(0) - '0') + (int) (s.charAt(3) - '0');
		int b = (int) (s.charAt(1) - '0') + (int) (s.charAt(2) - '0');
		System.out.printf("%d%d%n", a, b);
	}
}
数字の文字列操作(0埋め)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.printf("%03d%n", sc.nextInt());
		sc.close();
	}
}
数字の文字列操作(時刻1)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] hm = Arrays.stream(sc.nextLine().split(":")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int h = hm[0];
		int m = hm[1];
		System.out.println(h);
		System.out.println(m);
	}
}
数字の文字列操作(時刻2)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int[] hm = Arrays.stream(sc.nextLine().split(":")).mapToInt(Integer::parseInt).toArray();
		sc.close();
		int m = hm[1];
		int h = hm[0] + m / 60;
		m %= 60;
		System.out.printf("%02d:%02d%n", h, m);
	}
}
文字列
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for (int n = sc.nextInt(); n > 0; n--) {
			String t = sc.next();
			int[] hhmm = Arrays.stream(t.split(":")).mapToInt(Integer::parseInt).toArray();
			int hh = hhmm[0];
			int mm = hhmm[1];
			int h = sc.nextInt();
			int m = sc.nextInt();
			mm += m;
			hh += h + mm / 60;
			hh %= 24;
			mm %= 60;
			System.out.printf("%02d:%02d%n", hh, mm);
		}
		sc.close();
	}
}

JavaScript
整数と文字列
const [N, ...A] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
for (var i = 0; i < N; i++) {
	console.log(A[i].length);
}
部分文字列
const [a, s] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
console.log(s.indexOf(a[0]) === -1 ? "NO" : "YES");
数字の文字列操作(基本)
const s = require("fs").readFileSync("/dev/stdin", "utf8");
const a = (s[0] - '0') + (s[3] - '0');
const b = (s[1] - '0') + (s[2] - '0');
console.log("" + a + b);
数字の文字列操作(0埋め)
console.log(require("fs").readFileSync("/dev/stdin", "utf8").trim().padStart(3, '0'));
数字の文字列操作(時刻1)
const [h, m] = require("fs").readFileSync("/dev/stdin", "utf8").split(':').map(Number);
console.log(h);
console.log(m);
数字の文字列操作(時刻2)
let [h, m] = require("fs").readFileSync("/dev/stdin", "utf8").split(':').map(Number);
m += 30;
h += Math.floor(m / 60);
m %= 60;
console.log(String(h).padStart(2, '0') + ':' + String(m).padStart(2, '0'));
文字列
const [N, ...THM] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split('\n');
const n = Number(N);
for (var i = 0; i < n; i++) {
	const [t, h, m] = THM[i].split(' ');
	let [hh, mm] = t.split(':').map(Number);
	mm += Number(m);
	hh += Number(h) + Math.floor(mm / 60);
	hh %= 24;
	mm %= 60;
	console.log(String(hh).padStart(2, '0') + ':' + String(mm).padStart(2, '0'));
}

Kotlin
整数と文字列
fun main() {
	repeat (readLine()!!.toInt()) {
		println(readLine()!!.length)
	}
}
部分文字列
fun main() {
	val a = readLine()!![0]
	val s = readLine()!!
	println(if (s.indexOf(a) == -1) "NO" else "YES")
}
数字の文字列操作(基本)
fun main() {
	val s = readLine()!!
	val a = (s[0] - '0') + (s[3] - '0')
	val b = (s[1] - '0') + (s[2] - '0')
	println("" + a + b)
}
数字の文字列操作(0埋め)
fun main() {
	println("%03d".format(readLine()!!.toInt()))
}
数字の文字列操作(時刻1)
fun main() {
	val (h, m) = readLine()!!.split(':').map { it.toInt() }
	println(h)
	println(m)
}
数字の文字列操作(時刻2)
fun main() {
	var (h, m) = readLine()!!.split(':').map { it.toInt() }
	m += 30
	h += m / 60
	m %= 60
	println("%02d:%02d".format(h, m))
}
文字列
fun main() {
	repeat (readLine()!!.toInt()) {
		val (t, h, m) = readLine()!!.split(' ')
		var (hh, mm) = t.split(':').map { it.toInt() }
		mm += m.toInt()
		hh += h.toInt() + mm / 60
		hh %= 24
		mm %= 60
		println("%02d:%02d".format(hh, mm))
	}
}

PHP
整数と文字列
<?php
	for ($n = intval(fgets(STDIN)); $n; $n--)
		echo strlen(trim(fgets(STDIN))), PHP_EOL;
?>
部分文字列
<?php
	$a = fgets(STDIN)[0];
	$s = trim(fgets(STDIN));
	echo strpos($s, $a) === false ? "NO" : "YES", PHP_EOL;
?>
数字の文字列操作(基本)
<?php
	$s = trim(fgets(STDIN));
	$a = ($s[0] - '0') + ($s[3] - '0');
	$b = ($s[1] - '0') + ($s[2] - '0');
	echo "$a$b", PHP_EOL;
?>
数字の文字列操作(0埋め)
<?php
	printf("%03d\n", intval(fgets(STDIN)));
?>
数字の文字列操作(時刻1)
<?php
	[$h, $m] = array_map("intval", explode(':', fgets(STDIN)));
	echo $h, PHP_EOL;
	echo $m, PHP_EOL;
?>
数字の文字列操作(時刻2)
<?php
	[$h, $m] = array_map("intval", explode(':', fgets(STDIN)));
	$m += 30;
	$h += $m / 60;
	$m %= 60;
	printf("%02d:%02d%s", $h, $m, PHP_EOL);
?>
文字列
<?php
	$n = intval(fgets(STDIN));
	while ($n--) {
		[$t, $h, $m] = explode(' ', fgets(STDIN));
		[$hh, $mm] = array_map("intval", explode(':', $t));
		$mm += intval($m);
		$hh += $h + $mm / 60;
		$hh %= 24;
		$mm %= 60;
		printf("%02d:%02d%s", $hh, $mm, PHP_EOL);
	}
?>

Perl
整数と文字列
for (1..int(<STDIN>)) {
	chomp(my $a = <STDIN>);
	print length($a), $/;
}
部分文字列
chomp(my $a = <STDIN>);
chomp(my $s = <STDIN>);
print index($s, $a) == -1 ? "NO$/" : "YES$/";
数字の文字列操作(基本)
my @s = split '', <STDIN>;
my $a = ($s[0] - '0') + ($s[3] - '0');
my $b = ($s[1] - '0') + ($s[2] - '0');
print "$a$b$/";
数字の文字列操作(0埋め)
print sprintf("%03d$/", int(<STDIN>));
数字の文字列操作(時刻1)
my ($h, $m) = map { int($_) } split ':', <STDIN>;
print "$h$/";
print "$m$/";
数字の文字列操作(時刻2)
my ($h, $m) = map { int($_) } split ':', <STDIN>;
$m += 30;
$h += $m / 60;
$m %= 60;
print sprintf("%02d:%02d$/", $h, $m);
文字列
for (1..int(<STDIN>)){
	my ($t, $h, $m) = split ' ', <STDIN>;
	my ($hh, $mm) = map { int($_) } split ':', $t;
	$mm += $m;
	$hh += $h + $mm / 60;
	$hh %= 24;
	$mm %= 60;
	print sprintf("%02d:%02d$/", $hh, $mm);
}

Python3
整数と文字列
for _ in range(int(input())):
	print(len(input()))
部分文字列
a = input()[0]
s = input()
print("YES" if a in s else "NO")
数字の文字列操作(基本)
s = input()
a = int(s[0]) + int(s[3])
b = int(s[1]) + int(s[2])
print(str(a) + str(b))
数字の文字列操作(0埋め)
print("%03d" % int(input()))
print("{:03d}".format(int(input())))
print(f"{int(input()):03d}")
数字の文字列操作(時刻1)
h, m = map(int, input().split(':'))
print(h)
print(m)
数字の文字列操作(時刻2)
h, m = map(int, input().split(':'))
m += 30
h += m // 60
m %= 60
print("%02d:%02d" % (h, m))
文字列
for _ in range(int(input())):
	t, h, m = input().split(' ')
	hh, mm = map(int, t.split(':'))
	mm += int(m)
	hh += int(h) + mm // 60
	hh %= 24
	mm %= 60
	print("{:02d}:{:02d}".format(hh, mm))

Ruby
整数と文字列
gets.to_i.times do
	p gets.chomp.size
end
部分文字列
a = gets[0]
s = gets.chomp
print s.index(a) ? "YES" : "NO"
数字の文字列操作(基本)
s = gets
a = s[0].to_i + s[3].to_i
b = s[1].to_i + s[2].to_i
print a, b, "\n"
数字の文字列操作(0埋め)
puts "%03d" % gets.to_i
数字の文字列操作(時刻1)
p *gets.split(':').map(&:to_i)
数字の文字列操作(時刻2)
h, m = gets.split(':').map(&:to_i)
m += 30
h += m / 60
m %= 60
puts "%02d:%02d" % [h, m]
文字列
gets.to_i.times do
	t, h, m = gets.split(' ')
	hh, mm = t.split(':').map(&:to_i)
	mm += m.to_i
	hh += h.to_i + mm / 60
	hh %= 24
	mm %= 60
	puts "%02d:%02d" % [hh, mm]
end

Scala
整数と文字列
import scala.io.StdIn._

object Main extends App{
	for (_ <- 0 until readInt()) {
		println(readLine().length)
	}
}
部分文字列
import scala.io.StdIn._

object Main extends App{
	val a = readLine()(0)
	val s = readLine()
	println(if (s.indexOf(a) == -1) "NO" else "YES")
}
数字の文字列操作(基本)
import scala.io.StdIn._

object Main extends App{
	val s = readLine()
	val a = (s(0) - '0') + (s(3) - '0')
	val b = (s(1) - '0') + (s(2) - '0')
	println("" + a + b)
}
数字の文字列操作(0埋め)
import scala.io.StdIn._

object Main extends App{
	printf("%03d\n", readInt())
}
数字の文字列操作(時刻1)
import scala.io.StdIn._

object Main extends App{
	val Array(h, m) = readLine().split(':').map { _.toInt }
	println(h)
	println(m)
}
数字の文字列操作(時刻2)
import scala.io.StdIn._

object Main extends App{
	var Array(h, m) = readLine().split(':').map { _.toInt }
	m += 30
	h += m / 60
	m %= 60
	printf("%02d:%02d\n", h, m)
}
文字列
import scala.io.StdIn._

object Main extends App{
	for (i <- 0 until readInt()) {
		val Array(t, h, m) = readLine().split(' ')
		var Array(hh, mm) = t.split(':').map { _.toInt }
		mm += m.toInt
		hh += h.toInt + mm / 60
		hh %= 24
		mm %= 60
		printf("%02d:%02d\n", hh, mm)
	}
}

Swift
整数と文字列
for _ in 0..<Int(readLine()!)! {
	print(readLine()!.count)
}
部分文字列
import Foundation

let a = readLine()!
let s = readLine()!
print(s.contains(a) ? "YES" : "NO")
数字の文字列操作(基本)
let s = Array(readLine()!)
let a = Int(String(s[0]))! + Int(String(s[3]))!
let b = Int(String(s[1]))! + Int(String(s[2]))!
print(String(a) + String(b))
数字の文字列操作(0埋め)
import Foundation

print(String(format: "%03d", Int(readLine()!)!))
数字の文字列操作(時刻1)
let hm = readLine()!.split(separator: ":").compactMap { Int($0) }
print(hm[0])
print(hm[1])
数字の文字列操作(時刻2)
import Foundation

let hm = readLine()!.split(separator: ":").compactMap { Int($0) }
var m = hm[1] + 30
var h = hm[0] + m / 60
m %= 60
print(String(format: "%02d:%02d", h, m))
文字列
import Foundation

for _ in 0..<Int(readLine()!)! {
	let thm = readLine()!.split(separator: " ")
	let hm = thm[0].split(separator: ":").compactMap { Int($0) }
	var mm = Int(thm[2])! + hm[1]
	var hh = Int(thm[1])! + hm[0] + mm / 60
	hh %= 24
	mm %= 60
	print(String(format: "%02d:%02d", hh, mm))
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?