数据结构考研学习笔记-创新互联
计算机考研机试备考(王道)
第二章 暴力求解
1.枚举策略
网站题目:数据结构考研学习笔记-创新互联
文章位置:http://cdiso.cn/article/dsjjsp.html
常见复杂度对应数据量表
例题2.1 abc
#includeusing namespace std;
int main()
{int a, b, c;
for(a=0;a<9;a++)
for(b=0;b<9;b++)
for (c = 0; c< 9; c++)
{ if ((a * 100 + b * 10 + c + b * 100 + c * 10 + c) == 532)
cout<< a<< " "<< b<< " "<< c<< endl;
}
return 0;
}
例题2.2 反序数
#includeusing namespace std;
int reverse(int number)
{int reversenumber = 0;
while (number != 0)
{reversenumber *= 10;
reversenumber += number % 10;
number /= 10;
}
return reversenumber;
}
int main()
{for (int i = 1000; i< 9999; i++)
if (i * 9 == reverse(i)) { cout<< i<< endl;
}
return 0;
}
例题2.3 对称平方数
#includeusing namespace std;
int Reverse(int number) {int reverseNumber = 0;
while (number != 0) {reverseNumber *= 10;
reverseNumber += number % 10;
number /= 10;
}
return reverseNumber;
}
int main() {for (int i = 1; i< 256; i++) {if (i * i == Reverse(i * i)) {cout<< i<< endl;
}
}
return 0;
}
2.图形排版例题2.4 输出梯形
#includeusing namespace std;
int main()
{int h; //梯形行数
while (cin >>h)
{int row = h;
int col = h + 2 * (h - 1); //推导公式得出列数
for (int i = 0; i< row; ++i)
{ for (int j = 0; j< col; ++j)
{ if (j< (col - (h + 2 * i))) //推导公式画出空格数
{cout<< " ";
}
else
{cout<< "*";
}
}
cout<< endl;
}
}
return 0;
}
3.日期问题例题2.5 今年的第几天
#includeusing namespace std;
int dayTable[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
bool IsLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {int Y, M, D;
while (cin >>Y >>M >>D) {int number = 0;
int row = IsLeapYear(Y);
for (int j = 0; j< M; j++) {number += dayTable[row][j];
}
number += D;
cout<< number<< endl;
}
return 0;
}
例题2.6 打印日期
#includeusing namespace std;
int dayTable[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
bool IsLeapYear(int year) {return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {int Y, M, D;
int number = 0;
while (cin >>Y >>number) {M = 0;
int row = IsLeapYear(Y);
while (number >dayTable[row][M]) {number -= dayTable[row][M];
M++;
}
D = number;
printf("%04d-%02d-%02d\n", Y, M, D);
}
return 0;
}
4.其他模拟例题2.7 手机键盘
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
网站题目:数据结构考研学习笔记-创新互联
文章位置:http://cdiso.cn/article/dsjjsp.html