liangsdu avatar liangsdu committed e5b1983

C语言程序设计练习一二三

Comments (0)

Files changed (21)

+/** HDOJ 2000: ASCII码排序 	**/
+
+#include <stdio.h>
+void swap(char *a, char *b)
+{
+    if(*a > *b)
+    {
+        char temp = *a;
+        *a = *b;
+        *b = temp;
+    }
+}
+
+int main(int argc, char **argv)
+{
+    char a, b, c, temp;
+    while(scanf("%c %c %c", &a, &b, &c) != EOF)
+    {
+        if(a>b) swap(&a, &b);
+        if(b>c) swap(&b, &c);
+        if(a>b) swap(&a, &b);
+            printf("%c %c %c\n", a, b, c);
+            getchar();
+    }
+
+    return 0;
+}
+/** HDOJ 2001:计算两点间距离 	**/
+
+#include <stdio.h>
+#include <math.h>
+int main(int argc, char **argv)
+{
+    float x1, y1, x2, y2;
+    while(scanf("%f %f %f %f", &x1, &y1, &x2, &y2) != EOF)
+    {
+      float distance = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
+      printf("%.2f\n", distance);
+    }
+    return 0;
+}
+/** HDOJ 2002: 计算球体积 **/
+#include <stdio.h>
+#include <math.h>
+#define PI 3.1415927
+int main(int argc, char **argv)
+{
+    double circle;
+    while(scanf("%lf", &circle) != EOF)
+    {
+        printf("%.3lf\n", 4*PI*pow(circle, 3)/3);
+    }
+    return 0;
+}
+/** HDOJ 2003: 求实数的绝对值 	**/
+
+#include <stdio.h>
+#include <math.h>
+int main(int argc, char **argv)
+{
+    double n;
+    while(scanf("%lf", &n) != EOF)
+        printf("%.2lf\n", fabs(n));
+
+    return 0;
+}
+/** HDOJ 2004: 成绩转换 	**/
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int score;
+    while(scanf("%d", &score) != EOF)
+    {
+        if(score >= 90 && score <= 100)
+            printf("A\n");
+        else if(score >=80 && score <=89)
+            printf("B\n");
+        else if(score >=70 && score <=79)
+            printf("C\n");
+        else if(score >=60 && score <=69)
+            printf("D\n");
+        else if(score >=0 && score <=59)
+            printf("E\n");
+        else
+            printf("Score is error!\n");
+    }
+    return 0;
+}
+/** HDOJ 2005: 第几天?
+给定一个日期,输出该日期是该年的第几天?
+日期如YYYY/MM/DD所示:1985/1/20 2006/3/12
+**/
+#include <stdio.h>
+int is_leap(int n)
+{
+    if((n%400 == 0) || ((n%4 == 0) && (n%100 !=0)))
+        return 1;
+    else
+        return 0;
+}
+
+int main(int argc, char **argv)
+{
+    int myMonth[2][13] = {{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
+                        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
+    };
+
+    int year, month, day;
+    char a, b;
+    while(scanf("%d%c%d%c%d", &year, &a, &month, &b, &day) != EOF)
+    {
+        int sum = 0;
+        int i;
+        int flag = is_leap(year);
+        for(i=1; i<month; i++)
+            sum = sum + myMonth[flag][i];
+        sum = sum + day;
+        printf("%d\n", sum);
+    }
+    return 0;
+}
+/** HDOJ 2006: 奇数乘积 	**/
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int n;
+    while(scanf("%d", &n) != EOF)
+    {
+        int sum = 1;
+        int temp;
+        int i;
+        for(i=0; i<n; i++)
+        {
+            scanf("%d", &temp);
+            if(temp%2 != 0)
+                sum = sum*temp;
+        }
+        printf("%d\n", sum);
+    }
+    return 0;
+}
+/** HDOJ 2007: 立方和和平方和
+给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
+**/
+
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int start, end;
+    while(scanf("%d %d", &start, &end) != EOF)
+    {
+        int i;
+        long odd_sum = 0;    /** 奇数 **/
+        long even_sum = 0;
+        if(start > end)
+        {   /** 不用变量交换2个数 **/
+            start = start + end;
+            end = start - end;
+            start = start - end;
+        }
+        for(i=start; i<=end; i++)
+        {
+            if(i%2 == 0)
+                even_sum += i*i;
+            else
+                odd_sum += i*i*i;
+        }
+        printf("%ld %ld\n", even_sum, odd_sum);
+    }
+
+    return 0;
+}
+/** HDOJ 2008: 统计给定的n个数中,负数、零和正数的个数
+note: 输入为N个实数
+**/
+
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int n;
+    while(scanf("%d", &n) != EOF && n != 0)
+    {
+        int j;
+        int negative=0;
+        int zero=0;
+        int positive=0;
+        double temp;
+        for(j=0; j<n; j++)
+        {
+            scanf("%lf", &temp);
+            if(temp > 0)
+                positive++;
+            else if(temp == 0)      /** fabs(temp) < DBL_EPSILON **/
+                zero++;
+            else negative++;
+        }
+        printf("%d %d %d\n", negative, zero, positive);
+    }
+    return 0;
+}
+/** HDOJ 2009: 求数列的和	**/
+
+#include <stdio.h>
+#include <math.h>
+int main(int argc, char **argv)
+{
+    int n, m;
+    while(scanf("%d %d", &n, &m) != EOF)
+    {
+        int i;
+        double sum = 0.0;
+        double temp = n;
+        for(i=0; i<m; i++)
+        {
+            sum = sum + temp;
+            temp = sqrt(temp);
+        }
+        printf("%.2lf\n", sum);
+    }
+    return 0;
+
+}
+/** HDOJ 2010: 输出n->m之间的水仙花数
+见过个查表的实现 100-999的水仙花数有153, 370, 371, 407
+退格删空格不能AC
+**/
+
+#include <stdio.h>
+int narcissus_number(int num)
+{
+    int a = num / 100;
+    int b = (num / 10) % 10; /** int b = num % 100 / 10 **/
+    int c = num % 10;
+    if( num == (a*a*a + b*b*b + c*c*c))
+        return 1;
+    else
+        return 0;
+}
+
+int main(int argc, char **argv)
+{
+    int m, n;
+    int i;
+    while(scanf("%d %d", &m, &n) != EOF)
+    {
+        int flag = 0;
+        int first_num = 1;
+        for(i=m; i<=n; i++)
+            if(narcissus_number(i))
+            {
+                if(first_num)
+                {
+                    printf("%d", i);
+                    first_num = 0;
+                }
+                else
+                    printf(" %d", i);
+                flag = 1;
+            }
+
+
+        if(flag == 0)
+            printf("no");
+
+        printf("\n");
+    }
+    return 0;
+}
+/** HDOJ 2011: 多项式求和	**/
+
+#include <stdio.h>
+#include <math.h>
+int main(int argc, char **argv)
+{
+    int m, n;
+    int i;
+    int array[100];
+    scanf("%d", &m);    /** m个实例 **/
+    for(i=0; i<m; i++)
+        scanf("%d", &array[i]);      /** 存放于array[0] -> array[m-1] **/
+
+    /** 计算部分 **/
+    for(i=0; i<m; i++)
+    {
+        double sum = 0.0;
+        int flag = 1;   /** 符号 **/
+        int j;
+        for(j=0; j<array[i]; j++)
+        {
+            sum = sum + flag * 1.0 / (j + 1);
+            flag = -flag;
+        }
+        printf("%.2lf\n", sum);
+    }
+    return 0;
+}
+/** HDOJ 2012: 素数判定 	**/
+#include <stdio.h>
+
+/** n是素数的话返回1,否则返回0 **/
+int is_prime(int n)
+{
+    int i;
+    for(i=2; i<=n/2; i++)
+        if(n%i == 0)
+            return 0;
+    return 1;
+}
+
+int main(int argc, char **argv)
+{
+    int x, y;
+    while((scanf("%d %d", &x, &y) != EOF) && (x || y))  /**格式控制**/
+    {
+        int i;
+        int flag = 1;
+        for(i=x; i<=y; i++)
+            if(is_prime(i*i+i+41) == 0)
+            {
+                flag = 0;
+                break;
+            }
+        if(flag)
+            printf("OK\n");
+        else
+            printf("Sorry\n");
+    }
+    return 0;
+}
+/** HDOJ 2013: 蟠桃记	**/
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int n;
+    while(scanf("%d", &n) != EOF)
+    {
+        int i;
+        int sum = 1;
+        for(i=1; i<n; i++)
+            sum = (sum + 1) * 2;
+        printf("%d\n", sum);
+    }
+    return 0;
+}
+/** HDOJ 2014: 青年歌手大赛_评委会打分 	**/
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int n;
+    while(scanf("%d", &n) !=EOF)
+    {
+        int i;
+        int score;
+        int max = -1;
+        int min = 101;
+        double sum=0;
+        for(i=0; i<n; i++)
+        {
+            scanf("%d", &score);
+            if(score > max)
+                max = score;
+            if(score < min)
+                min = score;
+            sum += score;
+        }
+        printf("%.2lf\n", (sum-max-min)/(n-2));
+    }
+    return 0;
+}
+/** HDOJ 2015: 偶数求和 	**/
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int n, m;
+    while(scanf("%d %d", &n, &m) != EOF)
+    {
+        int i;
+        int sum=0;
+        int count=0;
+        int num=2;
+        for(i=0; i<n; i++)
+        {
+            sum = sum + num;
+            num = num + 2;
+            count++;
+            if(count == m && i != n-1 )
+            {
+                printf("%d ", sum/count);
+                count = 0;
+                sum = 0;
+            }
+            else if( i == n-1)
+            {
+                printf("%d", sum/count);
+            }
+        }
+        printf("\n");
+    }
+    return 0;
+}
+/** HDOJ 2016: 数据的交换输出 	**/
+#include <stdio.h>
+#define N 100
+int main(int argc, char **argv)
+{
+    int arr[N];
+    int n;
+    while(scanf("%d", &n) != EOF && (n != 0) )
+    {
+        int min=0;
+        int i, temp;
+        for(i=0; i<n; i++)
+            scanf("%d", &arr[i]);
+        for(i=1; i<n; i++)
+            if(arr[min] > arr[i])
+                min = i;
+
+        /** 交换 **/
+        temp = arr[0];
+        arr[0] = arr[min];
+        arr[min] = temp;
+
+        for(i=0; i<n; i++)
+            if(i==0)
+                printf("%d", arr[i]);
+            else
+                printf(" %d",arr[i]);
+        printf("\n");
+    }
+    return 0;
+}
+/** HDOJ 2017: 字符串统计	**/
+#include <stdio.h>
+#include <ctype.h>      /* isdigit() function */
+#include <string.h>
+int main(int argc, char **argv)
+{
+    int n;
+    scanf("%d", &n);
+    int i, j, len;
+    for(i=0; i<n; i++)
+    {
+        int count=0;
+        char str[81];
+        scanf("%s", str);
+        len = strlen(str);
+        for(j=0; j<len; j++)
+            if(isdigit(str[j]))
+                count++;
+        printf("%d\n", count);
+    }
+
+    return 0;
+}
+/** HDOJ 2018: 母牛的故事 **/
+#include <stdio.h>
+
+int main(int argc, char **argv)
+{
+    int a[100];
+    int i, n;
+    a[1]=1;
+    a[2]=2;
+    a[3]=3;
+    a[4]=4;
+    for(i=5; i<100; i++)
+        a[i]=a[i-1] + a[i-3];
+    while(scanf("%d", &n) && (n != 0) )
+        printf("%d\n", a[n]);
+
+    return 0;
+}
+/** HDOJ 2019: 数列有序 	**/
+
+#include <stdio.h>
+#define N 100
+int main(int argc, char **argv)
+{
+    int n, m;
+    int a[N];
+    while((scanf("%d %d", &n, &m) != EOF) && (n && m))
+    {
+        int i, id;
+        for(i=0; i<n; i++)
+            scanf("%d", &a[i]);
+        for(i=0; i<n; i++)
+            if(a[i] > m)
+            {
+                id = i;
+                break;
+            }
+        for(i=0; i<id; i++)
+            printf("%d ", a[i]);
+        printf("%d ", m);
+        for(i=id; i<n; i++)
+            if(i == n-1)
+                printf("%d\n", a[i]);
+            else
+                printf("%d ", a[i]);
+    }
+    return 0;
+}
+/** HDOJ 2020: 绝对值排序 	**/
+#include <stdio.h>
+#define N 100
+void sort(int a[], int n)
+{
+    int i, j, temp;
+    for(i=0; i<n; i++)
+        for(j=i+1; j<n; j++)
+            if(abs(a[i]) < abs(a[j]))
+            {
+                temp = a[i];
+                a[i] = a[j];
+                a[j] = temp;
+            }
+}
+
+int main(int argc, char **argv)
+{
+    int n;
+    int a[N];
+    while(scanf("%d", &n) != EOF && n)
+    {
+        int i;
+        for(i=0; i<n; i++)
+            scanf("%d", &a[i]);
+        sort(a, n);
+        for(i=0; i<n; i++)
+            if(i == n-1)
+                printf("%d\n", a[i]);
+            else
+                printf("%d ", a[i]);
+    }
+    return 0;
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.