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 |
~此题打表法才能过 先开始直接法思路清晰但是超时 代码如下 #include <iostream> using namespace std; int main() { //freopen ("in.txt", "r", stdin); int i,j,sum=0,m,result=0; while(scanf("%d" , &m) != 0) { sum=0,result=0; for(i = 1; i<m; i ++) { sum = sum + i; if(sum < m) result = i*i + result; if(sum == m ) { result = i*i + result; printf("%d %d\n" , m ,result); break; } if(sum > m ) { result = result + (m + i - sum)*i; printf("%d %d\n" , m ,result); break; } } } return 0; } 打表法#include <iostream> using namespace std; int main() { //freopen ("in.txt", "r", stdin); int i,j,sum=0,m,result=0,a[160][3]; for(i = 1; i<= 160; i ++) { sum = sum + i; a[i][1] = sum; result = result + i * i; a[i][2] = result; } while(scanf("%d" , &m) && m != 0) { for(i = 1; i<= 160; i ++) { if(m == a[i][1]) { printf("%d %d\n" , m , a[i][2]); break; } if(a[i][1] < m && m < a[i+1][1]) { printf("%d %d\n" , m , a[i][2] + (m - a[i][1]) * (i+1)); break; } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator