首页 > 其他分享 >C语言例题

C语言例题

时间:2023-01-12 22:55:06浏览次数:33  
标签:int 31 30 C语言 break printf 例题 day

C语言例题

  1. 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

    #include <stdio.h>
    #include <math.h>
    
    int main() {
        int x,i,y;
        for (int i = 0; i < 100000; ++i) {
            x=sqrt(i+100);
            y=sqrt(i+268);
            if (pow(x,2)==i+100&&pow(y,2)==i+268){
                printf("%d\n",i);
            }
        }
        return 0;
    }
    
    

  1. 题目:企业发放的奖金根据利润提成。

    • 利润(工)低于或等于10万元时,奖金可提10%;
    • 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
    • 20万到40万之间时,高于20万元的部分,可提成5%;#40万到60万之间时高于40万元的部分,可提成3%;
    • 60万到100万之间时,高于60万元的部分,可提成1.5%;#高于100万元时,超过100万元的部分按1%提成。

    从键盘输入当月利润I,求应发放奖金总数?

    #include <stdio.h>
    
    int main() {
        int bonus;
        float profit;
    
        printf("Please input the proft:");
        scanf("%f",&profit);
        if(profit<=100000){
            bonus=0.1*profit;}
        else if(profit>100000&&profit<200000){
            bonus=(profit-100000)*0.075+10000;
        }
        else if(profit>=200000&&profit<400000){
            bonus=(profit-200000)*0.05+10000+100000*0.075;
        }
        else if(profit>=400000&&profit<600000){
            bonus=(profit-400000)*0.03+200000*0.05+10000+100000*0.075;
        }
        else if(profit>=600000&&profit<=1000000){
            bonus=(profit-600000)*0.015+200000*0.05+10000+100000*0.075+200000*0.03;
        }
        else if(profit>1000000){
            bonus=(profit-1000000)*0.01+200000*0.05+10000+100000*0.075+200000*0.03+400000*0.015;
        }
    
        printf("bonus=%d",bonus);
        return 0;
    }
    
    

  1. 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

    #include <stdio.h>
    
    int main() {
        int i,j,k;
        int s=0;
        for (int i = 1; i <5 ; i++) {
            for (int j = 1; j <5 ; j++) {
                for (int k = 1; k <5 ; k++) {
                    if(i!=j&&i!=k&&k!=j){
                        s++;
                        printf("%d%d%d\n",i,j,k);
                    }
                }
            }
        }
        printf("%d",s);
        return 0;
    }
    
    

  1. 题目:输入某年某月某日,判断这一天是这一年的第几天?

    #include <stdio.h>
    
    int main() {
        int year,month,day,a,s;
        printf("Please input the year month and day:");
        scanf("%d,%d,%d",&year,&month,&day);
        if(year%4==0&&year%100!=0){
            switch (month){
                case 1:{printf("Today is the %d day",day);
                }break;
                case 2:{printf("Today is the %d day",day+31);
                }break;
                case 3:{printf("Today is the %d day.",day+31+29);
                }break;
                case 4:{printf("Today is the %d day.",day+31+29+31);
                }break;
                case 5:{printf("Today is the %d day.",day+31+29+31+30);
                }break;
                case 6:{printf("Today is the %d day.",day+31+29+31+30+31);
                }break;
                case 7:{printf("Today is the %d day.",day+31+29+31+30+31+30);
                }break;
                case 8:{printf("Today is the %d day.",day+31+29+31+30+31+30+31);
                }break;
                case 9:{printf("Today is the %d day.",day+31+29+31+30+31+30+31+31);
                }break;
                case 10:{printf("Today is the %d day.",day+31+29+31+30+31+30+31+31+30);
                }break;
                case 11:{printf("Today is the %d day.",day+31+29+31+30+31+30+31+31+30+31);
                }break;
                case 12:{printf("Today is the %d day.",day+31+29+31+30+31+30+31+31+30+31+30);
                }break;
            }
        } else{
            switch (month){
                case 1:{printf("Today is the %d day",day);
                }break;
                case 2:{printf("Today is the %d day",day+31);
                }break;
                case 3:{printf("Today is the %d day.",day+31+28);
                }break;
                case 4:{printf("Today is the %d day.",day+31+29+31);
                }break;
                case 5:{printf("Today is the %d day.",day+31+29+31+30);
                }break;
                case 6:{printf("Today is the %d day.",day+31+29+31+30+31);
                }break;
                case 7:{printf("Today is the %d day.",day+31+29+31+30+31+30);
                }break;
                case 8:{printf("Today is the %d day.",day+31+29+31+30+31+30+31);
                }break;
                case 9:{printf("Today is the %d day.",day+31+29+31+30+31+30+31+31);
                }break;
                case 10:{printf("Today is the %d day.",day+31+29+31+30+31+30+31+31+30);
                }break;
                case 11:{printf("Today is the %d day.",day+31+29+31+30+31+30+31+31+30+31);
                }break;
                case 12:{printf("Today is the %d day.",day+31+29+31+30+31+30+31+31+30+31+30);
                }break;
            }
        }
        return 0;
    }
    
    

    今天又是废话文学的一天...T-T


  1. 题目:输入三个整数x, y,z,请把这三个数由小到大输出。

    #include <stdio.h>
    
    int main() {
        int x,y,z;
        printf("Please input three numbers:");
        scanf("%d,%d,%d",&x,&y,&z);
        if(x<=y&&x<=z){
            if(y<=z){
                printf("%d,%d,%d",x,y,z);
            } else{
                printf("%d,%d,%d",x,z,y);
            }
        }
        if(y<=x&&y<=z){
            if(x<=z){
                printf("%d,%d,%d",y,x,z);
            } else{
                printf("%d,%d,%d",y,z,x);
            }
        }
        if(z<=x&&z<=y){
            if(x<=y){
                printf("%d,%d,%d",z,x,y);
            } else{
                printf("%d,%d,%d",z,y,x);
            }
        }
        return 0;
    }
    
    

  1. 题目:用*号输出字母C的图案。

    #include <stdio.h>
    
    int main() {
        printf("*********\n");
        printf("*\n");
        printf("*\n");
        printf("*\n");
        printf("*\n");
        printf("*********");
        return 0;
    }
    
    

  1. 题目:输出9+9口诀。

    #include <stdio.h>
    
    int main() {
        int i,j;
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j <=i ; j++) {
                printf("%d*%d=%d ",j,i,i*j);
            }
            printf("\n");
        }
        return 0;
    }
    
    

  1. 题目:古典问题(兔子生惠)∶有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?(输出前40个月即可)

    #include <stdio.h>
    int Fib(int n){
        if(n<=2){
            return 1;
        } else{
            return Fib(n-1)+Fib(n-2);
        }
    }
    int main() {
        int n,i;
        printf("Please input a number:");
        scanf("%d",&n);
        for (int i = 1; i<=n; i++) {
            printf("%d\n",Fib(i)*2);
        }
        return 0;
    }
    
    

  1. 题目:判断101到200之间的素数。

    #include <stdio.h>
    
    int main() {
        int i,j;
        for (int i = 101; i <200 ; i++) {
            for (int j = 2; j <i ; j++) {
                if (i%j==0){
                    break;
            }
                if(j>=i/2){
                    printf("%d\n",i);
                    break;
            }
                
            }
        }
        return 0;
    }
    
    

    当结果出现多个重复值时可在循环后加入break;


  1. 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
#include <stdio.h>
#include <math.h>

int main() {
    int i,j,k;

    for (int i = 100; i < 1000; i++) {
        j=i/10;
        if ((pow(i/100,3)+pow(j%10,3)+pow(i%10,3))==i){
            printf("%d\n",i);

        }
    }
    return 0;
}


  1. 题目:将一个正整数分解质因数。例如:输入90,打印出90=233*5。

    #include <stdio.h>
    
    int main() {
        int i,x;
        printf("Please input a number:");
        scanf("%d",&x);
        printf("%d=",x);
        for (int i =2; i <=x ; i++) {
            while (x%i==0){
                printf("%d",i);
                x=x/i;
                if(x!=1){
                    printf("*");
        
                }
            }
        }
        return 0;
    }
    
    

  1. 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示.

    #include <stdio.h>
    
    int main() {
        int score;
        printf("Please input the score:");
        scanf("%d",&score);
        if (score>=90){
            printf("A");
        } else{
            (score<60)?printf("C"):printf("B");
        }
        return 0;
    }
    

  1. 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

    #include <stdio.h>
    
    int main() {
        int m,n,i,j;
        int s=1;
        printf("Please input m and n:");
        scanf("%d,%d",&m,&n);
        //最大公约数:共有质因数的乘积
        //最小公倍数:两个数的乘积/最大公约数
        for (int i = 2; i <=m ; i++) {
            for (int j = 2; j <=n ; j++) {
                if ((m%i==0)&&(n%j==0)){
                    while (i==j){
                        s=s*i;
                        break;  //如果不加则一直在while里循环
                    }
                }
    
            }
        }
        printf("the greatest common divisor is %d\n",s);
        printf("the least common multiple is %d",m*n/s);
        return 0;
    }
    
    

  1. 题目:输入一行字符,分列统计出其中英文字母、空格、数字和其它字符的个数。

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main() {
        int i;
        int s1=0;
        int s2=0;
        int s3=0;
        int s4=0;
        char a[100];
        printf("Please input chars:");
        gets(a);
        for (int i = 0; i <strlen(a) ; i++) {
            if (isalpha(a[i])){
                s1++;
            }
            else if(a[i]==' '){
                s2++;
            }
            else if(isdigit(a[i])){
                s3++;
            } else{
                s4++;
            }
        }
        printf("the alpha has %d\n",s1);
        printf("the blanket has %d\n",s2);
        printf("the number has %d\n",s3);
        printf("the other has %d",s4);
        return 0;
    }
    
    

  1. 题目:求s=a+aa+aaa+aaaa+aa. ..a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
#include <stdio.h>
#include <math.h>
    
    int main() {
        int n,i,a;
        int sum=0;
        printf("Please input a number:");
        scanf("%d",&n);
        for (int i = 1; i <=n ; i++) {
            a+=pow(10,(i-1))*2;
            sum+=a;
        }
        printf("%d",sum);
        return 0;
    }
    

  1. 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6李1+2+3.编程找出10日日以内的所有完数。

    #include <stdio.h>
    
    int main() {
        int i, j, sum;
        for (i = 1; i <= 1000; i++) {   //或者i,j都从2开始sum从1开始
            sum = 0;    
          //必须在for循环中赋值,如果在for循环外面赋值则每次外循环开始都变成0
          //每一个i对应一个sum值因此需要在i循环内重新让sum=0
            for (j = 1; j <= i / 2; j++) {
                if (i % j == 0)
                    sum += j;
            }
            if (sum == i) {
                printf("%d \n", i);
            }
        }
        return 0;
    }
    
    

  1. 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下.求它在第10次落地时,共经过多少米?第10次反弹多高?

    #include <stdio.h>
    
    int main() {
        float h=100;
        int n,i;
        float sum=100;
        for (int i = 1; i <=10 ; i++) {
            h = h / 2.0;
            sum += h;
            if(i==10){
                printf("The tenth height is %f\n", h);
            }
        }
        printf("sum=%f", sum);
        return 0;
    }
    
    

标签:int,31,30,C语言,break,printf,例题,day
From: https://www.cnblogs.com/yuanyu610/p/17048196.html

相关文章

  • 《数据结构 - C语言》单链表
    目录结构定义初始化建立清空求表长判断是否为空表取值查找插入删除销毁遍历打印测试结构定义#include<stdio.h>#include<malloc.h>#include<stdlib.h>#defineOK......
  • 初识C语言
    1、对编程而言,可移植性意味着什么?在一种系统中编写的C程序稍作修改或不修改就可以在其他系统运行。2、编程的七个主要步骤是什么?定义程序的目标设计程序编写程序编......
  • c语言根据成员变量地址获取结构体的地址
    目录参考链接正文参考链接正文C语言中,根据成员变量地址获取结构体的地址。有一种实现方法:member_address-&(((TYPE*)0)->member);这个里面最让人疑惑是&(((TYPE*)0......
  • C语言公司考勤系统[2023-01-12]
    C语言公司考勤系统[2023-01-12]1.题目:公司考勤系统考勤系统是公司人事管理重要环节,用于记录员工迟到、早退、缺席、请假等出勤情况,并能提供数据统计功能。系统需求如下:......
  • C语言十进制转2进制数
    #include<stdio.h>#include<math.h>longtoBinary(intnum);intmain(intargc,charconst*argv[]){ intnum; printf("Pleaseinputanumber:"); scanf("%......
  • C语言指针统览
    前言本文对C语言指针和指针使用时的问题做一个概览性的总结,并对一些值得探讨的问题进行讨论。阅读本文,读者能达到统览C语言指针的目的。以下的讨论只针对32/64位机器。指针......
  • c语言实现三子棋
    前言:在此之前我们学习了循环,函数,数组等相关知识,我们来写一个小游戏练练手概述:代码大致分为三部分程序主函数,函数,声明函数(这一点我们在通讯录项目是就介绍过了,将代码分为三部......
  • 哪些软件和程序用了C语言?
    在日常生活中,很多系统软件和桌面应用程序都采用C语言进行开发,下面给出了一些示例。1.操作系统UNIX是第一个使用**语言设计的操作系统,它使用的编程语言就是C语言。后来,Mic......
  • 一个C语言的剪刀石头布小游戏
    /******************************************************石头剪刀布的程序geek_monkey于2015年3月3日修改了bug(输入字符非石头剪刀布都算是玩家赢)编译环境为VC+......
  • C语言学生成绩录入系统
    C语言学生成绩录入系统学生成绩录入系统录入10名学生的学号,姓名,及3门课程(高数、马克思、C语言)的平时成绩与考试成绩,3门课程的比例如下:高数:总成绩=平时成绩30%+考试成......