chapter1&&2

來源:互聯網
上載者:User

輸入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 100000000int 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 100000000int main(){#ifdef LOCALfreopen("g:\\in.txt", "r", stdin);freopen("g:\\out.txt", "w", stdout);#endifint 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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.