算法竞赛QQ群:210838572,一起进步吧!
例题代码
1-1 圆柱体的表面积
// 1-1
#include <stdio.h>
#include <math.h>
int main()
{
const double pi = acos(-1.0); // ¦Ð
double r, h, s1, s2, s;
scanf("%lf%lf", &r, &h); // C99
s1 = pi*r*r;
s2 = 2*pi*r*h;
s = s1*2 + s2;
printf("Ares = %.3f\n", s);
return 0;
}
1-2 三位数反转
// 1-2
#include <stdio.h>
int main()
{
int num;
scanf("%d", &num);
printf("%d%d%d\n", num%10, num/10%10, num/100);
return 0;
}
1-3 交换变量
// 1-3
#include <stdio.h>
int main()
{
int a, b, t;
scanf("%d%d", &a, &b);
t = a;
a = b;
b = t;
printf("%d %d\n", a, b);
return 0;
}
1-4 鸡兔同笼
// 1-4
#include <stdio.h>
int main()
{
int x, y, m, n;
scanf("%d%d", &n, &m);
y = (m-2*n)/2;
x = n-y;
if(m % 2 == 1 || x < 0 || y < 0) // ¼¦ÍþùΪÕûÊý£¬²¢ÇÒ¶¼´óÓÚ0
{
printf("No Answer\n");
}
else
{
printf("%d %d\n", x, y);
}
return 0;
}
1-5 三整数排序
// 1-5
#include <stdio.h>
void swap(int &x, int &y);
int main()
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(a > b){swap(a, b);}
if(a > c){swap(a, c);}
if(b > c){swap(b, c);}
printf("%d %d %d\n", a, b, c);
return 0;
}
void swap(int &x, int &y)
{
int t;
t = x;
x = y;
y = t;
return;
}
习题代码
t1-1 平均数(average)
// t1-1
#include <stdio.h>
int main()
{
double x, y, z, res;
scanf("%lf%lf%lf", &x, &y, &z);
res = (x+y+z)/3;
printf("%.3f\n", res);
return 0;
}
t1-2 温度(temperature)
// t1-2
#include <stdio.h>
int main()
{
double f, c;
scanf("%lf", &f);
c = 5*(f-32)/9;
printf("%.3f\n", c);
return 0;
}
t1-3 连续和(sum)
// t1-3
#include <stdio.h>
int main()
{
double n, res;
scanf("%lf", &n);
res = n*(n+1)/2;
printf("%.3f", res);
return 0;
}
t1-4 正弦和余弦(sin和cos)
// t1-4
#include <stdio.h>
#include <math.h>
int main()
{
const double pi = acos(-1.0);
double n, s, c;
scanf("%lf", &n);
n = n/180*pi;
s = sin(n);
c = cos(n);
printf("%f %f", s, c);
return 0;
}
t1-5 打折(discount)
// t1-5
#include <stdio.h>
int main()
{
const double PRICE = 95;
const double sale = 0.85;
double n, cost;
scanf("%lf", &n);
cost = PRICE*n;
if(cost >= 300)
{
printf("%.2f\n", cost*sale);
}
else
{
printf("%.2f\n", cost);
}
return 0;
}
t1-6 三角形(triangle)
// t1-6
#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(!(a+b > c)) {printf("not a triangle\n"); return 0;}
if(!(a+c > b)) {printf("not a triangle\n"); return 0;}
if(!(b+c > a)) {printf("not a triangle\n"); return 0;}
if(a*a+b*b==c*c || a*a+c*c==b*b || b*b+c*c==a*a)
{
printf("yes\n");
}
else
{
printf("no\n");
}
return 0;
}
t1-7 年份(year)
// t1-7
#include <stdio.h>
int main()
{
int year;
scanf("%d", &year);
if(year%4 == 0 && year%100 != 0)
{
printf("yes\n");
}
else
{
printf("no\n");
}
return 0;
}