当前位置:网站首页>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
边栏推荐
- 软件测试缺陷报告---定义,组成,缺陷的生命周期,缺陷跟踪产后处理流程,缺陷跟踪处理流程,缺陷跟踪的目的,缺陷管理工具
- GCC Rust is approved to be included in the mainline code base, or will meet you in GCC 13
- 仿牛客网项目总结
- Parameter introduction and selection points of wireless module
- Crawler text data cleaning
- 蛮力法/邻接表 广度优先 有向带权图 无向带权图
- 35. Reverse linked list
- kotlin中函数作为参数和函数作为返回值实例练习
- 手把手教你配置Jenkins自动化邮件通知
- leetcode-128:最长连续序列
猜你喜欢
随机推荐
斩获BAT、TMD技术专家Offer,我都经历了什么?
Shell变量与赋值、变量运算、特殊变量
MySQL installation tutorial (detailed, package teaching package~)
C语言小程序 -- 常见经典练习题
MySQL (6)
What have I experienced when I won the offer of BAT and TMD technical experts?
加密生活,Web3 项目合伙人的一天
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
TiDB 在长银五八消费金融核心系统适配经验分享
JS逆向之浏览器补环境(一)
汉诺塔问题
kotlin中函数作为参数和函数作为返回值实例练习
【Map与Set】之LeetCode&牛客练习
程序员转正述职报告/总结
keep-alive缓存组件
Likou Daily Question - Day 46 - 704. Binary Search
Word/Excel 固定表格大小,填写内容时,表格不随单元格内容变化
What does a software test report contain?
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
Mysql: Invalid default value for TIMESTAMP