当前位置:网站首页>C language applet -- common classic practice questions
C language applet -- common classic practice questions
2022-07-31 01:46:00 【south wind fahaxiki】
Share 5 simple C language small programs, now you, do you still remember the C language you studied hard at the time, the classic C language questions, recall it
[Program 1] Question: With 1, 2, 3, and 4 numbers, how many different three-digit numbers can be formed without repeating numbers?How much are they?
1. Program analysis: The numbers that can be filled in the hundreds, tens, and ones are 1, 2, 3, and 4.Make up all the permutations and then remove the permutations that do not meet the conditions.
2. Program source code:
#include int main(){int i,j,k;printf("\n");for(i=1;i<5;i++) /*The following is a triple loop*/for(j=1;j<5;j++)for (k=1;k<5;k++){if (i!=k&&i!=j&&j!=k) /*make sure i,j,k are different from each other*/printf("%d,%d,%d\n",i,j,k);}}
【Program 2】
Question: Enter a certain day, a certain month, a certain year, and determine the day of the year?
1. Program analysis: Take March 5th as an example, you should add up the first two months, and then add 5 days, which is the first day of the year, special cases, leap years and the input month is greater than 3need to consider an extra day.
2. Program source code:
#include int main(){int day,month,year,sum,leap;printf("\nplease input year,month,day\n");scanf("%d,%d,%d",&year,&month,&day);switch(month) /*First calculate the total number of days in the month before a certain month*/{case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:printf("data error");break;}sum=sum+day; /*Add the number of days in a day*/if(year%400==0||(year%4==0&&year%100!=0)) /*Determine whether it is a leap year*/leap=1;elseleap=0;if(leap==1&&month>2) /*If it is a leap year and the month is greater than 2, add one day to the total number of days*/sum++;printf("It is the %dth day.",sum);}
【Program 3】
Title: Input three integers x, y, z, please output the three numbers from small to large.
1. Program analysis: We find a way to put the smallest number on x, first compare x with y, if x>y, exchange the values of x and y, and then compare x and z, if x>z, swap the values of x and z to minimize x.
2. Program source code:
#include int main(){int x,y,z,t;scanf("%d%d%d",&x,&y,&z);if (x>y){t=x;x=y;y=t;} /*Swap the values of x,y*/if(x>z){t=z;z=x;x=t;} /*Swap the values of x,z*/if(y>z){t=y;y=z;z=t;} /*Swap the values of z,y*/printf("small to big: %d %d %d\n",x,y,z);}
【Program 4】
Title: Output 9*9 formula.
1. Program analysis: consider row and column, a total of 9 rows and 9 columns, i controls the row, j controls the column.
2. Program source code:
#include int main(){int i,j,result;printf("\n");for (i=1;i<10;i++){for(j=1;j
【Program 5】
Title: Print out all the "daffodils number", the so-called "daffodil number" refers to a three-digit number, and the sum of the cubes of the digits is equal to the number itself.For example: 153 is a "daffodil number", because 153=1 cube + 5 cube + 3 cube.
1. Program analysis: use the for loop to control 100-999 numbers, each number is decomposed into units, tens, and hundreds.
2. Program source code:
#include main(){int i,j,k,n;printf("'water flower'number is:");for(n=100;n<1000;n++){i=n/100;/*Decompose to the hundreds place*/j=n/10%10;/*decompose to ten digits*/k=n%10;/*Decompose out the one digit*/if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)printf("%-5d",n);}}
And that's it for today, everyone remember to like and favorite, share and forward, and pay attention to little brother!Finally, if you want to learn or are learning C/C++ programming, you can join Editor’s Programming Learning C/C++ Penguin Circle
边栏推荐
猜你喜欢
【网络安全】文件上传靶场通关(1-11关)
GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
leetcode-399:除法求值
【微信小程序】一文带你了解数据绑定、事件绑定以及事件传参、数据同步
Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
prometheus 监控概述
【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
JS逆向之浏览器补环境(一)
MySQL (6)
Centos 7.9 install PostgreSQL14.4 steps
随机推荐
35. Reverse linked list
【Mysql】——索引的深度理解
Distributed. Distributed lock
类似 MS Project 的项目管理工具有哪些
太阳能板最大面积 od js
leetcode-128: longest continuous sequence
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
[WeChat applet] This article takes you to understand data binding, event binding, event parameter transfer, and data synchronization
Fiddler抓包模拟弱网络环境测试
prometheus 监控概述
"Real" emotions dictionary based on the text sentiment analysis and LDA theme analysis
MySQL (6)
黄东旭:TiDB的优势是什么?
Chi-square distribution of digital image steganography
ROS Action通信
The Meta Metaverse Division lost 2.8 billion in the second quarter, still want to continue to bet?Metaverse development has yet to see a way out
软件测试报告有哪些内容?
MySQL的存储过程
The PC side determines the type of browser currently in use