#include<iostream>
使用 namespace std;
int main(){
int i=1;
do{
if(i <10){
cout<<"00"<<i<<"。
}
else if(i <100){
cout<<"0"<<i<<" "。
}
else {
cout<<i<<" ";
}
if(i%10 ==0) {
cout<<endl;
}
i ;
}while (i <=100)。
return 0;
這就是輸出
001 002 003 004 005 006 007 008 009 010
011 012 013 014 015 016 017 018 019 020
021 022 023 024 025 026 027 028 029 030
031 032 033 034 035 036 037 038 039 040
041 042 043 044 045 046 047 048 049 050
051 052 053 054 055 056 057 058 059 060
061 062 063 064 065 066 067 068 069 070
071 072 073 074 075 076 077 078 079 080
081 082 083 084 085 086 087 088 089 090 >。
091 092 093 094 095 096 097 098 099 100
這是我想要的輸出:
這是我想要的輸出。
001
011 012
021 022 023
031 032 033 034
041 042 043 044 045
051 052 053 054 055 056
061 062 063 064 065 066 067
071 072 073 074 075 076 077 078
081 082 083 084 085 086 087 088 089 >。
091 092 093 094 095 096 097 098 099 100
uj5u.com熱心網友回復:
你可以在嵌套回圈的幫助下做到這一點:
int main() {
for (int i = 0; i < 10; i ) {
for (int j = 1; j <= i 1; j )
std::cout << std::setw(3) << std:: setfill('0') << i * 10 j << " ;
std::cout << std::endl;
}
uj5u.com熱心網友回復:
當查看預期的輸出時,我看到兩種模式:
001
011
021 021
031 031
041 041
051 041
061 061
071 071
081 081
091 091
這可以由
產生for(int y =0; y < 10; y) {
int yval = y * 10 1;
//見下文。
}
實際要列印的列值似乎遵循這樣的模式
for(int x = 0; x <= y; x) {
int xval = yval x;
// print xval.
}
現在,只需要將這兩者結合起來,并正確格式化輸出即可。
CodePudding
你已經知道如何列印單個數字,只是大的圖案是錯誤的。由于圖案是二維的,當這種二維的方面也出現在你的代碼中時,你的代碼會更容易。我的意思是。你可以迭代i,而不是迭代row和column:
for (int row=0; row<10; row) {
for (int col=0; col<10; col) {
if ( ...... ) {
print_number(row,col)。
}
}
std::cout << "
"。
}
在每一行之后都會列印一個換行符,你只需要為if找出合適的條件,并實作一個print_number函式(你已經有了該函式所需的大部分內容)。
為了獲得獎勵,你可以閱讀break來改善上述回圈的邏輯。或者調整內回圈的邊界,使你可以洗掉if。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/331858.html
標籤:
