输入r,h,求圆柱体的表面积,保留3位小数
#include <stdio.h> #include <math.h> int main() { const double pi = 4.0 * atan(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 = %.3lf\n", s); return 0; }
%md %0md
#include <stdio.h> int main() { int n, m; scanf("%d", &n); m = (n%10)*100 + (n/10%10)*10 + n/100; printf("%03d\n", m); return 0; }
输出所有形如aabb的四位完全平方数
代码1:
#include <stdio.h> #include <math.h> int main() { int a, b, n; double m; for(a = 1; a <= 9; a++) for(b = 0; b <=9; b++) { n = a*1100 + b*11; m = sqrt(n); if(floor(m+0.5) == m) printf("%d\n", n); } return 0; }
代码2:
#include <stdio.h> int main() { int x, n, hi, lo; for(x = 1; ; x++) { n = x*x; if(n < 1000) continue; if(n > 9999) break; hi = n / 100; lo = n %100; if(hi/10 == hi%10 && lo/10 == lo %10) printf("%d\n", n); } return 0; }
计时函数的应用:
#include <stdio.h> #include <time.h> int main() { const int MOD = 1000000; int i, j, n, S = 0; scanf("%d", &n); for(i = 1; i <= n; i++) { int factorial = 1; for(j = 1; j <= i; j++) factorial = (factorial *j % MOD); S = (S + factorial) % MOD; } printf("%d\n", S); printf("Time used = %.2lf\n", (double)clock() / CLOCKS_PER_SEC); return 0; }
define用法:
#include <stdio.h> #define INF 100000000 int main() { int x, max = -INF, min = INF, count = 0, s = 0; while(scanf("%d", &x) == 1) { count++; s += x; if(x < min) min = x; if(x > max) max = x; } printf("%d %d %.3lf\n", min, max, (double)s/count); return 0; }
重定向的使用:
#define LOCAL #include <stdio.h> #define INF 100000000 int main() { #ifdef LOCAL freopen("g:\\in.txt", "r", stdin); freopen("g:\\out.txt", "w", stdout); #endif int x, n = 0, min = INF, max = -INF, s = 0; while(scanf("%d", &x) == 1) { s += x; if(x < min) min = x; if(x > max) max = x; n++; } printf("%d %d %.3lf\n", min, max, (double)s/n); return 0; }
64位整数:在gcc、VS2008中是%lld, 在win中是%I64d
保留指定位数的小数:
一。%nf 即输出的数字占n位 当原数字位数大于n时原样输出,原数字位数小于n时输出数字左端补上空格,比如原数字为a=1.23456;n为4时输出为1.23456,n为9时输出为
(空格空格1.23456)
二。%n.mf 即输出总共占n位其中有m位小数 如a=1.23456 用%4.2f输出为1.23如果用
%5,1f输出为123.4即长度为5小数为1!这里也有当原数字长度小于n时左端补空格这个规则!
还有就是当n前面有个负号时即%-nf或%-n.mf时就右端补空格!
#include <stdio.h> int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); printf("%.*lf\n", c, (double)a/b); return 0; }