常見復(fù)雜度對應(yīng)數(shù)據(jù)量表
例題2.1 abc
#includeusing namespace std;
int main()
{int a, b, c;
for(a=0;a<9;a++)
for(b=0;b<9;b++)
for (c = 0; c< 9; c++)
{ if ((a * 100 + b * 10 + c + b * 100 + c * 10 + c) == 532)
cout<< a<< " "<< b<< " "<< c<< endl;
}
return 0;
}
例題2.2 反序數(shù)
#includeusing namespace std;
int reverse(int number)
{int reversenumber = 0;
while (number != 0)
{reversenumber *= 10;
reversenumber += number % 10;
number /= 10;
}
return reversenumber;
}
int main()
{for (int i = 1000; i< 9999; i++)
if (i * 9 == reverse(i)) { cout<< i<< endl;
}
return 0;
}
例題2.3 對稱平方數(shù)
#includeusing namespace std;
int Reverse(int number) {int reverseNumber = 0;
while (number != 0) {reverseNumber *= 10;
reverseNumber += number % 10;
number /= 10;
}
return reverseNumber;
}
int main() {for (int i = 1; i< 256; i++) {if (i * i == Reverse(i * i)) {cout<< i<< endl;
}
}
return 0;
}
2.圖形排版例題2.4 輸出梯形
#includeusing namespace std;
int main()
{int h; //梯形行數(shù)
while (cin >>h)
{int row = h;
int col = h + 2 * (h - 1); //推導(dǎo)公式得出列數(shù)
for (int i = 0; i< row; ++i)
{ for (int j = 0; j< col; ++j)
{ if (j< (col - (h + 2 * i))) //推導(dǎo)公式畫出空格數(shù)
{cout<< " ";
}
else
{cout<< "*";
}
}
cout<< endl;
}
}
return 0;
}
3.日期問題例題2.5 今年的第幾天
#includeusing namespace std;
int dayTable[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
bool IsLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {int Y, M, D;
while (cin >>Y >>M >>D) {int number = 0;
int row = IsLeapYear(Y);
for (int j = 0; j< M; j++) {number += dayTable[row][j];
}
number += D;
cout<< number<< endl;
}
return 0;
}
例題2.6 打印日期
#includeusing namespace std;
int dayTable[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
bool IsLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {int Y, M, D;
int number = 0;
while (cin >>Y >>number) {M = 0;
int row = IsLeapYear(Y);
while (number >dayTable[row][M]) {number -= dayTable[row][M];
M++;
}
D = number;
printf("%04d-%02d-%02d\n", Y, M, D);
}
return 0;
}
4.其他模擬例題2.7 手機(jī)鍵盤
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧