uva725除法uva11059最大乘积uva10976分数拆分(暴力求解法)

来源:互联网 发布:张艺谋电影 知乎 编辑:程序博客网 时间:2024/04/28 06:15

原题链接:

725:  

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666

11059:   

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2000

10976:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1917


725:

利用 被除数 和 商 求除数。检测被除数与除数是否符合要求。注意换行问题与循环范围。O(n)

11059:

注意大小,用longlong。 不需要建二维数组(详见代码)。O(n^2)(也可dp    O(n))

10976:

易得出 循环范围为  k+1  到 2*k 。利用y和k求x,判断x是否是整数。O(n)


代码如下:

725

#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cstdio>using namespace std;int n;bool P;//标记是否存在满足要求的bool check(int x, int y)//检测0-9是否都有{bool p[10];int z,k=y;memset(p, 0, sizeof(p));while (x){z = x % 10;if (p[z])return false;p[z] = true;x /= 10;}while (y){z = y % 10;if (p[z])return false;p[z] = true;y /= 10;}if (k < 10000)p[0] = true;for (int i = 0; i < 10;i++)if (!p[i]) return false;return true;}void solve(){double j;for (int i = 12345; i <= 98765; i++){j = i*1.0 / n;if ((int)j==j&&check(i,(int)j))//需判断j是否为整数{cout << i << " / ";if (j < 10000)printf("%05d", (int)j);//不到10000补0,也可用c++(比较麻烦)else cout << j;cout << " = " << n << endl;P = false;}}}int main(){bool o = false;while (cin >> n&&n){if (o)//注意换行问题cout << endl;else o = true;P = true;solve();if (P)cout << "There are no solutions for "<<n<<"." << endl;}return 0;}

11059

#include<iostream>#include<cstring>#include<string>#include<cstdio>using namespace std;int number[20];int main(){int n;int k = 0;while (cin >> n){long long  max = 0;//long longfor (int i = 1; i <= n; i++)cin >> number[i];int begin, end;for (begin = 1; begin <= n; begin++){long long tempsum = 1, sum = 1;//long  long  以及赋值为1for (end = begin; end <= n; end++){sum= tempsum* number[end];if (sum> max)max = sum;tempsum = sum;//勿忘更新tempsum的值}}cout << "Case #" << ++k << ": The maximum product is " << max << "." << endl << endl;}return 0;}

10976

#include<iostream>#include<cstdio>using namespace std;int X[10000 + 10];int Y[10000 + 10];int main(){int k;while (cin >> k){int sum = 0;for (int y = k + 1; y <= 2 * k; y++){double x = k*y*1.0 / (y-k);if ((int)x == x){X[sum] = x;Y[sum] = y;sum++;}}cout << sum << endl;for (int i = 0; i < sum;i++)cout << "1/" << k << " = 1/" << X[i] << " + 1/" << Y[i] << endl;}return 0;}


0 0