标签:can i++ question second std write alt orm any
It is easy to see that for every fraction in the form(k > 0), we can always find two positive integers x and y,x ≥ y, such that:
Now our question is: can you write a program that counts how many such pairs of x and y there are for any given k?
Input contains no more than 100 lines, each giving a value of k (0 < k ≤ 10000).
For each k, output the number of corresponding (x, y) pairs, followed by a sorted list of the values of x and y, as shown in the sample output.
2 12
2 1/2 = 1/6 + 1/3 1/2 = 1/4 + 1/4 8 1/12 = 1/156 + 1/13 1/12 = 1/84 + 1/14 1/12 = 1/60 + 1/15 1/12 = 1/48 + 1/16 1/12 = 1/36 + 1/18 1/12 = 1/30 + 1/20 1/12 = 1/28 + 1/21 1/12 = 1/24 + 1/24
法一:
#include<cstdio> #include<iostream> #include<cstdio> using namespace std; int main() { int k,n; while( ~scanf("%d",&k)) { int cnt=0; if(k==0) return 0; for(int i=k+1;i<=2*k;i++) { if(((i*k)%(i-k))==0) cnt++; } printf("%d\n",cnt); for(int i=k+1;i<=2*k;i++) { printf("1/%d = 1/%d + 1/%d\n",k,(i*k)/(i-k),i); } } return 0; }
法二:
#include<cstdio> #include<iostream> #include<vector> #include<algorithm> using namespace std; vector<pair<int,int> > vec; int main() { int n,x,y; while(~scanf("%d",&n)) { vec.clear(); int cnt=0; for(int i=n+1;i<=2*n;i++) { if((i*n)%(i-n)==0) { x=i; y=i*n/(i-n); if(x<y) swap(x,y); vec.push_back(make_pair(x,y)); cnt++; } } sort(vec.begin(),vec.end()); printf("%d\n",cnt); for(int i=cnt-1;i>=0;i--) printf("1/%d = 1/%d + 1/%d\n",n,vec[i].first,vec[i].second); } return 0; }
AC题总结:
1.最后输出格式要与题意一致
2.时间超时的时候注意方法
3.输入scanf的返回值判断
标签:can i++ question second std write alt orm any
原文地址:http://www.cnblogs.com/is-Tina/p/7487611.html