算法競賽入門經(jīng)典
習題1-1平均數(shù)
#include<stdio.h>
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<stdio.h>
int main() {
float f, c;
scanf("%f", &f);
c = 5 * (f - 32) / 9;
printf("%.3f", c);
}
習題1-3
#pragma warning(disable:4996)
#include<stdio.h>
int main() {
int n, sum;
scanf("%d", &n);
sum = (1 + n) * n / 2;
printf("%d", sum);
}
習題1-4
#include<stdio.h>
#include<math.h>
#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<stdio.h>
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<stdio.h>
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<stdio.h>
int main() {
int year;
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) ||(year % 400 == 0)) {
printf("yes");
}
else {
printf("no");
}
}
習題2-1水仙花數(shù)
#pragma warning(disable:4996)
#include<stdio.h>
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<stdio.h>
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<stdio.h>
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<stdio.h>
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分數(shù)化小數(shù)
#pragma warning(disable:4996)
#include<stdio.h>
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<stdio.h>
#include<iostream>
#include<set>
using namespace std;
int main() {
int a, b, c;
for (int i = 123; i < 329; i++) {
set <int> s;//使用set中元素不可重復這一性質(zhì)
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蛇形填數(shù)
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#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;
}
循環(huán)單詞
#pragma warning(disable:4996)
#include <string.h>
#include <cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<math.h>
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;
}