算法入门经典001


printf("Hello, World!\n");
printf("%.1f\n",8.0/5.0);
printf("%.2f\n",8.0/5.0);
printf("%f\n",8.0/5.0);
printf("%.1f\n",8/5);
printf("%d\n",8.0/5.0);

Hello, World!
1.6
1.60
1.600000
1.6
4352



#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {

printf("%.8f\n",1+2*sqrt(3)/(5-0.1));


return 0;
}

1.70695951


sqrt(x)计算算术平方根


#include <stdio.h>

int main(int argc, const char * argv[]) {


int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",a+b);

return 0;
}

12
1
13
Program ended with exit code: 0


圆柱体的表面积



#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
const double pi = acos(-1.0);
double r, h, s1,s2,s;

scanf("%lf%lf",&r,&h);

s1 = pi*r*r;

s2 = 2*pi*r*h;

s = s1*2.0+s2;

printf("Area = %.3f\n",s);

return 0;
}



3.5
9
Area = 274.889
Program ended with exit code: 0





三位数反转

#include <stdio.h>


int main(int argc, const char * argv[]) {
int n;
scanf("%d",&n);

printf("%d%d%d\n",n%10,n/10%10,n/100);


return 0;
}

123
321
Program ended with exit code: 0


#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int n,m;


scanf("%d",&n);

m = (n%10)*100+(n/10%10)*10+(n/100);
printf("%03d\n",m);



return 0;
}


250
052
Program ended with exit code: 0


234
432
Program ended with exit code: 0



交换变量


#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int a,b,t;


scanf("%d%d",&a,&b);
t = a;
a = b;
b = t;

printf("%d %d\n",a,b);

return 0;
}


18 19
19 18
Program ended with exit code: 0


#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int a,b;
scanf("%d%d",&a,&b);

a = a + b;
b = a-b;
a = a-b;

printf("%d %d\n",a,b);

return 0;
}


10
20
20 10
Program ended with exit code: 0

#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int a,b;
scanf("%d%d",&a,&b);

printf("%d %d\n",b,a);

return 0;
}



20 10
10 20
Program ended with exit code: 0


keep it simple and stupid


鸡兔同笼


#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int a,b,n,m;

scanf("%d%d",&n,&m);

a = (4*n-m)/2;

b = n - a;


if (m % 2 == 1 || a< 0 || b < 0)
{
printf("No Answer\n");
}else{
printf("%d %d\n",a,b);

}



return 0;
}


14 32
12 2
Program ended with exit code: 0


10 16
No Answer
Program ended with exit code: 0


三正数排序

#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int a,b,c;

scanf("%d%d%d",&a,&b,&c);

if (a<b && b < c)
printf("%d %d %d\n",a,b,c);
if (a<c && c < b)
printf("%d %d %d\n",a,c,b);
if (b<a && a < c)
printf("%d %d %d\n",b,a,c);
if (b<c && c < a)
printf("%d %d %d\n",b,c,a);
if (c<a && a < b)
printf("%d %d %d\n",c,a,b);
if (c<b && b < a)
printf("%d %d %d\n",c,b,a);


return 0;
}


20 
3
8
3 8 20
Program ended with exit code: 0



#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int a,b,c;

scanf("%d%d%d",&a,&b,&c);

if (a<b && b < c)
printf("%d %d %d\n",a,b,c);
else if (a<c && c < b)
printf("%d %d %d\n",a,c,b);
else if (b<a && a < c)
printf("%d %d %d\n",b,a,c);
else if (b<c && c < a)
printf("%d %d %d\n",b,c,a);
else if (c<a && a < b)
printf("%d %d %d\n",c,a,b);
else
printf("%d %d %d\n",c,b,a);


return 0;
}


111
111
111
111 111 111
Program ended with exit code: 0


#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int a,b,c,t;

scanf("%d%d%d",&a,&b,&c);  //9 8 3

if (a>b)
{
t = a;
a = b;
b = t;
}// a<=b   8 9 3

if (a>c)
{
t = a;
a = c;
c = t;
} //a<=c,a<=b 3 9 8

if (b>c)
{
t = b;
b = c;
c = t;
} // 3 8 9

printf("%d %d %d\n",a,b,c);


return 0;
}


9
8
3
3 8 9
Program ended with exit code: 0


平均数

#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
int a,b,c;

scanf("%d%d%d",&a,&b,&c);
printf("%.3f\n",(a+b+c)/3.0);


return 0;
}


20
10
30
20.000
Program ended with exit code: 0


温度


#include <stdio.h>
#include <math.h>


int main(int argc, const char * argv[]) {
double f,c;

scanf("%lf",&f);

c = 5*(f-32)/9;

printf("%.3f\n",c);




return 0;
}



100
37.778
Program ended with exit code: 0


到51页






Loading Disqus comments...
Table of Contents