Di sini, tugas kita adalah membagi string S menjadi n bagian yang sama. Kami akan mencetak pesan kesalahan jika string tidak dapat dibagi menjadi n bagian yang sama jika tidak semua bagian harus dicetak sebagai output dari program
Untuk memeriksa apakah string dapat dibagi menjadi N bagian yang sama, kita perlu membagi panjang string dengan n dan menetapkan hasilnya ke variabel karakter.
Jika char keluar menjadi nilai floating point, kita tidak dapat membagi string jika tidak menjalankan for loop untuk melintasi string dan membagi string pada setiap interval karakter
Dapatkan angka positif dari pengguna dan cetak setiap digit angka
Contoh 1
bilangan = 12
keluaran
2
1
Contoh 2
bilangan = 123
keluaran
3
2
1
Mari kita merancang sebuah logika
bilangan = 12
Pertama mari kita pikirkan, bagaimana cara membagi digit terakhir 2 dari angka 12
Karena angkanya desimal [basis 10], rentang setiap digit harus 0 hingga 9
Jadi, kita bisa mendapatkannya dengan mudah dengan melakukan modulo 10
Menyukai,
12 % 10 => 2 [ kita pisahkan angka 2 dari angka 12]
Sekarang, kita harus membagi angka 1 dari angka 12
Ini dapat dicapai dengan membagi angka dengan 10 dan mengambil modulo 10
Menyukai,
12/10 => 1
Sekarang ambil modulo 10
1% 10 = 1
Dengan menggunakan metode di atas, kita dapat memisahkan setiap digit dari sebuah angka
Logika
Hingga angka menjadi 0
Mengerjakan
Ambil angka % 10 dan cetak hasilnya
Bagilah angkanya dengan 10. [angka = angka / 10]
Contoh
Ambil num = 123
Langkah 1
Bilangan = 123 [tidak sama dengan nol]
Mod = 123 % 10 ==> 3
Bilangan = 123 / 10 ==> 12
Langkah 2
Bilangan = 12 [tidak sama dengan 0]
Mod = 12 % 10 ==> 2
Bilangan = 12 / 10 ==> 1
Langkah 3
Bilangan = 1 [tidak sama dengan 0]
Mod = 1 % 10 ==> 1
Bilangan = 1/10 ==> 0
Langkah 4
Num = 0 [sama dengan nol, hentikan proses]
Penjelasan Bergambar
Program
Contoh
/***************************** *Program : split the digits * *Language : C * *****************************/ #include int main[] { int num; scanf["%d",&num]; while[num > 0] //do till num greater than 0 { int mod = num % 10; //split last digit from number printf["%d\n",mod]; //print the digit. num = num / 10; //divide num by 10. num /= 10 also a valid one } return 0; }
Menjalankannya
Topik yang Mungkin Anda Suka
// represent a number n as sum of four.
3// represent a number n as sum of four.
4
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]82
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]83
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]64
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]83
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]70
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]83
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]76________1______45
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_1_______45
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_92
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]44
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]83
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]45
// Returns count of ways
4countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
// represent a number n as sum of four.
9 countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]44
5_00
// represent a number n as sum of four.
4
502
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]45
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]45
505
506
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
508
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]45
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]30
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
512
513
5_14
C#
5_15
5_16
5_17
using
519
using
_9
521
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_4
58
// Returns count of ways
namespace
3
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]0
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]1
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]0
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]3
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_4
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]0
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]7
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]8
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
536
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
538
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
540
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
54
55
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]0
57
58
54
55
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]0
// A Simple C++ program to count number of ways to
2// A Simple C++ program to count number of ways to
3
54
55
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]0 ________135______7
// A Simple C++ program to count number of ways to
8
54
55
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]0 ________126______2
// represent a number n as sum of four.
3// represent a number n as sum of four.
4 // represent a number n as sum of four.
5
// represent a number n as sum of four.
3// represent a number n as sum of four.
7
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
// represent a number n as sum of four.
9 #include
0#include
_1
5_70
namespace
3
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]23
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]25
574
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_4
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]0
#include
8countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
580
#include
_1
#include
_1
5_83
PHP
5_84
5_85
5_86
5_87
// Returns count of ways
589
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]1
591
592
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_4
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]8
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
597
598
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
536
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
// A Simple C++ program to count number of ways to
02countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_5
// A Simple C++ program to count number of ways to
04countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
54
55
// A Simple C++ program to count number of ways to
08 // A Simple C++ program to count number of ways to
09// A Simple C++ program to count number of ways to
08 // A Simple C++ program to count number of ways to
11591
std;
3// A Simple C++ program to count number of ways to
135_____5_____58
54
55
// A Simple C++ program to count number of ways to
19 countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]45
// A Simple C++ program to count number of ways to
08std;
3// A Simple C++ program to count number of ways to
19 // A Simple C++ program to count number of ways to
11591
std;
3// A Simple C++ program to count number of ways to
19// A Simple C++ program to count number of ways to
15// A Simple C++ program to count number of ways to
3
54
55
// A Simple C++ program to count number of ways to
32 countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]45
// A Simple C++ program to count number of ways to
19std;
3____135___________________32 // A Simple C++ program to count number of ways to
______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________// A Simple C++ program to count number of ways to
8
54
55
// A Simple C++ program to count number of ways to
45 countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]45
// A Simple C++ program to count number of ways to
32std;
3____135______________________5135135135_______________________________________________________________________________________________________________________________________________________________________________________________________________________// represent a number n as sum of four.
3// represent a number n as sum of four.
4
55
// A Simple C++ program to count number of ways to
08 countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]83
// A Simple C++ program to count number of ways to
19 countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]__________________________32 ___________________________________________________________________________________________________________________________.
________126
countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]_17_______97
// A Simple C++ program to count number of ways to
70countWays[n, parts, nextPart] = ∑countWays[n, parts, i] nextPart Count of parts of n. Initially parts = 4 nextPart --> Starting point for next part to be tried We try for all values from nextPart to n. We initially call the function as countWays[n, 4, 1]5
// represent a number n as sum of four.
9 55
// Returns count of ways
62#include
41591
#include
43________________________14646______________________________________________________________________________________________________________________________________________________D