Ziffernfolge umdrehen (Schleifen)
Schreiben Sie ein Programm, das von einer gegebenen ganzen Zahl (integer) ihre Umkehrung ausgibt. Aus 2454 wird also 4542. Aus 20010 wird 1002 (= 01002 die neue führende Null wird abgeschnitten).
0 Kommentare
8 Lösung(en)
def umkehrung(zahl):
s=str(zahl)
z=int(s)
s=''
while (z>=1):
s=s+str(z%10)
z = z /10
print s
umkehrung(124234)
umkehrung('123443434')
public class Main {
public static void main(String[] args) {
int num = 1030;
System.out.println( reverse1( num ) );
System.out.println( reverse2( num ) );
}
public static int reverse1( int z ) {
StringBuffer s = new StringBuffer( ).append( z ).reverse();
return Integer.parseInt( s.toString() );
}
public static int reverse2( int z ) {
int ziffer, ret = 0;
while ( z != 0 ) {
ziffer = z % 10;
ret *= 10;
ret += ziffer;
z /= 10;
}
return ret;
}
}
a = raw_input("Zahl = ")[::-1]
while a.startswith("0"):
a = a[1:]
print a
Lösung von: Name nicht veröffentlicht
x = int(input('Zahl: '))
u = 0
while x > 0:
u = u * 10 + x % 10
x = x // 10
print(u)
Lösung von: Claude Vonlanthen (Kantonsschule Olten)
x = str(int(input('Zahl: ')))
print(x[::-1])
Lösung von: Claude Vonlanthen (Kantonsschule Olten)
function reverse(num) {
return parseInt(num.toString().split('').reverse().join(''));
}
console.log(reverse(2454));
console.log(reverse(20010)); // lissalanda@gmx.at
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// NET 6.x | C# 10.x | VS-2022
// Variante 1
static int ReverseInt1(int n) => int.Parse(string.Join("", n.ToString().Reverse()));
// Variante 2
static int ReverseInt2(int n) => Convert.ToInt32(new string(n.ToString().Reverse().ToArray()));
// Variante 3
static int ReverseInt3(int n) {
var m = 0;
while (n > 0) {
m = n % 10 + m * 10;
n /= 10;
}
return m;
}
// Variante 4
static int ReverseInt4(int n) {
var s = n.ToString().ToCharArray();
for (int i = 0; i < s.Length / 2; i++)
(s[i], s[^(i + 1)]) = (s[^(i + 1)], s[i]);
return int.Parse(s);
}
Lösung von: Jens Kelm (@JKooP)
// C++ 14 | VS-2022
#include <iostream>
#include <string>
#include <algorithm>
const auto reverse_int_1(int n) {
auto str{ std::to_string(n) };
std::reverse(str.begin(), str.end());
return std::stoi(str);
}
const auto reverse_int_2(int n) {
auto m{ 0 };
while (n) {
m = n % 10 + m * 10;
n /= 10;
}
return m;
}
const auto reverse_int_3(int n) {
auto str{ std::to_string(n) };
for (size_t i{ 0 }; i < str.length() / 2; ++i)
std::swap(str[i], str[str.length() - i - 1]);
return std::stoi(str);
}
const auto reverse_int_4(int n) {
std::string out{};
auto str{ std::to_string(n) };
for (auto it{ str.rbegin() }; it != str.rend(); ++it) {
out.push_back(*it);
}
return std::stoi(out);
}
const auto reverse_int_5(int n) {
auto str{ std::to_string(n) };
std::string out{str};
std::copy(str.rbegin(), str.rend(), out.begin());
return std::stoi(out);
}
Lösung von: Jens Kelm (@JKooP)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 1 |
Schwierigkeit: | k.A. |
Webcode: | 6s3s-xoyx |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |