Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
关键在于理解Ugly Numbers首先,1一定是Ugly Numbers; 其次,2是由1*2所得,也是; 3是由1*3所得,也是; 4是由2*2所得,也是; 5是由1*5所得,也是; 6是由2*3或者3*2所得,也是; 8是由4*2所得,也是; 10是由5*2所得,也是; 12是由6*2所得,也是; 但14不是Ugly Numbers;因为前面的数字没有7; 所以我以下的代码不对: #include "stdio.h" #define MAX 9999 int a[MAX]; void Ugly() { int i, j; j = 1; for (i = 1; i <= MAX; i++) { if (i == 1) { a[j] = 1; j++; } if (i % 2 == 0 || i % 3 == 0 || i % 5 == 0) { a[j] = i; j++; } } } int main() { //freopen("1.txt", "r", stdin); int n; Ugly(); while (scanf("%d", &n) != EOF) { if (n == 0) { break; } printf("%d\n", a[n]); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator