算法竞赛入门经典
习题1-1平均数
#include
int main() {
int a, b, c, sum;
float average;
scanf("%d%d%d", &a, &b, &c);
sum = a + b + c;
average = sum / 3;
printf("%.3f \n", average);
return 0;
}
习题1-2温度
#pragma warning(disable:4996)
#include
int main() {
float f, c;
scanf("%f", &f);
c = 5 * (f - 32) / 9;
printf("%.3f", c);
}
习题1-3
#pragma warning(disable:4996)
#include
int main() {
int n, sum;
scanf("%d", &n);
sum = (1 + n) * n / 2;
printf("%d", sum);
}
习题1-4
#include
#include
#define PI 3.14159265
int main() {
double n, sinn = 0, cosn = 0;
double m, val;
//printf("%f", n);
scanf("%lf", &n);
val = PI / 180;
sinn = sin(n * val);
cosn = cos(n * val);
printf("%.3f %.3f\n", sinn, cosn);
return 0;
}
习题1-5
#include
int main() {
int price = 95, n = 0;
double sum;
scanf("%d", &n);
sum = n * price;
if (sum >= 300) {
sum = sum * 0.85;
printf("%.2f", sum);
}
return 0;
}
习题1-6
#pragma warning(disable:4996)
#include
int main() {
int a, b, c;
int a2, b2, c2;
scanf("%d%d%d", &a, &b, &c);
a2 = a * a;
b2 = b * b;
c2 = c * c;
if ((a + b > c) && (a + c > b) && (b + c > a)) {
if ((a2 + b2 == c2) || (a2 + c2 == b2) || (b2 + c2 == a2)) {
printf("yes");
}
else {
printf("no");
}
}
else {
printf("not a triangle");
}
}
习题1-7
#pragma warning(disable:4996)
#include
int main() {
int year;
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) ||(year % 400 == 0)) {
printf("yes");
}
else {
printf("no");
}
}
习题2-1水仙花数
#pragma warning(disable:4996)
#include
int main() {
int i = 100;
for (i = 100; i <= 999; i++) {
int a = 0, b = 0, c = 0;
a = i % 10;
b = (i /10)% 10;
c = i /100;
if ((a * a*a + b * b*b + c * c *c== i)) {
printf("%d\n", i);
}
}
}
习题2-2韩信点兵
#pragma warning(disable:4996)
#include
int main() {
int i = 10;
int a, b, c, kase = 0;
while (scanf("%d%d%d", &a, &b, &c) == 3) {
kase =kase+1;
int m = -1;
for (i = 10; i <= 100; i++) {
if (i % 3 == a && i % 5 == b && i % 7 == c) {
m = i;
break;
}
}
if (m != -1) {
printf("case %d: %d\n", kase, m);
}
else {
printf("case %d: No answer\n",kase);
}
}
}
习题2-3倒三角形
#pragma warning(disable:4996)
#include
int main() {
int n = 0;
scanf("%d", &n);
for (int i = n; i > 0; i--) {//按行输入
for (int j = n - i; j > 0; j--) {//输出空格
printf(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
printf("#");//输出#
}
printf("\n");//换行
}
}
习题2-4子序列的和
#pragma warning(disable:4996)
#include
int main() {
int kase = 0;
int n=1,m;
while (scanf("%d%d", &n, &m) == 2 && n != 0) {
kase += 1;
double num = 0;
for (long long i = n; i <=m; i++) {
num += 1.0 / (i * i);
}
printf("Case %d: %.5f", kase, num);
}
}
习题2-5分数化小数
#pragma warning(disable:4996)
#include
int main() {
int kase = 0;
int a, b, c;
double num = 0;
while (scanf("%d%d%d", &a, &b, &c ) == 3 && a != 0) {
kase += 1;
num = (a*1.0) / b;
printf("Case %d: %.*f", kase,c,num );
//
}
return 0;
}
习题2-6排列
#pragma warning(disable:4996)
#include
#include
#include
using namespace std;
int main() {
int a, b, c;
for (int i = 123; i < 329; i++) {
set s;//使用set中元素不可重复这一性质
int a1 , a2 , a3 ,b1 , b2 , b3, c1 , c2 , c3 ;
a = i;
b = i * 2;
c = i * 3;
a1 = a % 10;
a2 = a / 10 % 10;
a3 = a / 100;
b1 = b % 10;
b2 = b / 10 % 10;
b3 = b /100;
c1 = c % 10;
c2 = c / 10 % 10;
c3 = c / 100;
s.insert(a1);
s.insert(a2);
s.insert(a3);
s.insert(b1);
s.insert(b2);
s.insert(b3);
s.insert(c1);
s.insert(c2);
s.insert(c3);
if ((b<=987&&c<=987)&&s.size()==9&&s.count(0)==0) {//需要判断是否存在0
printf("%d %d %d\n", a, b, c);
}
}
return 0;
}
例3-3蛇形填数
#include
#include
#include
#define max 250
int a[max][max];
int main(){
int n, x, y;
int tot = 0;
scanf("%d", &n);
memset(a, 0, sizeof(a));
tot = a[x = 0][y = n - 1] = 1;//使得第一个元素为1;
while (tot < n * n) {
while (x + 1 < n && !a[x + 1][y]) {
x = x + 1;//x递增1
tot = tot + 1;
a[x][y] = tot;
}
while (y - 1 >= 0 && !a[x][y - 1]) {
y = y - 1;
tot = tot + 1;
a[x][y] = tot;
}
while (x - 1 >= 0 && !a[x - 1][y]) {
x = x - 1;
tot = tot + 1;
a[x][y] = tot;
}
while (y + 1 < n && !a[x][y + 1])
{
y = y + 1;
tot = tot + 1;
a[x][y] = tot;
}
}
for (x = 0; x < n; x++) {
for (y = 0; y < n; y++) {
printf("%3d", a[x][y]);
}
printf("\n");
}
return 0;
}
循环单词
#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include
using namespace std;
#define max 100
int getAnswer(string s1,string s2){
for (int i = 0; i< s1.size(); i++) {
for (int j = 0; j< s2.size(); j++) {
if (s1[i] == s2[j] && j == s2.size() - 1) {
return 1;
}
else {
continue;
}
}
}
return 0;
}
int main()
{
int a;
cin >> a;
int flag[max] = { 0 };
char s[10][30];
for (int i = 0; i < a; i++) {
scanf("%s", s[i]);
}
int count = 0;
for (int j = 0; j < a; j++) {
for (int m = j + 1; m < a; m++) {
if (strlen(s[j]) != strlen(s[m])) {
break;
}
else {
string s1;
string s2;
string s3;
s1 = s[j];
s2 = s[m];
s3 = s1 + s1;
int f;
f = getAnswer(s3, s1);
if (flag[j]==0) {
count += f;
}
//这两行用去去重,确定共有几个单词模块
flag[m] = f;
flag[j] = f;
}
}
}
cout << count << endl;
return 0;
}