第五章-算法入门
来源:互联网 发布:vb if语句前 编辑:程序博客网 时间:2024/04/28 16:11
5.1 equation
#include<iostream> #include<stdio.h> #include<string> using namespace std; double calcul(double x) { return 8*x*x*x*x + 7*x*x*x + 2*x*x +3*x + 6; } int main() { double y; cin >> y; if(y < 6 || y > calcul(100)) { cout << "No solution!" <<endl; } else { double x; double left = 0; double right = 100; x = (left + right) / 2; while(calcul(x) != y || (right - left) > 1e-8) { if(calcul(x) == y || (right - left) < 1e-8) { printf("%.4lf",x); return 0; } else if(calcul(x) > y) { right = x; } else if(calcul(x) < y) { left = x; } x = (left + right) / 2; } printf("%.4lf\n",x); } return 0; }
5.2 sequences
(使用64位整型)
#include<iostream> #include<stdio.h> #include<string> using namespace std; int main() { __int64 a1,a2,a3; int k; scanf("%I64d %I64d %I64d %d",&a1,&a2,&a3,&k); a1 = a1; a2 = a2; a3 = a3; __int64 d,q1,q2; d = a2 - a1; if(d == (a3-a2)) { cout << "arithmetic sequences. "<<endl; printf("%I64d\n",(a1 + (k-1) * d) % 1000000007); return 0; } __int64 q = a2 / a1; k--; cout << "geometric sequences. " << endl; //快速幂 __int64 res = a1; while(k>0) { if(k&1) res = (res*q)% 1000000007; q = (q*q) % 1000000007; k>>=1; } printf("%I64d\n",res % 1000000007); return 0; }
5.3 Game_Prediction
#include<iostream> #include<stdio.h> #include<string> using namespace std; bool arr[10086]; int main() { int count = 0; int m,n; cin >> m >> n; int a; int i; for(i =0; i < n ;i++) { cin >> a; arr[a] = true; } int max = m * n; while(arr[max]) { count++; max--; } //max为对方最大牌 i = 1; while(arr[i]) { i++; } //i为对方最小牌 while(!arr[i]) { i++; } //i为我方有效最小牌 int neg = 0; //相连0的数量 int pos = 0; //相连1的数量 for(int j = max; j >= i; j--) { while(!arr[j]) { neg++; j--; } while(arr[j]) { pos++; j--; } if(neg >= pos) { neg -= pos; pos = 0; } else { count += neg; pos -= neg; neg = 0; } } cout << count <<endl; return 0; }
5.4 Painter
#include<iostream> #include<stdio.h> #include<string> #include<algorithm> using namespace std; double arr[15]; double minu[15]; int main() { int i,j; int n; cin >> n; for(i = 1; i <= n;i++) cin >> arr[i]; cin >> arr[0]; //0为需要的灰色颜料 double ml = 0; bool flag = false; bool flag2= true; double count = 0; for(i = 1; i<= 40; i++) { if(flag) break; ml = i*50; //买到颜料体积 flag2 = true; for(j = 1; j <= n; j++) { if(arr[j] > ml) //需要超过买到 { flag2 = false; break; } else minu[j] = ml - arr[j]; } count = 0; while(flag2) { sort(minu+1,minu+n+1); if(!minu[n-2]) break; count++; if(count >= arr[0]) { flag = true; break; } minu[n]--; minu[n-1]--; minu[n-2]--; /// cout <<minu[12]<<" "<<minu[11]<<" "<<minu[10]<<endl; } } cout << i-1; return 0; }
5.5 Best_Cow_Line
#include<iostream> #include<stdio.h> #include<string> #include<algorithm> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int left = 0; int right = n-1; bool flag; while(left <= right) { flag = false; //确定left与right较小的一个,相同时比较下一位 for(int i = 0; i+left < right; i++) { if(s[i+left] < s[right-i]) { flag = true; break; } else if(s[i+left] > s[right-i]) { flag = false; break; } } if(flag) { cout << s[left]; left++; } else { cout << s[right]; right--; } } return 0; }
0 0
- 第五章-算法入门
- 算法竞赛入门经典 第五章 字符串
- 算法竞赛入门经典 第五章 6174
- 算法竞赛入门经典-第五章源代码
- 算法竞赛入门经典第五章
- 算法入门竞赛 第五章例题 题解
- 算法竞赛入门经典第五章习题
- 算法竞赛入门经典第五章习题
- 《算法竞赛入门经典》 第五章 基础题目选解
- 算法竞赛入门经典 第五章 字母重排
- 算法竞赛入门经典 第五章 因子和阶乘
- 算法竞赛入门经典(第一版) 第五章 解题报告
- 算法竞赛入门第五章(竞赛题目选讲)解析:
- 算法竞赛入门第五章习题分析(部分)
- 算法竞赛入门经典-第五章错位输出
- 算法竞赛入门经典-第五章TEX括号
- 算法竞赛入门经典-第五章周期串
- 算法竞赛入门经典-第五章小学生算术
- 单步调试 step into/step out/step over 区别
- Visual Studio 13编译MYSQL CONNECTOR C/C++ (MYSQLCPPCONN)
- 极大似然估计(MLE)
- 数据库三范式
- 解决浏览器记住密码输入框的问题
- 第五章-算法入门
- 非静态成员的sizeof
- win2003右键没有(共享和安全)的问题
- 分享 Matlab实现视频图像前景分割
- 深入理解Objective-C:Category(上)
- UVa 165 - Stamps
- Selenium+autoit+java 上传文件
- Android自定义控件---“取消”视图
- 客户端时间存储的取舍