当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
手把手教你配置Jenkins自动化邮件通知
想要写出好的测试用例,先要学会测试设计
观察者(observer)模式(一)
有没有可以做副业可以日入300元方法?
What is the ideal college life?
蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
VSCode Plugin: Nested Comments
leetcode-128: longest continuous sequence
leetcode-128:最长连续序列
uniapp uses 3rd party fonts
12张图带你彻底搞懂服务限流、熔断、降级、雪崩
keep-alive缓存组件
【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
How to expose Prometheus metrics in go programs
加密生活,Web3 项目合伙人的一天
华为od 转骰子 js
C语言小程序 -- 常见经典练习题
Analyze the capabilities and scenarios of the cloud native message flow system Apache Pulsar
rpm安装postgresql12
pc端判断当前使用浏览器类型