1051 平方根的和
代码(dp 递推,dp[0]存储求和)
#include<bits/stdc++.h>
using namespace std;int main() {int item, n;cin >> item >> n;double dp[1000];memset(dp,0,sizeof dp);dp[1] = item; dp[0] = dp[1];for(int i = 2; i <= n; i ++) {dp[i] = sqrt(dp[i-1]);dp[0] += dp[i];}//cout << dp[3] << endl;printf("%.2lf\n",dp[0]);return 0;
}
1052 数列求和4
代码(dp递推,dp[0]存储求和)
#include<bits/stdc++.h>
using namespace std;int dp[10];int main() {int n, a;cin >> n >> a;for(int i = 1; i <= n; i ++) {dp[i] = dp[i-1]*10 + a;dp[0] += dp[i];}cout << dp[0] << endl;return 0;
}
1053 正弦函数
代码
#include<bits/stdc++.h>
using namespace std;int main() {double x;cin >> x;double z = x, m = 1, flag = 1, sum = 0;for(int i = 1; i <= 10; i ++) {sum += flag*z/m;flag = -flag;z = z*x*x;m = m*2*i*(2*i+1);}printf("%.3lf\n",sum);return 0;
}
1054 猴子吃桃
代码 (dp从n推到1)
#include<bits/stdc++.h>
using namespace std;double dp[30];int main() {int n;cin >> n;dp[n] = 1;for(int i = n-1; i >=1; i --) {dp[i] = 2*dp[i+1]+2;}cout << (int)dp[1] << endl;return 0;
}
1055 兔子繁殖问题
分析
二维dp,横坐标代表月份,纵坐标三种
纵坐标三种,1,2,3.分别代表1月大,2月大,满3月及其以上,2月和满3月在下一月都要生1月大的兔子。
代码(二维dp,横坐标代表月份,纵坐标三种)
#include<bits/stdc++.h>
using namespace std;int dp[50][4];int main() {int n;cin >> n;dp[1][1] = 1;for(int i = 2; i <= n; i ++) {dp[i][1]=dp[i][3] = dp[i-1][2]+dp[i-1][3];dp[i][2] = dp[i-1][1];}cout << dp[n][1]+dp[n][2]+dp[n][3];return 0;
}
1056 整存整取
P1056 - 整存零取 - HAUTOJ
代码(dp递推)
横坐标五个,1,2,3,4,5分别代表第几年。数组里存储银行里有多少钱。纵坐标是,
#include<bits/stdc++.h>
using namespace std;double dp[7];int main() {double x;cin >> x;for(int i = 5; i > 0; i --) {dp[i] = dp[i+1] + 1000;dp[i] = dp[i]/(1.0+12*x);}cout << dp[1] << endl;return 0;
}
1057 素数判断
代码
注意不要漏了 n == 1的情况。
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;int flag = 0;for(int i = 2; i*i<=n; i ++) {if(n%i==0) {flag = 1;break;}}if(flag || n==1) puts("No");else puts("Yes");return 0;
}
1058 求解不等式
代码
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;unsigned long sum = 1,num = 1;int i;for(i = 2; sum < n; i ++) {num *= i;sum += num;}cout << "m<=" << i-2 << endl;//第一项判断sum<n 是i==2时判断的 加上i++,故i-2 return 0;
}
1059 最高分
代码
#include<bits/stdc++.h>
using namespace std;int main() {int maxx = 0;int x;while(cin >> x && x>=0) {maxx = max(maxx,x);}cout << maxx << endl;return 0;
}
1060 逆序数字
代码(取模%)
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;//if(n == 0) {cout << 0 <<" "; return 0;}while(n) {cout << n%10 << " ";n /= 10;}return 0;
}
1061 顺序输出各位数字
代码(使用stack逆序输出)
也可以使用数组存储,从后开始输出;
或者先用一个循环计算最高位权,如12345,要输出1,先计算处最高位需要除以10000,从第一位开始,每%10一个数就乘以10的位权。12345*1, 1234*10; 123*100, 12*1000, 1*10000.
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;stack<int> stk;while(n) {stk.push(n%10);n /= 10;}while(stk.size()) {cout << stk.top() << " ";stk.pop();}return 0;
}
代码2(计算最高位位权)
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;int h = 1;int t = n;n /= 10;while(n) {n /= 10;h *= 10;}n = t;while(h) {cout << n/h << " ";n -= (n/h)*h;h /= 10;}return 0;
}
1062 最大公约数
代码(模拟)
我的笔记,欧几里得(辗转相除法)
数论—快速幂,欧几里得及其扩展,逆元,单位元-CSDN博客
#include<bits/stdc++.h>
using namespace std;int main() {int a, b;cin >> a >> b;while(a%b) {int c = a%b;a = b;b = c;}cout << b << endl;return 0;
}
代码2(数组dp递推存储)
#include<bits/stdc++.h>
using namespace std;int dp[100];int main() {int a, b;cin >> a >> b;dp[1] = a; dp[2] = b;int i = 2;while(dp[i-1]%dp[i]) {dp[i+1] = dp[i-1]%dp[i];i ++;}cout << dp[i] << endl;return 0;
}
1063 最大公约与最小公倍数
代码
最小公倍数 = a*b / (最大公约数)
注意,先除后乘防止数据溢出,使用unsigned 也没用。
#include<bits/stdc++.h>
using namespace std;int dp[100];int main() {int a, b;cin >> a >> b;dp[1] = a; dp[2] = b;int i = 2;while(dp[i-1]%dp[i]) {dp[i+1] = dp[i-1]%dp[i];i ++;}//cout << dp[i] << endl;
// unsigned long res = dp[1]*dp[2]/dp[i] ;cout << dp[i] << " " << dp[1]/dp[i]*dp[2] << endl;return 0;
}
1064 加密字符
代码
注意空格字符存在,所以不能cin (碰到空格停止) 需要 ch = getchar();
转换后是小写也要小写下一位 K->k->l 所以 if if 而不是if else if, 并且先大写转小写
#include<bits/stdc++.h>
using namespace std;int main() {char ch;ch = getchar();while( ch!='@') {if(ch >= 'A' && ch <= 'Z') ch = ch + 'a'-'A';if(ch >='a' && ch<='z') ch = 'a'+(ch-'a'+1)%26;cout << ch ;ch=getchar();}return 0;
}
1065 统计数字字符的个数
代码(string的getline(cin,s) )
#include<bits/stdc++.h>
using namespace std;int main() {string s;getline(cin,s);int res = 0;for(int i = 0; i < s.size(); i ++) {if(s[i]>='0'&&s[i]<='9') res ++;}cout << res << endl;return 0;
}
代码2(char ch[] 的gets(ch) )
#include<bits/stdc++.h>
using namespace std;int main() {char ch[100];gets(ch);int res = 0;for(int i = 0; ch[i]!='\0'; i ++) {if(ch[i]>='0'&&ch[i]<='9') res ++;}cout << res << endl;return 0;
}
1066 字符分类统计
代码
#include<bits/stdc++.h>
using namespace std;int zf,sz,qt;int main() {string s;getline(cin,s);for(int i = 0; i < s.size(); i ++) {if(s[i]>='0'&&s[i]<='9') sz ++; else if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z') )zf ++;else qt++;}cout << "letter:" << zf << endl;cout << "digit:" << sz << endl;cout << "other:" << qt << endl;return 0;
}
1067 有问题的里程表
分析:
数据规模小,可以从1-num 遍历,只要里边包含有4的数 不要,剩下数目就是了。
这个里程表是一个,只有 1 2 3 5 6 7 8 9 0 的九进制数,我们需要十进制。
代码(九进制转十进制)
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;int sum = 0;int base = 1;while(n) {int num = n%10;if(num>4) num --;sum += num*base;base *= 9; n/=10;}cout << sum << endl;return 0;
}
1068 二进制数
代码(直接从前往后递推不断*2)
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;unsigned res = 0;for(int i = 0; i < n; i ++) {int x;cin >> x;res = res*2 + x;} cout << res << endl;return 0;
}
1069 向z同学学习
代码
#include<bits/stdc++.h>
using namespace std;int main() {int day = 0;int m, k;cin >> m >> k;while(m) {m --;day ++;if(day%k==0) m++;}cout << day << endl;return 0;
}
代码2
1070 小汽车的位置
代码
#include<bits/stdc++.h>
using namespace std;int main() {int dx[5] = {-10,0,10,0}, dy[5] = {0,-10,0,10};int time,command;int pretime = 0;int x = 0, y = 0;int i = 3;while(cin >> time >> command ) {x += dx[i]*(time-pretime), y += dy[i]*(time-pretime);switch(command) {case 1: i = (i+1)%4; break;case 2: i = (i+3)%4; break;}if(command == 3) break; //刚好移动完最后一次,pretime 到 time(command==3) //x += dx[i]*(time-pretime), y += dy[i]*(time-pretime);pretime = time;}cout << x <<" "<< y << endl;return 0;
}
1071 分解质因子
代码
直接除就不好,不用判断因子是不是素数,因为从2开始弄,哪怕有4,也会拆成2*2,根据这个思路,直接遍历所有因子就行。
#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;int t = n;int flag = 1;for(int i = 2; i <= n; ) {if(t%i && t!=i) {i ++; continue;}if(flag) {flag = 0;cout << i;}else cout << " " << i;t /= i;}return 0;
}
1072 青蛙爬井
代码
对每天进行循环,看看哪一天白天能爬上去(只有白天是往上爬)
也就是在白天之后加一个剩余高度判断if(h<=0) break; 爬上去了
#include<bits/stdc++.h>
using namespace std;int main() {int day = 0;int h, up, d;cin >> h >> up >> d;while(h>0) {day ++;if(h>0) h-=up;if(h<=0) break;h+=d;}cout << day << endl;return 0;
}