“ Ctrl AC!一起 AC!”
题目集合链接:HDU2000-2099http://acm.hdu.edu.cn/listproblem.php?vol=11
1. 好像没有HDU2038 呜呜呜
2. 主要是整合提供了AC的代码,思路不太详细qwq
目录
1. ASCII码排序(HDU2000)
2. 计算两点间的距离(HDU2001)
3. 计算球体积(HDU2002)
4. 求绝对值(HDU2003)
5. 成绩转换(HDU2004)
6. 第几天?(HDU2005)
7. 求奇数的乘积(HDU2006)
8. 平方和与立方和(HDU2007)
9. 数值统计(HDU2008)
10. 求数列的和(HDU2009)
11. 水仙花数(HDU2010)
12. 多项式求和(HDU2011)
13. 素数判定(HDU2012)
14. 蟠桃记(HDU2013)
15. 青年歌手大奖赛_评委会打分(HDU2014)
16. 偶数求和(HDU2015)
17. 数据的交换输出(HDU2016)
18. 字符串统计(HDU2017)
19. 母牛的故事(HDU2018)
20. 数列有序!(HDU2019)
21. 绝对值排序(HDU2020)
22. 发工资咯:)(HDU2021)
23. 海选女主角(HDU2022)
24. 求平均成绩(HDU2023)
25. C语言合法标识符(HDU2024)
26. 查找最大元素(HDU2025)
27. 首字母变大写(HDU2026)
28. 统计元音(HDU2027)
29. Lowest Common Multiple Plus(HDU2028)
30. Palindromes _easy version(HDU2029)
31. 汉字统计(HDU2030)
32. 进制转换(HDU2031)
33. 杨辉三角(HDU2032)
34. 人见人爱A+B(HDU2033)
35. 人见人爱A-B(HDU2034)
36. 人见人爱A^B(HDU2035)
37. 改革春风吹满地(HDU2036)
38. 今年暑假不AC(HDU2037)
39. 三角形(HDU2039)
40. 亲和数(HDU2040)
41. 超级楼梯(HDU2041)
42. 不容易系列之二(HDU2042)
43. 密码(HDU2043)
44. 一只小蜜蜂...(HDU2044)
45. 不容易系列之(3)―― LELE的RPG难题(HDU2045)
46. 骨牌铺方格(HDU2046)
47. 阿牛的EOF牛肉串(HDU2047)
48. 神、上帝以及老天爷(HDU2048)
49. 不容易系列之(4)――考新郎(HDU2049)
50. 折线分割平面(HDU2050)
51. Bitset(HDU2051)
52. Picture(HDU2052)
53. Switch Game(HDU2053)
54. A == B ?(HDU2054)
55. An easy problem(HDU2055)
56. Rectangles(HDU2056)
57. A + B Again(HDU2057)
58. The sum problem(HDU2058)
59. 龟兔赛跑(HDU2059)
60. Snooker(HDU2060)
61. Treasure the new start, freshmen!(HDU2061)
62. Subset sequence(HDU2062)
63. 过山车(HDU2063)
64. 汉诺塔III(HDU2064)
65. "红色病毒"问题(HDU2065)
66. 一个人的旅行(HDU2066)
67. 小兔的棋盘(HDU2067)
68. RPG的错排(HDU2068)
69. Coin Change(HDU2069)
70. Fibbonacci Number(HDU70)
71. Max Num(HDU2071)
72. 单词数(HDU2072)
73. 无限的路(HDU2073)
74. 叠筐(HDU2074)
75. A|B?(HDU2075)
76. 夹角有多大(题目已修改,注意读题)(HDU2076)
77. 汉诺塔IV(HDU2077)
78. 复习时间(HDU2078)
79. 选课时间(HDU2079)
80. 夹角有多大II(HDU2080)
81. 手机短号(HDU2081)
82. 找单词(HDU2082)
83. 简易版之最短距离(HDU2083)
84. 数塔(HDU2084)
85. 核反应堆(HDU2085)
86. A1 = ?(HDU2086)
87. 剪花布条(HDU2087)
88. Box of Bricks(HDU2088)
89. 不要62(HDU2089)
90. 算菜价(HDU2090)
91. 空心三角形(HDU2091)
92. 整数解(HDU2092)
93. 考试排名(HDU2093)
94. 产生冠军(HDU2094)
95. find your present (2)(HDU2095)
96. 小明A+B(HDU2096)
97. Sky数(HDU2097)
98. 分拆素数和(HDU2098)
99. 整除的尾数(HDU2099)
思路:冒泡排序,如果前面的字母大于后面的字母 就交换
代码:
#include
#include
#include
#include
using namespace std;
int main(){
char a[5];
while(cin>>a){
//冒泡排序:
for(int i=0;i<3;i++){
for(int j=i;j<3;j++){
if(a[i]>a[j]){ //交换
int m=a[i];
a[i]=a[j];
a[j]=m;
}
}
}
for(int i=0;i<3;i++){
if(i!=2) cout<
思路:两点间的距离公式
代码:
#include
#include
#include
#include
using namespace std;
int main(){
double a,b,c,d;
while(cin>>a>>b>>c>>d){
double k=sqrt((a-c)*(a-c)+(b-d)*(b-d));
printf("%.2lf\n",k);
}
return 0;
}
思路:球体积的计算公式,注意PI的精度要够高
代码:
#include
#include
#include
#include
#define PI 3.1415927
using namespace std;
int main(){
double n;
while(cin>>n){
double v=(4*PI*n*n*n)/3;
printf("%.3lf\n",v);
}
return 0;
}
思路:fabs()求浮点数的绝对值(或直接判断改变正负)
代码:
#include
#include
#include
#include
#include
#define PI 3.1415927
using namespace std;
int main(){
double n;
while(cin>>n){
double k=fabs(n);
printf("%.2lf\n",k);
}
return 0;
}
思路:if else 的使用
代码:
#include
#include
#include
#include
#include
using namespace std;
int main(){
int n;
while(cin>>n){
if(n>=90&&n<=100){
cout<<"A"<=80&&n<=89){
cout<<"B"<=70&&n<=79){
cout<<"C"<=60&&n<=69){
cout<<"D"<=0&&n<=59){
cout<<"E"<
思路:先用一个数组存放每个月的天数,再用sum逐月逐日增加天数,若是闰年则需特殊处理二月
注意scanf输入格式!!!
代码:
#include
int main()
{
int num[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
int year,month,day,sum;
while(scanf("%d/%d/%d",&year,&month,&day)!=EOF)
{
sum=0;
if((year%4==0&&year%100!=0)||year%400==0) //是闰年
{
for(int i = 0; i < month-1; i++) //数组下标从 零 开始,但是月份从 一 开始
{
if(i==1)
{
sum = sum + 29; //二月变成29天
}
else
{
sum = sum + num[i];
}
}
sum = sum + day;
printf("%d\n",sum);
}
else
{
for(int i = 0; i < month-1; i++)
{
sum = sum + num[i];
}
sum = sum + day;
printf("%d\n",sum);
}
}
return 0;
}
思路:判断奇数:如果某个数是奇数,则其二进制最低一位是1
代码:
#include
using namespace std;
int main() {
int n;
while (cin >> n) {
int ans = 1;
for (int i = 0; i < n; i++) {
int num; cin >> num;
if (num & 1) ans *= num;
}
cout << ans << endl;
}
return 0;
}
思路:遍历判断奇偶
代码:
#include
#include
using namespace std;
int main() {
int m, n;
while ((scanf("%d %d", &m, &n) != EOF)) {
if(n
思路:判断正负
代码:
#include
using namespace std;
int main() {
int n;
while (cin >> n && n) {
int a = 0, b = 0, c = 0;
while (n--) {
float num; cin >> num;
if (num < 0) a++;
else if (num == 0) b++;
else c++;
}
cout << a << ' ' << b << ' ' << c << endl;
}
return 0;
}
思路:将数列的第一项n 不断开平方根,同时ans不断加上n
代码:
#include
using namespace std;
int main() {
double n, m;
while (cin >> n >> m) {
double ans = 0.0;
while (m--) {
ans += n;
n = sqrt(n);
}
cout << fixed << setprecision(2) << ans << endl;
}
return 0;
}
思路:数的分解,将数的每一位分解出来,再开三次方。(详见代码)
代码:
#include
using namespace std;
bool ishua(int num) {
// %10表示各位,/10%10表示十位...
if (num == (pow(num % 10, 3) + pow(num / 10 % 10, 3) + pow(num / 100 % 10, 3))) return true;
else return false;
}
int main() {
int m, n;
while (cin >> m >> n) {
//因格式要求,flag用来判断是否是第一个遇见的水仙花数,来控制空格的输出
//同时flag用来判断有没有遇见过水仙花数,来控制“no”的输出
int flag = false;
for (int i = m; i <= n; i++) {
if (ishua(i)) {
if (!flag) {
cout << i; flag = true;
}
else {
cout << " " << i;
}
}
}
if (!flag) cout << "no" << endl;
else cout<
思路:模拟多项式,通过i的奇偶判断加减
代码:
#include
using namespace std;
double f(int n) {
double ans = 0.0;
double first = 1;
for (int i = 1; i <= n; i++) {
if (i & 1) ans += (first / i);
else ans -= (first / i);
}
return ans;
}
int main() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
cout << fixed << setprecision(2) << f(n) << endl;
}
return 0;
}
思路:判断素数:首先1不是素数,2是素数。然后遍历2到sqrt(num),如果num能整除其中的某一个数,那么它就不是素数。否则是素数。
代码:
#include
using namespace std;
bool isprime(int num) {
if (num == 1) return false;
if (num == 2) return true;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
bool ok(int x, int y) {
for (int i = x; i <= y; i++) {
int ans = i * i + i + 41;
if (!isprime(ans)) return false;
}
return true;
}
int main() {
int x, y;
while (cin >> x >> y &&(x||y)) {
if (ok(x, y)) cout << "OK" << endl;
else cout << "Sorry" << endl;
}
return 0;
}
思路:这道题要逆着推答案,最后一天有1个桃子,那么倒数第二天有(1+1)*2个桃子,倒数第三天有((1+1)*2+1)*2个桃子。循环n-1次,因为第一天到第n-1天才吃了桃子,第n天没有吃桃子,只是告诉了我们那天的桃子个数。
代码:
#include
using namespace std;
int f(int n) {
int ans = 1;
for (int i = 2; i <= n; i++) {
ans = (ans + 1) * 2;
}
return ans;
}
int main() {
int n;
while (cin >> n) {
cout << f(n) << endl;
}
return 0;
}
思路:找到最大最小值,减去再除n-2
代码:
#include
using namespace std;
int main() {
int n;
while (cin >> n) {
double sum = 0, maxx = -1, minn = 0x3f3f3f3f;
for (int i = 0; i < n; i++) {
double num; cin >> num;
sum += num;
if (num > maxx) maxx = num;
if (num < minn) minn = num;
}
cout << fixed << setprecision(2) << (sum - maxx - minn) / (n - 2) << endl;
}
return 0;
}
思路:设一个计数器k,每次sum加数的时候,k++,k加到m的时候输出阶段性答案(注意空格处理),然后k归零。最后判断k是否大于零,以判断是否存在最后的剩余项
代码:
#include
using namespace std;
void f(int n, int m) {
int sum = 0, k = 0;
for (int i = 2; i <= 2 * n; i += 2) {
sum += i; k++;
if (k == m) {
//空格处理
if(i==2*n) cout< 0) {
cout << sum / k ;
}
}
int main() {
int n, m;
while (cin >> n >> m) {
f(n, m);
cout << endl;
}
return 0;
}
思路:找到最小的数,记录它的下标,最后与第一个数交换即可
代码:
#include
using namespace std;
int num[105];
int main() {
int n;
while (cin >> n && n) {
int minn = 0x3f3f3f3f;
int minn_id = 0;
for (int i = 0; i < n; i++) {
cin >> num[i];
if (num[i] < minn) {
minn = num[i];
minn_id = i;
}
}
swap(num[0],num[minn_id]);
for (int i = 0; i < n; i++) {
if (i == 0) cout << num[0];
else cout << " " << num[i];
}
cout << endl;
}
return 0;
}
思路:判断数字
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int cnt = 0;
string s; cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] >= '0' && s[i] <= '9') cnt++;
}
cout << cnt << endl;
}
return 0;
}
思路:动态规划,初始第一年的牛 1头(这头牛已经具有生育能力,可以理解为它岁数已经>=4),第二年的牛 2头,第三年的牛 3头。然后根据递推方程
a[i]=a[i-1]+a[i-3] 也就是今年的牛是去年的牛数量的基础上加上三年前之前的牛(也就是岁数>=4的牛)生出的新牛数量(这些新牛数量刚好就是这些大岁数的牛的数量,因为一头成熟的牛 每年必生且只生 一头小牛)
代码:
#include
#include
using namespace std;
int main()
{
int i,n;
int a[60];
a[1]=1;a[2]=2;a[3]=3;
for(i=4;i<56;i++)
{
a[i]=a[i-1]+a[i-3];
}
while(scanf("%d",&n)!=EOF && n)
{
printf("%d\n",a[n]);
}
return 0;
}
思路:边输入边输出,如果m大于当前数,就输出当前数;如果m小于当前数,就将m和当前数一起输出。并将m设为无穷大,防止重复输出。
代码:
#include
using namespace std;
int main() {
int n, m;
while (cin >> n >> m&&n&&m) {
for (int i = 0; i < n; i++) {
int num; cin >> num;
if (m != 0x3f3f3f3f && m <= num) {
cout << m << " " << num ;
m = 0x3f3f3f3f;
}
else {
cout << num ;
}
if(i!=n-1) cout<<" ";
}
cout << endl;
}
return 0;
}
思路:使用STL sort自定义排序算法(超级方便,不会的可以自行搜索学习qwq)
代码:
#include
using namespace std;
int num[105];
bool cmp(int a, int b) {
return abs(a) > abs(b); //如果a的绝对值大于b的绝对值,返回true,让a排在b前面
}
int main() {
int n;
while (cin >> n && n) {
for (int i = 0; i < n; i++) {
cin >> num[i];
}
sort(num, num + n, cmp);
for (int i = 0; i < n; i++) {
if (i == 0) cout << num[i];
else cout << " " << num[i];
}
cout << endl;
}
return 0;
}
思路:贪心,想要最少的钞票数,那钞票的面值肯定要越大越好。所以对于每个人的工资,先给他他能达到的最大面值的钱。可以通过int除法去除小数。对于工资45的老师,50面值太多了,所以先考虑10面值,给多少张10面值的钱呢。可以通过int除法:45/10=4 得知。
代码:
#include
using namespace std;
int main() {
int n;
while (cin >> n && n) {
int ans = 0;
while (n--) {
int salary; cin >> salary;
while (salary > 0) {
if (salary >= 100) {
ans += (salary / 100);
salary -= (salary / 100) * 100;
}
else if (salary >= 50) {
ans += (salary / 50);
salary -= (salary / 50) * 50;
}
else if (salary >= 10) {
ans += (salary / 10);
salary -= (salary / 10) * 10;
}
else if (salary >= 5) {
ans += (salary / 5);
salary -= (salary / 5) * 5;
}
else if (salary >= 2) {
ans += (salary / 2);
salary -= (salary / 2) * 2;
}
else {
ans += salary;
salary = 0;
}
}
}
cout << ans << endl;
}
return 0;
}
思路:用row,col,MAX实时记录更新的数据即可
代码:
#include
using namespace std;
int MM[105][105];
int main() {
int n, m;
while (cin >> n >> m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> MM[i][j];
}
}
int row, col, MAX = -1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (abs(MM[i][j]) > MAX) {
MAX = abs(MM[i][j]);
row = i, col = j;
}
}
}
cout << row << " " << col << " " << MM[row][col] << endl;
}
return 0;
}
思路:用二维数组,二维数组稍微开大点,这样就可以用后面的空间,存同一行或同一列的值。
因为scanf/printf较快,用较慢的cin/cout会WA!
代码:
#include
using namespace std;
double score[55][10];
int main() {
int n, m;
while (cin >> n >> m) {
memset(score, 0, sizeof(score));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%lf", &score[i][j]);
score[i][m] += score[i][j];
score[n][j] += score[i][j];
}
score[i][m] /= m;
}
for (int j = 0; j < m; j++) score[n][j] /= n;
int exceed = 0;
for (int i = 0; i < n; i++) {
int flag = true;
for (int j = 0; j < m; j++) {
if (score[i][j] < score[n][j]) flag = false;
}
if (flag) exceed++;
}
for (int i = 0; i < n; i++) {
if(i!=0) cout<<" ";
printf("%.2lf", score[i][m]);
}
cout << '\n';
for (int j = 0; j < m; j++) {
if(j!=0) cout<<" ";
printf("%.2lf", score[n][j]);
}
cout << '\n';
printf("%d\n\n", exceed);
}
return 0;
}
思路:C语言标识符只能是字母(A~Z,a~z)、数字(0~9)、下划线()组成的字符串,并且其第一个字符必须是字母或下划线。
代码:
#include
using namespace std;
bool legal(string s) {
if (isdigit(s[0])) return false;
for (int i = 0; i < s.size(); i++) {
if (!isalnum(s[i]) && s[i] != '_') return false;
}
return true;
}
int main() {
int t; cin >> t;
getchar();
while (t--) {
string s;
getline(cin, s);
if (legal(s)) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}
思路:MAX记录最大值
代码:
#include
using namespace std;
int main() {
string s;
while (getline(cin, s)) {
int MAX = -1;
for (int i = 0; i < s.size(); i++) {
if (s[i] - 'a' > MAX) MAX = s[i] - 'a';
}
for (int i = 0; i < s.size(); i++) {
if (s[i] - 'a' == MAX) cout << s[i] << "(max)";
else cout << s[i];
}
cout << endl;
}
return 0;
}
思路:toupper函数,printf("%c") 格式化输出
代码:
#include
using namespace std;
int main() {
string s;
while (getline(cin, s)) {
for (int i = 0; i < s.size(); i++) {
if (i == 0) printf("%c", toupper(s[i]));
else if (i != 0 && s[i - 1] == ' ') printf("%c", toupper(s[i]));
else cout << s[i];
}
cout << endl;
}
return 0;
}
思路:五个计数器,注意输出格式控制
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
getchar();
while (t--) {
string s;
getline(cin, s);
int a = 0, b = 0, c = 0, d = 0, e = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'a') a++;
else if (s[i] == 'e') b++;
else if (s[i] == 'i') c++;
else if (s[i] == 'o') d++;
else if (s[i] == 'u') e++;
}
cout << "a:" << a << endl;
cout << "e:" << b << endl;
cout << "i:" << c << endl;
cout << "o:" << d << endl;
cout << "u:" << e << endl;
if (t != 0) cout << endl;
}
return 0;
}
思路:gcd函数求两个数的最小公倍数,然后要求三个数的最小公倍数的话,可先求前两个的最小公倍数,再求这个最小公倍数与第三个数的最小公倍数,以此类推。
代码:
#include
#define int long long
using namespace std;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b,a%b);
}
signed main() {
int n;
while (cin>>n) {
int a[105] = { 0 };
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - 1; i++) {
int g = gcd(a[i], a[i + 1]);
a[i + 1] = (a[i] * a[i + 1]) / g;
}
cout << a[n - 1] << endl;
}
return 0;
}
思路:左右对比回文串
代码:
#include
using namespace std;
bool is(string s) {
int l = 0, r = s.size() - 1;
while (l < r) {
if (s[l] != s[r]) return false;
l++, r--;
}
return true;
}
int main() {
int t; cin >> t;
getchar();
while (t--) {
string s;
getline(cin, s);
if (is(s)) cout << "yes" << endl;
else cout << "no" << endl;
}
return 0;
}
思路:汉字码是小于0。并且一个汉字占两个字节。
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
getchar();
while (t--) {
string s;
getline(cin, s);
int cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] < 0) cnt++;
}
cout << cnt / 2 << endl;
}
return 0;
}
思路:进制转换经典例题
资料:十进制转换成十六进制
代码:
#include
using namespace std;
string turn(int num, int radix) {
stack st;
while (num > 0) {
int temp = num % radix;
if (temp >= 10) st.push(temp - 10 + 'A');
else st.push(temp + '0');
num /= radix;
}
string ans = "";
while (!st.empty()) {
ans += st.top();
st.pop();
}
return ans;
}
int main() {
int num, radix;
while (cin >> num >> radix) {
if (num < 0) {
cout << '-';
num = -num;
}
cout << turn(num, radix) << endl;
}
return 0;
}
思路:先预处理出30行的杨辉三角,再直接用就行。某行的杨辉三角规律,第一列与最后一列为1,其他的值为上一行的上一列的值加上一行的同一列的值。
代码:
#include
using namespace std;
int a[35][35];
void pre() {
a[0][0] = 1;
for (int i = 1; i <= 29; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) a[i][j] = 1;
else a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
}
int main() {
pre();
int n;
while (cin >> n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j != 0) cout << " ";
cout << a[i][j];
}
cout << endl;
}
cout << endl;
}
return 0;
}
思路:取余与int除法的应用
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int ah, am, as, bh, bm, bs;
cin >> ah >> am >> as >> bh >> bm >> bs;
int cs = (as + bs) % 60;
int cm = (am + bm + (as + bs) / 60) % 60;
int ch = ah + bh + (am + bm + (as + bs) / 60) / 60;
cout << ch << " " << cm << ' ' << cs << endl;
}
return 0;
}
思路:使用set可以自动排序,并且可以按值删除;
资料:set常见用法详解
代码:
#include
using namespace std;
int main() {
int n, m;
set se;
while (cin >> n >> m && (n||m)) {
se.clear();
for (int i = 0; i < n; i++) {
int num; cin >> num;
se.insert(num);
}
for (int i = 0; i < m; i++) {
int num; cin >> num;
if (se.find(num) != se.end()) se.erase(num);
}
if (se.size() == 0) cout << "NULL" << endl;
else {
for (auto it = se.begin(); it != se.end(); it++) cout << *it << " ";
cout << endl;
}
}
return 0;
}
思路:因为数可能会特别大,所以一步步次方的同时要一步步对1000取余,聚焦于后三位数字
代码:
#include
using namespace std;
int f(int n, int m) {
int ans = 1;
while (m--) {
ans *= n;
ans %= 1000;
}
return ans;
}
int main() {
int n, m;
while (cin >> n >> m && (n || m)) {
cout << f(n, m) << endl;
}
return 0;
}
思路:叉乘,这题的多边形好像都是中点在原点,或则卡轴三角形,所以简化了向量的获得过程
参考:叉乘
代码:
#include
using namespace std;
struct point {
int x, y;
}p[105];
int main() {
int n;
while (cin >> n && n) {
for (int i = 0; i < n; i++) {
cin >> p[i].x >> p[i].y;
}
p[n].x = p[0].x, p[n].y = p[0].y;
int area = 0;
for (int i = 0; i < n; i++) {
area += (p[i].x * p[i + 1].y - p[i].y * p[i + 1].x);
}
printf("%.1lf\n", area / (double)2);
}
return 0;
}
思路:贪心,将所有的节目按结束时间从小到大排序;初始化当前进行到的时间点(prelast,上一个能看的节目的结束时间,初始化为0),只要被判断的这个节目开始的时间>=当前的时间,就把这个节目算进去,然后更新prelast。
代码:
#include
using namespace std;
struct pro {
int s, e;
}p[105];
bool cmp(pro a, pro b) {
return a.e < b.e;
}
int main() {
int n;
while (cin >> n && n) {
for (int i = 0; i < n; i++) {
cin >> p[i].s >> p[i].e;
}
sort(p, p + n, cmp);
int prelast = 0, ans = 0; //prelast表示上一个能看的节目的结束时间
for (int i = 0; i < n; i++) {
if (p[i].s >= prelast) {
ans++;
prelast = p[i].e;
}
}
cout << ans << endl;
}
return 0;
}
思路:两边之和大于第三边,记得用double!!!(我前面define了)
代码:
#include
#define int double
using namespace std;
bool is(int a, int b, int c) {
if (a == 0 || b == 0 || c == 0) return false;
if (a + b <= c || a + c <= b || b + c <= a) return false;
return true;
}
signed main() {
int t; cin >> t;
while (t--) {
int a, b, c; cin >> a >> b >> c;
if (is(a, b, c)) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
思路:求真约数咯!
代码:
#include
using namespace std;
int yue(int num) {
int ans = 1; //真约数一定有 1
for (int i = 2; i <= sqrt(num); i++) {
if (i != sqrt(num) && num % i == 0) ans += (i + (num / i));
if (i == sqrt(num) && num % i == 0) ans += sqrt(num);
}
return ans;
}
int main() {
int t; cin >> t;
while (t--) {
int num1, num2; cin >> num1 >> num2;
if (yue(num1) == num2 && yue(num2) == num1) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
思路:动态规划,当前的方法等于上一级的方法加上两级的方法
代码:
#include
using namespace std;
int dp[55];
void pre() {
dp[2] = 1;
dp[3] = 2;
for (int i = 4; i <= 40; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
int main() {
pre();
int t; cin >> t;
while (t--) {
int M; cin >> M;
cout << dp[M] << endl;
}
return 0;
}
思路:先预处理,然后动态规划,从后往前算
代码:
#include
using namespace std;
int dp[35];
void pre() {
dp[0] = 3;
for (int i = 1; i <= 30; i++) {
dp[i] = (dp[i - 1] - 1) * 2;
}
}
int main() {
pre();
int t; cin >> t;
while (t--) {
int n; cin >> n;
cout << dp[n] << endl;
}
return 0;
}
思路:各种函数快捷判断
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
string s; cin >> s;
int a = 0, b = 0, c = 0, d = 0;
for (int i = 0; i < s.size(); i++) {
if (isupper(s[i])) a = 1;
else if (islower(s[i])) b = 1;
else if (isdigit(s[i])) c = 1;
else d = 1;
}
if ((a + b + c + d >= 3) && s.size() >= 8 && s.size() <= 16) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
思路:根据b-a的差值为关键进行动态规划,就是将a变成1,b变成b-a+1,这样就将起点都变成1了,方便运算。从1到达5的路线数=从1到达3的路线数+从1到达4的路线数。
代码:
#include
#define int long long
using namespace std;
int dp[55];
void pre() {
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= 50; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
signed main() {
pre();
int t; cin >> t;
while (t--) {
int a, b; cin >> a >> b;
cout << dp[b - a] << endl;
}
return 0;
}
思路:
n个格子只需要先涂n-1个,再涂一个就好。
1.第一个和第n-1个不同色的情况,,那么第n个只有一种涂法,所以第一种情况有1*a[n-1]种
2.第1个和第n-1个同色,那么第n个有两种涂法,而第1个和第n-1个同色的种数就是a[n-2]*1种,所以第二种情况有a[n-2]*1*2种
所以a[n]=a[n-1]+2*a[n-2]
n=3是特例,因为当n=3时是没有第二种情况的
代码:
#include
using namespace std;
long long a[60];
void pre() {
a[1] = 3, a[2] = 6, a[3] = 6;
for (int i = 4; i <= 50; i++) {
a[i] = a[i - 1] + 2 * a[i - 2];
}
}
int main() {
pre();
int n;
while (cin >> n) {
cout << a[n] << endl;
}
return 0;
}
思路:动态规划
代码:
#include
#define int long long
using namespace std;
int dp[55];
void pre() {
dp[1] = 1, dp[2] = 2;
for (int i = 3; i <= 50; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
signed main() {
pre();
int n;
while (cin >> n) {
cout << dp[n] << endl;
}
return 0;
}
思路:杭电ACM 2047 - 阿牛的EOF牛肉串(解题思路与详细分析)
代码:
#include
#define int long long
using namespace std;
int dp[50];
void pre() {
dp[1] = 3, dp[2] = 8;
for (int i = 3; i <= 40; i++) {
dp[i] = 2 * dp[i - 1] + 2 * dp[i - 2];
}
}
signed main() {
pre();
int n;
while (cin >> n) {
cout << dp[n] << endl;
}
return 0;
}
思路:神,上帝以及老天爷_昔我往矣wood的博客-CSDN博客_神、上帝以及老天爷
代码:
#include
#include
int main()
{
int i,j,n,a;
double g,f[100];
scanf("%d",&n);
for(j=0;j
思路:[ 不容易系列之(4)——考新郎]+(错排的简单理解+改进计算组合数)_丿繁年的博客-CSDN博客
代码:
#include
#define int long long
using namespace std;
int f[105];
int C(int n, int m) {
int ans = 1;
if (m < n - m) m = n - m;
for (int i = m + 1; i <= n; i++) {
ans *= i;
}
for (int i = 1; i <= n - m; i++) {
ans /= i;
}
return ans;
}
void pre() {
f[1] = 0, f[2] = 1;
for (int i = 3; i <= 20; i++) {
f[i] = (i - 1) * (f[i - 1] + f[i - 2]);
}
}
signed main() {
pre();
int t; cin >> t;
while (t--) {
int n, m; cin >> n >> m;
cout << f[m] * C(n, m) << endl;
}
return 0;
}
思路:折线分割平面(图文解析)_扣子不会飞的博客-CSDN博客_折线分割平面
代码:
#include
using namespace std;
int f(int m) {
int sum = 0;
for (int i = 1; i <= m - 2; i++) sum += i;
sum += 2 * m;
return sum;
}
int main() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
cout << f(2 * n) - 2 * n << endl;
}
return 0;
}
思路:十进制转二进制
代码:
#include
using namespace std;
void f(int n) {
stack st;
while (n > 0) {
st.push(n % 2);
n /= 2;
}
while (!st.empty()) {
printf("%d",st.top());
st.pop();
}
}
int main() {
int n;
while (~scanf("%d",&n)) {
f(n);
puts("");
}
return 0;
}
思路:模拟
代码:
#include
#include
int main(void) {
int i, j, w, h;
char ch[100][100];
while (scanf("%d %d", &w, &h) != EOF) {
memset(ch, ' ', 100 * 100*sizeof(char));
ch[0][0] = '+';
ch[0][w+1] = '+';
ch[h+1][0] = '+';
ch[h+1][w+1] = '+';
for (i = 1; i <= w; i++) {
ch[0][i] = '-';
ch[h+1][i] = '-';
}
for (j = 1; j <= h; j++) {
ch[j][0] = '|';
ch[j][w+1] = '|';
}
for (i = 0; i < h+2; i++) {
for (j = 0; j < w+2; j++)
printf("%c", ch[i][j]);
putchar('\n');
}
putchar('\n');
}
return 0;
}
思路:看某个数的因子有几个,就知道操作了几次
代码:
#include
using namespace std;
bool f(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) sum++;
}
return !(sum % 2 == 0);
}
int main() {
int n;
while (cin >> n) {
cout << f(n) << endl;
}
return 0;
}
思路:用字符串,消除前导零和小数后缀零
代码:
#include
using namespace std;
int main() {
string a, b;
while (cin >> a >> b) {
if (a.find('.')!=-1 && a[a.size() - 1] == '0') {
int id = a.size() - 1;
while (id >= 0 && a[id] != '.' && a[id] == '0') {
a.pop_back();
id--;
}
}
if (a[a.size() - 1] == '.') a.pop_back();
while (a.size() && a[0] == '0') a.erase(a.begin());
if (b.find('.')!=-1 && b[b.size() - 1] == '0') {
int id = b.size() - 1;
while (id >= 0 && b[id] != '.' && b[id] == '0') {
b.pop_back();
id--;
}
}
if (b[b.size() - 1] == '.') b.pop_back();
while (b.size() && b[0] == '0') b.erase(b.begin());
if (a == b) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
思路:字符差变数字
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
char a; int b; cin >> a >> b;
int sum = 0;
if (islower(a)) sum -= (a - 'a' + 1);
else sum += (a - 'A' + 1);
cout << sum + b << endl;
}
return 0;
}
思路:检测重叠部分
代码:
#include
double min(double x1,double x2)
{
if(x1x2)
return x1;
else
return x2;
}
void change(double *a ,double *b)
{
double temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
double x1,x2,x3,x4,y1,y2,y3,y4,s;
while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4))
{
if(x1>x2) change(&x1,&x2);
if(x3>x4) change(&x3,&x4);
if(y1>y2) change(&y1,&y2);
if(y3>y4) change(&y3,&y4);
if((x4>x1)&&(y4>y1)&&(x3
思路:使用格式化输入输出,64位整数
代码:
#include
using namespace std;
int main()
{
__int64 a,b,sum;
while(scanf("%I64X %I64X",&a,&b) != EOF)
{
sum = a + b;
if(sum < 0)
{
sum = fabs(sum);
printf("-%I64X\n",sum);
}
else
printf("%I64X\n",sum);
}
return 0;
}
思路:【杭电oj】2058 - The sum problem(高斯公式)_wyg1997的博客-CSDN博客
代码:
#include
#include
#include
using namespace std;
int main()
{
long long n,m;
long long a,b;
while (~scanf("%lld %lld",&n,&m) && (n || m))
{
for (int i = sqrt((double)(2*n)) ; i > 0 ; i--)
{
if ((2 * m) % i == 0 && ((2 * m / i) + i - 1) % 2 == 0)
{
a = ((2 * m / i) + i - 1) / 2;
b = 2 * m / i - a;
if (a > b)
swap(a,b);
if (a > 0)
printf ("[%d,%d]\n",a,b);
}
}
printf ("\n");
}
return 0;
}
思路:HDU 2059-龟兔赛跑_凉哈哈的博客-CSDN博客
代码:
#include
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 105;
int h[N];
double dp[N];
int main() {
int l;
while (cin >> l) {
int n, c, t; cin >> n >> c >> t;
int vr, vt1, vt2; cin >> vr >> vt1 >> vt2;
for (int i = 1; i <= n; i++) cin >> h[i];
h[0] = 0, h[n + 1] = l;
dp[0] = 0;
double tu = l * 1.0 / vr;
for (int i = 1; i <= n + 1; i++) {
double minx = INF;
for (int j = 0; j < i; j++) {
int dis = h[i] - h[j];
double time;
if (c >= dis) time = dis * 1.0 / vt1;
else time = (c * 1.0 / vt1) + (dis - c) * 1.0 / vt2;
if (j != 0) time += t;
if (dp[j] + time < minx) minx = dp[j] + time;
}
dp[i] = minx;
}
if (dp[n + 1] > tu) cout << "Good job,rabbit!" << endl;
else cout << "What a pity rabbit!" << endl;
}
return 0;
}
参考:HDU2060 Snooker【水题】_ITCharge的博客-CSDN博客
代码:
#include
#include
#include
#include
using namespace std;
int main()
{
int N;
cin >> N;
while(N--)
{
int ball,score1,score2;
cin >> ball >> score1 >> score2;
if(ball > 6)
{
score1 += (ball-6)*8+27;
}
else
{
int i = 7;
while(ball--)
{
score1 += i;
i--;
}
}
if(score1 >= score2)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
思路:算GPA
代码:
#include
#include
#include
using namespace std;
int main()
{
int N;
int j = 0;
cin >> N;
while(N--)
{
string name;
double credits,score;
double mem = 0, sum = 0;
int flag = 1, K;
cin >> K;
while( K-- ){
cin >> name >> credits >> score;
if(score < 60)
flag = 0;
mem+=credits*score;
sum+=credits;
}
if(flag)
printf("%.2lf\n",mem/sum);
else
printf("Sorry!\n");
if( N )
printf("\n");
}
return 0;
}
参考:HDU 2062——Subset sequence(最详细的解释,看不懂你找我)_unique_pursuit的博客-CSDN博客
代码:
#include
#include
#include
#include
#include
#include
参考:【例题记录】匈牙利算法求二分图的最大匹配_Ctrl AC的博客-CSDN博客
代码:
#include
using namespace std;
const int N = 1005;
int f[N][N], used[N], match[N];
int e, n, m;
bool DFS(int left) {
for (int i = 1; i <= m; i++) {
if (f[left][i] && !used[i]) {
used[i] = true;
if (!match[i] || DFS(match[i])) {
match[i]=left;
return true;
}
}
}
return false;
}
int main() {
while (cin >> e , e) {
cin >> n >> m;
memset(match, 0, sizeof match);
memset(f, 0, sizeof f);
for (int i = 1; i <= e; i++) {
int x, y; cin >> x >> y;
f[x][y] = true;
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
memset(used, 0, sizeof used);
if (DFS(i)) cnt++;
}
cout << cnt << endl;
}
return 0;
}
思路:hdu 2064:汉诺塔III_serendipityLB的博客-CSDN博客
代码:
#include
#define int long long
using namespace std;
const int N = 36;
int f[N];
signed main() {
f[1] = 2;
for (int i = 2; i <= 35; i++) {
f[i] = 3 * f[i - 1] + 2;
}
int n;
while (cin >> n) {
cout << f[n] << endl;
}
return 0;
}
思路:【组合数学】 红色病毒问题 详解(指数型生成函数+泰勒展开)_Nefu_qky的博客-CSDN博客
代码:
#include
#include
#include
using namespace std;
long long quickpower(long long base,long long power){
long long ret=1;
while(power){
if(power%2)
ret=ret*base%100;
base=base*base%100;
power/=2;
}
return ret;
}
int main()
{
int T;
while(~scanf("%d",&T)&&T){
for(int i=1;i<=T;i++){
long long N;
scanf("%lld",&N);
printf("Case %d: %lld\n",i,(quickpower(4,N-1)+quickpower(2,N-1))%100);
}
printf("\n");
}
return 0;
}
思路:HDU2066:一个人的旅行(Dijkstra)_键盘上的舞者的博客-CSDN博客
代码:
#include
#include
#include
using namespace std;
const int inf = 1<<30;
int T,S,D,n;
int map[1111][1111];
int vis[1111],cast[1111];
int s[1111],e[1111];
void Dijkstra()
{
int i,j,minn,pos;
memset(vis,0,sizeof(vis));
vis[0] = 1;
for(i = 0; i<=n; i++)
cast[i] = map[0][i];
for(i = 1; i<=n; i++)
{
minn = inf;
for(j = 1; j<=n; j++)
{
if(cast[j]
思路:HDU2067——小兔的棋盘(迷宫,动态规划,卡特兰数)_一百个Chocolate的博客-CSDN博客
代码:
#include
using namespace std;
const int maxn=40;
long long dp[maxn][maxn];
int main()
{
memset(dp,0,sizeof(dp));
for(int i=1;i>n&&n!=-1)
{
cout<<++k<<" "<
思路:RPG的错排【错排公式+组合数学】_Wuliwuliii的博客-CSDN博客
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
思路:Coin Change HDU - 2069 (超详细题解)_林小鹿@的博客-CSDN博客
代码:
#include
#include
#include
using namespace std;
int a[5] = { 1,5,10,25,50 }; ///表示5种硬币各自的价值
int dp[110][260];///表示dp[i][v] 用i个硬币凑成价值为v的方法数
int main()
{
int n;
while (cin >> n)
{
memset(dp, 0, sizeof(dp)); //初始化dp数组
dp[0][0] = 1;///边界
for (int i = 0; i < 5; i++)
{
for (int k = 1; k <= 100; k++) //k个硬币
{
for (int v = a[i]; v <= n; v++)
{
dp[k][v] += dp[k - 1][v - a[i]];//状态转移方程
}
}
}
int ans = 0;
for (int i = 0; i <= 100; i++)
{
ans += dp[i][n];
}
cout << ans << endl;
}
return 0;
}
思路:斐波那契数
代码:
#include
using namespace std;
__int64 fibo[55];
void pre() {
fibo[0] = 0, fibo[1] = 1;
for (int i = 2; i <= 50; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
}
}
int main() {
pre();
int n;
while (cin >> n && n != -1) cout << fibo[n] << endl;
return 0;
}
思路:HDU-2071 Max Num_springflower02的博客-CSDN博客
代码:
#include
using namespace std;
int main() {
int n;
cin>>n;
for(int i=0; i>m;
double max;
cin>>max;
for(int j=1; j>tmp;
if(tmp>max){
max = tmp;
}
}
printf("%.2lf\n", max);
}
return 0;
}
思路:HDU 2072 单词数_ultimater的博客-CSDN博客
代码:
#include
#include
#include
#include
using namespace std;
int main()
{
string s,w;
while(getline(cin,s),s!="#")
{
istringstream src(s);
setst;
while(src>>w)st.insert(w);
cout<
思路:杭电OJ2073 无限的路(已AC,超详细解释,适合初学者)_HDU-五七小卡的博客-CSDN博客
代码:
#include
#include
#include
double f[210];
double dabiao()//有公式了就先打表一下,就是提前计算好,下面代入点(x,y)的时候可以直接调用
{
f[1]=1;
for(int i=2;i<=200;i++)
{
double m,n;
m=sqrt((i-1)*(i-1)+(i-1)*(i-1));
n=sqrt(i*i+(i-1)*(i-1));
f[i]=f[i-1]+m+n;
}//有不明白的见上面解析
return 0;
}
double distance(int x,int y)
{
double d=0;
d=f[x+y]+x*sqrt(2.0);
return d;
}//点(x,y)到原点的距离公式
int main()
{
int x1,y1,x2,y2,n;
double d1,d2,d;
dabiao();
while(scanf("%d",&n)!=EOF)
{
while(n--)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
d1=distance(x1,y1);
d2=distance(x2,y2);
d=d1-d2;
if(d<0) d=-d;
printf("%.3lf\n",d);
}
}
return 0;
}
思路:一层层分析,注意格式
代码:
#include
using namespace std;
void modify(char mp[][85], int level, int n, char now) {
for (int j = level; j <= n - level + 1; j++) mp[level][j] = now, mp[n - level + 1][j] = now;
for (int i = level; i <= n - level + 1; i++) mp[i][level] = now, mp[i][n - level + 1] = now;
}
int main() {
int n; char cen, out;
bool flag = false;
while (cin >> n >> cen >> out) {
if (flag) {
puts("");
}
else flag = true;
char mp[85][85];
int level = (n + 1) / 2;
int now = level & 1 ? out : cen;
for (int i = 1; i <= level; i++) {
now = now == cen ? out : cen;
modify(mp, i, n, now);
}
if (n == 1) {
cout << cen << endl ;
}
else {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((i == 1 && j == 1) || (i == 1 && j == n) || (i == n && j == 1) || (i == n && j == n)) cout << ' ';
else cout << mp[i][j];
}
cout << endl;
}
}
}
return 0;
}
思路:大水题
代码:
#include
#define int long long
using namespace std;
signed main() {
int t; cin >> t;
while (t--) {
int a, b; cin >> a >> b;
if (a % b == 0) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
思路:按比例算
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
double h, m, s; cin >> h >> m >> s;
if (h > 12) h -= 12;
double shi = (h / 12) * 360 + ((m * 60 + s) / 3600) * 30;
double fen = (m / 60) * 360 + (s / 60) * 6;
double d = max(fen, shi) - min(fen, shi);
if (d > 180) d = 360 - d;
cout << (int)d << endl;
}
return 0;
}
思路:【杭电oj】2077 - 汉诺塔IV(递推)_wyg1997的博客-CSDN博客
代码:
#include
int main()
{
__int64 hanoi[22]; //把n个移到相邻杆子所需次数
hanoi[0] = 0;
hanoi[1] = 1;
for (int i = 2 ; i <= 19 ; i++)
hanoi[i] = hanoi[i-1] * 3 + 1;
int u;
scanf ("%d",&u);
int n;
while (u--)
{
scanf ("%d",&n);
printf ("%d\n",2*hanoi[n-1]+2);
}
return 0;
}
思路:HDU2078 复习时间【水题】_海岛Blog的博客-CSDN博客
代码:
/* HDU2078 复习时间 */
#include
#include
int main(void)
{
int t, n, m, a, i;
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
int min = INT_MAX;
for(i=1; i<=n; i++) {
scanf("%d", &a);
if(a < min)
min = a;
}
printf("%d\n", (100 - min) * (100 - min));
}
return 0;
}
思路:选课时间 (HDU-2079)(母函数)_Stephencurry‘s csdn的博客-CSDN博客
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int N, K; cin >> N >> K;
int v[10], n[10], a[45], b[45];
for (int i = 0; i < K; i++) {
cin >> v[i] >> n[i];
}
memset(a, 0, sizeof a);
a[0] = 1;
for (int i = 0; i < K; i++) {
memset(b, 0, sizeof b);
for (int j = 0; j <= n[i] && j * v[i] <= N; j++) {
for (int k = 0; k + j * v[i] <= N && k <= N; k++) {
b[k + j * v[i]] += a[k];
}
}
memcpy(a, b, sizeof b);
}
cout << a[N] << endl;
}
return 0;
}
思路:https://blog.csdn.net/weixin_44313771/article/details/104590798?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166281639716800192276318%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166281639716800192276318&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-3-104590798-null-null.142^v47^pc_rank_34_default_2,201^v3^control_1&utm_term=%E5%A4%B9%E8%A7%92%E6%9C%89%E5%A4%9A%E5%A4%A7II
代码:
#include
#include
int main()
{
int T;
double a, b, c, t, x1, y1, x2, y2;
scanf("%d", &T);
while (T--)
{
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
a = sqrt(x1 * x1 + y1 * y1);//第一个点到原点的距离
b = sqrt(x2 * x2 + y2 * y2);//第二个点到原点的距离
c = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));//两点之间的距离
t = acos((a * a + b * b - c * c) / (2 * a * b)) * (180 / acos(-1.0));//因为cosπ=-1,所以arccos-1=π
printf("%.2f\n", t);
}
return 0;
}
思路:字符串处理
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
string s; cin >> s;
cout << 6;
for (int i = 6; i <= 10; i++) {
cout << s[i];
}
cout << endl;
}
return 0;
}
思路:母函数模板
代码:
#include
using namespace std;
const int maxn = 10010;
const int INF = 0x3f3f3f3f;
int n[maxn], v[maxn];
int sup[maxn], temp[maxn];
int main() {
int t; cin >> t;
while (t--) {
for (int i = 1; i <= 26; i++) {
cin >> n[i]; v[i] = i;
}
memset(sup, 0, sizeof sup);
sup[0] = 1;
for (int i = 1; i <= 26; i++) {
memset(temp, 0, sizeof temp);
for (int j = 0; j <= n[i] && j * v[i] <= 50; j++) {
for (int k = 0; k <= 50 && k + j * v[i] <= 50; k++) {
temp[k + j * v[i]] += sup[k];
}
}
memcpy(sup, temp, sizeof temp);
}
int sum = 0;
for (int i = 1; i <= 50; i++) {
if (sup[i]) sum += sup[i];
}
cout << sum << endl;
}
return 0;
}
思路:选中间的点
代码:
#include
#include
int main()
{
int m,t,n,i,j,s,a[501],c;
scanf("%d",&m);
for (t=0;ta[j])
{
c=a[i];a[i]=a[j];a[j]=c;
}
for (i=0;i
思路:动态规划,从底层开始往上推
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int n; cin >> n;
int mp[105][105] = { 0 };
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cin >> mp[i][j];
}
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
mp[i][j] = max(mp[i + 1][j], mp[i + 1][j + 1]) + mp[i][j];
}
}
cout << mp[1][1] << endl;
}
return 0;
}
思路:递推
代码:
#include
#define int long long
using namespace std;
int gao[35], di[35];
void pre() {
gao[0] = 1, di[0] = 0;
for (int i = 1; i <= 33; i++) {
gao[i] = 3 * gao[i - 1] + 2 * di[i - 1];
di[i] = gao[i - 1] + di[i - 1];
}
}
signed main() {
pre();
int n;
while (cin >> n && n != -1) {
cout << gao[n] << ", " << di[n] << endl;
}
return 0;
}
思路:HDU2086 A1 = ?(组合数学 + 思维)_江心之人的博客-CSDN博客
代码:
#include
using namespace std;
int main() {
int n;
while (~scanf("%d",&n)) {
double a0, an1;
scanf("%lf%lf", &a0, &an1);
double sum = 0;
for (int i = 0; i < n; ++i) {
double t;
scanf("%lf", &t);
sum += (n - i) * t;
}
double a1 = (n * a0 + an1 - 2 * sum) / (n + 1);
printf("%.2lf\n", a1);
}
return 0;
}
思路:KMP算法【算法小结】KMP及扩展KMP_Ctrl AC的博客-CSDN博客
代码:
#include
using namespace std;
int nxt[105];
void getNext(string s, int len) {
int j = -1; nxt[0] = -1;
for (int i = 1; i < len; i++) {
while (j != -1 && s[i] != s[j + 1]) j = nxt[j];
if (s[i] == s[j + 1]) j++;
nxt[i] = j;
}
}
int KMP(string text, string pattern) {
int n = text.size(), m = pattern.size();
getNext(pattern, m);
int ans = 0, j = -1;
for (int i = 0; i < n; i++) {
while (j != -1 && text[i] != pattern[j + 1]) {
j = nxt[j];
}
if (text[i] == pattern[j + 1]) j++;
if (j == m - 1) ans++, j = -1;
}
return ans;
}
int main() {
string a, b;
while (cin >> a && a != "#") {
cin >> b;
cout << KMP(a, b) << endl;
}
return 0;
}
思路:先算平均值,再算每个abs,加起来除2,注意输出格式
代码:
#include
using namespace std;
int main() {
int n; bool flag = false;
while (cin >> n, n) {
vector vec;
int all = 0;
for (int i = 0; i < n; i++) {
int num; cin >> num;
all += num;
vec.push_back(num);
}
int ave = all / n; int sum2 = 0;
for (int i = 0; i < n; i++) {
sum2 += abs(vec[i] - ave);
}
if (flag) cout << endl;
else flag = true;
cout << sum2 / 2 << endl;
}
return 0;
}
思路:数位dp总结 之 从入门到模板_wust_wenhao的博客-CSDN博客_数位dp
代码:
#include
using namespace std;
int a[20];
int dp[20][2];
int dfs(int pos, int pre, int sta, bool limit) {
//cout << "???" << endl;
if (pos == -1) return 1;
if (!limit && dp[pos][sta] != -1) return dp[pos][sta];
int up = limit ? a[pos] : 9;
int tmp = 0;
for (int i = 0; i <= up; i++) {
if (pre == 6 && i == 2) continue;
if (i == 4) continue;
tmp += dfs(pos - 1, i, i == 6, limit && i == a[pos]);
}
if (!limit) dp[pos][sta] = tmp;
return tmp;
}
int solve(int num) {
int pos = 0;
while (num) {
a[pos++] = num % 10;
num /= 10;
}
return dfs(pos - 1, -1, 0, true);
}
int main() {
int n, m;
while (cin >> n >> m, n && m) {
memset(dp, -1, sizeof dp);
cout << solve(m) - solve(n - 1) << endl;
}
return 0;
}
思路:水题
代码:
/* HDU2090 算菜价 */
#include
#define N 32
char s[N];
int main(void)
{
double x, y, sum = 0;
while(~scanf("%s%lf%lf", s, &x, &y))
sum += x * y;
printf("%.1f\n", sum);
return 0;
}
思路:注意行末没有空格,观察每行的格式
代码:
#include
using namespace std;
int main() {
char c; int height; bool flag = false;
while (cin >> c && c != '@') {
if (flag) { cout << endl; }
else { flag = true; }
cin >> height;
int width = 2 * height - 1;
for (int i = 1; i <= height-1; i++) {
int space1 = height - i;
int space2 = (i - 1) * 2 - 1;
while (space1--) cout << " ";
cout << c;
if (space2 > 0) {
while (space2--) cout << " ";
cout << c;
}
cout << endl;
}
for (int i = 1; i <= width; i++) cout << c;
cout << endl;
}
return 0;
}
思路:整数解(韦达定理解法)_轩辕小猪的博客-CSDN博客
代码:
#include
using namespace std;
#include
int main()
{
ios::sync_with_stdio(false);
int m, n;
while (cin >> m >> n)
{
if (m == 0 && n == 0)
break;
else
{
if ((m * m - 4 * n) < 0)
cout << "No" << endl;
else
{
if (sqrt(m * m - 4 * n) == (int)sqrt(m * m - 4 * n))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
}
return 0;
}
思路:2093 考试排名_焚冉的博客-CSDN博客
代码:
#include
#include
#include
#include
using namespace std;
struct acm //定义结构体
{
char name[100];
int ac;
int sj;
}ss[10000];
bool cmp(acm a,acm b) //排序函数
{
if(a.ac==b.ac)
{
return a.sjb.ac; //ac数,大则大。
}
}
int main()
{
int z=0;
int n,m;
cin>>n>>m; //输入考试题数n,单位罚分数m。
while(cin>>ss[z].name) //没有规定人数,直接用while循环
{
for(int a=0;a>k; //接收成绩
if(k<=0)
{
continue; //如果输入的成绩小于0则跳出当前循环,进行下一次循环。
}
ss[z].ac++; //没有跳出循环的话,程序从上而下,执行这条语句,ac数+1
ss[z].sj+=k;//当前人的总成绩
if(getchar()=='(')//如果有字符“(”,接收且判断。Getchar()用于只接收一个字符。
{
cin>>l; //接收罚题次数
cin>>ch; //接收字符“)”
ss[z].sj+=l*m; //总成绩加上罚时
}
}
z++; //次数+1,此用于判断总统计人数。不然没有办法输出。
}
sort(ss,ss+n,cmp); //结构体排序
for(int i=0;i
思路:出度为零的点有且只有一个
代码:
#include
using namespace std;
int main() {
int t;
while (cin >> t , t ) {
map chu;
map exist;
while (t--) {
string a, b; cin >> a >> b;
exist[a]++; exist[b]++;
chu[b]++;
}
int cnt = 0;
for (auto it = exist.begin(); it != exist.end(); it++) {
string now = it->first;
if (chu[now] == 0) cnt++;
}
cout << (cnt == 1 ? "Yes" : "No") << endl;
}
return 0;
}
思路:同一个数异或同一个数等于零
代码:
#include
using namespace std;
int main() {
int t;
while (scanf("%d",&t) != EOF && t ) {
int sum = 0;
for (int i = 0; i < t; i++) {
int num; scanf("%d", &num);
sum ^= num;
}
cout << sum << endl;
}
return 0;
}
思路:%100
代码:
#include
using namespace std;
int main() {
int t; cin >> t;
while (t--) {
int a, b; cin >> a >> b;
a = a % 100, b = b % 100;
int c = (a + b) % 100;
cout << c << endl;
}
return 0;
}
思路:进制转换
代码:
#include
int main()
{
int n, s1, s2, sum;
int fun(int n, int p);
while (scanf("%d", &n) != EOF)
{
if (n == 0)
break;
sum = (n / 1000) + (n % 1000) / 100 + (n % 100) / 10 + n % 10;
s1 = fun(n, 12);
s2 = fun(n, 16);
if (sum == s1 && sum == s2)//判断
printf("%d is a Sky Number.\n", n);
else
printf("%d is not a Sky Number.\n", n);
}
return 0;
}
int fun(int n, int p)
{
int temp, s;
s = 0;
while (n != 0)
{
temp = n % p;//求余数
n = n / p;//求商
s = s + temp;//求各位数的和
}
return s;
}
思路:分拆素数和 (hdu 2098)_阿里斯顿k的博客-CSDN博客_分拆素数和
代码:
#include
#include
#include
using namespace std;
int k=0; //1是素数 0不是素数
int sushu (int a){
for(int i=2;i*i<=a;i++){
if(a%i==0)
return 0;
}
return 1;
}
int main()
{
int n,sum;
while(~scanf("%d",&n),n)
{ sum=0;
for(int i=2;i
思路:先乘上100,再看余数。注意格式
代码:
#include
using namespace std;
int main() {
int a, b;
while (cin >> a >> b, a && b) {
a *= 100;
int left = a % b;
int add = b - left;
bool first = true;
if (add == b) {
first = false;
cout << "00";
}
for (int i = add; i <= 99; i += b) {
if (!first) cout << " ";
else first = false;
if (i / 10 == 0) cout << 0 << i;
else cout << i ;
}
cout << endl;
}
return 0;
}
“ Ctrl AC!一起 AC!”