Príklady na tlač polovičnej pyramídy, pyramídy, obrátenej pyramídy, Pascalovho trojuholníka a Floydovho trojuholníka v programovaní v C ++ pomocou riadiacich príkazov.
Aby ste pochopili tento príklad, mali by ste mať znalosti nasledujúcich programovacích tém C ++:
- C ++ ak, ak … inak a Vnorené, ak … inak
- C ++ pre slučku
- C ++ zatiaľ čo a robiť … zatiaľ čo Loop
- Príkaz C ++ na prerušenie a pokračovanie
Zoznam zdrojových kódov |
---|
Vytlačte trojuholník pomocou *, číslic a znakov |
Vytlačte obrátený trojuholník pomocou * a číslice |
Kód na tlač pyramídy |
Kód na tlač reverznej pyramídy |
Kód na vytlačenie značky Pascal |
Kód na tlač Floydovho trojuholníka |
Programy na tlač trojuholníkov pomocou znakov *, čísel a znakov
Príklad 1: Program na tlač polovičnej pyramídy pomocou *
* * * * * * * * * * * * * * * *
Zdrojový kód
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = 1; i <= rows; ++i) ( for(int j = 1; j <= i; ++j) ( cout << "* "; ) cout << ""; ) return 0; )
Príklad 2: Program na tlač polovičnej pyramídy a pomocou čísel
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Zdrojový kód
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = 1; i <= rows; ++i) ( for(int j = 1; j <= i; ++j) ( cout << j << " "; ) cout << ""; ) return 0; )
Príklad 3: Program na tlač polovičnej pyramídy pomocou abecedy
ABBCCCDDDDEEEEE
Zdrojový kód
#include using namespace std; int main() ( char input, alphabet = 'A'; cout <> input; for(int i = 1; i <= (input-'A'+1); ++i) ( for(int j = 1; j <= i; ++j) ( cout << alphabet << " "; ) ++alphabet; cout << endl; ) return 0; )
Programy na tlač obrátenej polovičnej pyramídy pomocou * a čísel
Príklad 4: Obrátená polovičná pyramída pomocou *
* * * * * * * * * * * * * * * *
Zdrojový kód
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = rows; i>= 1; --i) ( for(int j = 1; j <= i; ++j) ( cout << "* "; ) cout << endl; ) return 0; )
Príklad 5: Obrátená polovičná pyramída pomocou čísel
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Zdrojový kód
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = rows; i>= 1; --i) ( for(int j = 1; j <= i; ++j) ( cout << j << " "; ) cout << endl; ) return 0; )
Programy na zobrazovanie pyramídy a obrátenej pyramídy pomocou znakov * a číslic
Príklad 6: Program na tlač celej pyramídy pomocou *
* * * * * * * * * * * * * * * * * * * * * * * * * *
Zdrojový kód
#include using namespace std; int main() ( int space, rows; cout <> rows; for(int i = 1, k = 0; i <= rows; ++i, k = 0) ( for(space = 1; space <= rows-i; ++space) ( cout <<" "; ) while(k != 2*i-1) ( cout << "* "; ++k; ) cout << endl; ) return 0; )
Príklad 7: Program na tlač pyramídy pomocou čísel
1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5
Zdrojový kód
#include using namespace std; int main() ( int rows, count = 0, count1 = 0, k = 0; cout <> rows; for(int i = 1; i <= rows; ++i) ( for(int space = 1; space <= rows-i; ++space) ( cout << " "; ++count; ) while(k != 2*i-1) ( if (count <= rows-1) ( cout << i+k << " "; ++count; ) else ( ++count1; cout << i+k-2*count1 << " "; ) ++k; ) count1 = count = k = 0; cout << endl; ) return 0; )
Príklad 8: Obrátená plná pyramída pomocou *
* * * * * * * * * * * * * * * * * * * * * * * * * *
Zdrojový kód
#include using namespace std; int main() ( int rows; cout <> rows; for(int i = rows; i>= 1; --i) ( for(int space = 0; space < rows-i; ++space) cout << " "; for(int j = i; j <= 2*i-1; ++j) cout << "* "; for(int j = 0; j < i-1; ++j) cout << "* "; cout << endl; ) return 0; )
Príklad 9: Vytlačte Pascalov trojuholník
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Zdrojový kód
#include using namespace std; int main() ( int rows, coef = 1; cout <> rows; for(int i = 0; i < rows; i++) ( for(int space = 1; space <= rows-i; space++) cout <<" "; for(int j = 0; j <= i; j++) ( if (j == 0 || i == 0) coef = 1; else coef = coef*(i-j+1)/j; cout << coef << " "; ) cout << endl; ) return 0; )
Príklad 10: Vytlačte Floydov trojuholník.
1 2 3 4 5 6 7 8 9 10
Zdrojový kód
#include using namespace std; int main() ( int rows, number = 1; cout <> rows; for(int i = 1; i <= rows; i++) ( for(int j = 1; j <= i; ++j) ( cout << number << " "; ++number; ) cout << endl; ) return 0; )