zoukankan      html  css  js  c++  java
  • 啊哈!C语言课后参考答案下

    最近看到一本好评量很高的的C语言入门书,课本真的很好,入门的话。专业性没有那么强,但入门足够了!!好评!看着看着就想把这本书的题课后习题都写出来,最后就有了这个小结。可能有的不是最好,不那么专业,但主要是以初学者的思维角度去写。尽量让初学者通俗易懂。
    链接:https://pan.baidu.com/s/1nArPBm8nxCrj8awWQBglSw
    提取码:zlm8

    第五章

    //第六节
    /*请在4~100内验证哥德巴赫猜想,输出每一个偶数的所有可能的拆分方法。
    例如:
     4=2+2
     6=3+3    
     8=3+5    
     10=3+7=5+5    
     12=5+7    
     14=3+11=7+7   
     ……
    */
    
    #include <stdio.h>    
    #include <stdlib.h>    
    int main() {
       int k,a,b,i,fa,fb;
       for(k=4;k<=100;k=k+2){
           printf("%d",k);
           for(a=2;a<=k/2;a++){
               fa=0;//判断a是否为素数
               for(i=2;i<=a-1;i++){
                   if(a%i==0){
                       fa=1;
                       break;
                   }
               }
               if(fa==0){//如果a为素数
                   b=k-a;//判断b是否为质数
                   fb=0;
                   for(i=2;i<=b-1;i++){
                       if(b%i==0){
                           fb=1;
                           break;
                       }
                   }
                   if(fb==0){
                       printf("=");
                       printf("%d+%d",a,b);
                   }
               }
           }
           printf("
    ");
       }
       system("pause"); 
       return 0;
    }
    
    //第七节
    /*1、输入一个3位数,求这个数个位、十位和百位的数之和。例如, 输入782,输出17;输入156,输出12。*/
    #include <stdio.h>    
    #include <stdlib.h>    
    int main() {
       int a,i,j,k;
       scanf("%d",&a);
       i=a/100;
       j=(a/10)%10;
       k=a%10;
       printf("%d",i+j+k);
       system("pause"); 
       return 0;
    }
    /*2.输入一个n 位数,范围在1~99 999 999,求这个n位数每一位上的数之和。
    例如,输入12,输出3;输入234 510,输出15。*/
    #include <stdio.h>    
    #include <stdlib.h>    
    int main() {
       long int a,sum=0,i;
       scanf("%d",&a);
       i=a;
       while(i){
           sum=sum+(i%10);//通过循环将数字n的每一位都剥离下来,并存储到sum中
           i=i/10;
       }
       printf("%d",sum);
       system("pause"); 
       return 0;
    }
    
    //第八节
    /*用1~6这6个自然数组成一个三角形,并让这个三角形三条边上数字之和相等。例如,如图5-2所示的三角形中,三条边的值之和分别 为:5+3+4、4+2+6、5+1+6,都等于12。那么现在请你输出所有的可能。
    */
    #include <stdio.h>
    #include <stdlib.h>
    int main() {
        int a, b, c, ab, bc, ac,count=1;
        for (a = 1; a <= 6; a++)
        for (b = 1; b <= 6; b++)
        for (c = 1; c <= 6; c++)
        for (ab = 1; ab <= 6; ab++)
        for (bc = 1; bc <= 6; bc++)
        for (ac = 1; ac <= 6; ac++) {
            if (a != b && a != c && a != ab && a != bc && 
                a != ac && b != c && b != ab && b != bc && 
                b != ac && c != ab && c != bc && 
                c != ac && ab != bc && 
                ab != ac && bc != ac) {
                if (a + ab + b == a + ac + c && a + ab + b == b + bc + c) {
                    printf("这个三角形有%d
    ",count);
                    printf("  %d 
    ", a);
                    printf(" %d %d 
    ", ab, ac);
                    printf("%d %d %d
    ", b, bc, c);
                    count++;
                }
            }
        }
        system("pause");
        return 0;
    }
    
    
    //第九节
    /*如何生成一个1~20 000 000的随机数?*/
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    int main() {
        int a;
        srand((unsigned)time(NULL));
        a=rand()*1000;
        printf("%d",a);
        system("pause");
        return 0;
    }
    

    第六章

    //第六节
    //输入n个数,并将这n 个数按照从小到大或者从大到小的顺序输出。
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
        int i,j,t,n;
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        for(i=0;i<n-1;i++){
            for(j=i;j<n;j++){
                if(a[i]>a[j]){
                    t=a[i];a[i]=a[j];a[j]=t;
                }
            }
        }
        for(i=0;i<n;i++){
            printf("%d ",a[i]);
        }
        return 0;
    }
    

    第七章

    //第一节
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
        int a,b;
        char c;
        scanf("%d%c%d",&a,&c,&b);
        if(c=='+')
            printf("%d",a+b);
        if(c=='-')
            printf("%d",a-b);
        if(c=='*')
            printf("%d",a*b);
        if(c=='/')
            printf("%d",a/b);
        return 0;
    }
    
    //第二节
    getchar()读取一个字符,输入后等待用户按“Enter”键结束(带回 显)。
    getche()读取一个字符,输入后立即获取字符,不用按“Enter”键 结束(带回显)。
    getch()读取一个字符,输入后立即获取字符,不用按“Enter”键 来结束(不带回显)。
    
    //第六节
    //输入两个单词,然后按照字典序输出这两个单词。
    //strcmp(a, b)就是比较字符串a和字 符串b在字典中的顺序。
    //如果字符串a和字符串b完全相同,那么返回值为0。
    //如果字符串a在字典中比字符串b先出现,那么返回值小于0。
    //如果字符串a在字典中比字符串b后出现,那么返回值大于0。
    //举一个例子:假设a和b是两个字符数组,分别存储两个字符串,然 后把a和b按照字典序输出。
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
        char a[101],b[101];
        gets(a);
        gets(b);
        if(strcmp(a,b)<=0){
            puts(a);
            puts(b);
        }else{
            puts(b);
            puts(a);
        }
        return 0;
    }
    
    //第七节
    //输入5个单词,然后把这些单词按照字典序输出。
    //strcpy(a[i], a[j]);的意思就是把字符串a[j]的内容原封不动地复制到字符串a[i]中,从而替换掉字符串a[i]中原来的内容。
    
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
        char a[5][11],t[11];
        int i,j;
        for(i=0;i<=4;i++){
            gets(a[i]);
        }
        for(i=0;i<=3;i++){
            for(j=j+1;j<=4;j++){
                if(strcmp(a[i],a[j])>0){
                    strcpy(t,a[i]);
                    strcpy(a[i],a[j]);
                    strcpy(a[j],t);
                }
            }
        }
        for(i=0;i<=4;i++){
            puts(a[i]);
        }
        return 0;
    }
    
    欢迎查阅
  • 相关阅读:
    1021. Deepest Root (25)
    1013. Battle Over Cities (25)
    h5ai目录列表优化
    利用chrome调试手机网页
    跨域相关配置
    HttpClient服务端发送http请求
    滚动条样式优化(CSS3自定义滚动条样式 -webkit-scrollbar)
    javaScript复制粘贴
    效率工作
    spring boot实现文件上传下载
  • 原文地址:https://www.cnblogs.com/gh110/p/12158128.html
Copyright © 2011-2022 走看看