1.从键盘输入3个实数,输出其中的最大值、最小值和平均值
#include<stdio.h>
int main(){
double number[3];
double max,min,average=0;
int i;
printf("Please input three numbers:");
for(i=0;i<3;i++){
scanf("%lf",&number[i]);
}
max=min=number[0];
for(i=1;i<3;i++){
if(number[i]>max){
max=number[i];
}
if(number[i]<min){
min=number[i];
}
}
average=(number[0]+number[1]+number[2])/3.0;
printf("最大值:%.2lf;最小值:%.2lf;平均值:%.2lf\n",max,min,average);
return 0;
}
2.输入一个整型数,判断奇偶
#include<stdio.h>
int main(){
int number;
printf("Please input number:");
scanf("%d",&number);
if(number%2!=0){
printf("%d是奇数。",number);
}else{
printf("%d是偶数。",number);
}
return 0;
}
3.输入一个代码ASCII码的整数。如果该整数是某个字母的ASCII码,输出该字符。否则发出一声“嘟”并显示“非法输入”。
#include<stdio.h>
int main(){
int number;
printf("Please input number:");
scanf("%d",&number);
if(number>=65&number<=90 || number>=97&number<=122 ){
printf("ASCII字符为:%c",number);
}else{
printf("%c",'\007');
printf("非法输入");
}
return 0;
}
4.输入2个二维平面上的点,判断哪个点离(0,0)更近。【可借鉴欧几里德距离公式】
#include<stdio.h>
#include<math.h>
int main(){
double x1,y1,x2,y2;
double distance1=0,distance2=0;
printf("Please input two coordinate:");
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
distance1=sqrt(pow(x1,2)+pow(y1,2));
distance2=sqrt(pow(x2,2)+pow(y2,2));
if(distance1<distance2){
printf("点1更近");
}else{
printf("点2更近");
}
return 0;
}
5.输入一个二维平面上的直线方程,判断该方程与x和y轴是否有交点,输出交点坐标。
#include<stdio.h>
//直线方程可表示为:y=mx+b
int main(){
double m; //m为斜率
double x,y;
double b; //b为截距
printf("Please input m,b:");
scanf("%lf %lf",&m,&b);
//判断与x轴的交点
if(m==0){
printf("直线与x轴平行,无交点。");
}else{
y=0;
x=(b/m);
printf("与x轴交点坐标为:(%.2lf,%.2lf)。",x,y);
}
//判断与y轴的交点
if(m == INFINITY || m == -INFINITY){
printf("直线与y轴平行,无交点。");
}else{
x=0;
y=b;
printf("与y轴交点坐标为:(%.2lf,%.2lf)。",x,y);
}
return 0;
}
6.输入一个角度,判断它的正弦值是正数还是负数。
#include<stdio.h>
#include<math.h>
int main(){
double PI=3.14;
double x,result=0;
printf("Please input x:");
scanf("%lf",&x);
result=sin(x*PI/180);
if(result>0){
printf("%2lf的正弦值是正数。",x);
}else{
printf("%.2lf的正弦值是负数。",x);
}
return 0;
}
7.输入一个字母,判断它是元音字母还是辅音字母。用switch语句实现。
#include<stdio.h>
int main(){
char s;
printf("Please input s:");
switch(s=getchar()){
case 'a':
printf("%c是元音字母。",s);
break;
case 'e':
printf("%c是元音字母。",s);
break;
case 'i':
printf("%c是元音字母。",s);
break;
case 'o':
printf("%c是元音字母。",s);
break;
case 'u':
printf("%c是元音字母。",s);
break;
default:
printf("%c是辅音字母。",s);
break;
}
return 0;
}
8.输入3个非0整数,判断三个值是否能构成一个三角形,并判断构成的是什么三角形。
#include<stdio.h>
#include<math.h>
int main(){
int x,y,z;
printf("Please input x,y,z:");
scanf("%d %d %d",&x,&y,&z);
if(x+y>z&&x+z>y&&y+z>x){
printf("该三条边可以构成一个三角形。\n");
if(x==y&&x==z&&y==z){
printf("%d、%d、%d构成的是等边三角形。",x,y,z);
}else if(x==y||x==z||y==z){
printf("%d、%d、%d构成的是等腰三角形。",x,y,z);
}
if(pow(x,2)+pow(y,2)==pow(z,2) || pow(x,2)+pow(z,2)==pow(y,2) || pow(y,2)+pow(z,2)==pow(x,2)){
printf("%d、%d、%d构成的是直角三角形。",x,y,z);
}
}else{
printf("该三条边无法构成一个三角形。");
}
return 0;
}
9.凯撒密码是将每个字母循环后移3个位置后输出。编一个程序,输入一个字母,输出加密后的密码。
#include<stdio.h>
int main(){
char s;
printf("Please input s:");
scanf("%c",&s);
s=s+3;
printf("加密后的字符为:%c\n",s);
return 0;
}
拓展:如何对一串密码进行加密呢?比如:acdef-->dfghj
#include<stdio.h>
int main(){
int i,j;
char s[100];
printf("Please input s:");
for(i=0;i<6;i++){
scanf("%c",&s[i]);
s[i]=s[i]+3;
}
printf("加密后的字符串为:");
for(i=0;i<6;i++){
printf("%c",s[i]);
}
return 0;
}
10.已知2017年1月1日是星期日,编一程序计算2017年的某一天是星期几。
思路一:直接输入天数
#include <stdio.h>
#include <string.h>
char* weekname[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int NOLEAP_MON[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31};
int LEAP_MON[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31};
isLeap(int year) {
return (year%400==0) || (year%4==0 && year%100!=0);
}
int countDays(int year,int mon,int day) {
int i,total = 0,j;
if (year< 1980) {
total = 0;
for(i=1979; i>year; i--) {
total += 365;
if(isLeap(i)) total ++;
}
if(isLeap(year)) {
for(i=12; i>mon; i--) total += LEAP_MON[i];
total += LEAP_MON[mon] - day +1;
} else {
for(i=12; i>mon; i--) total += NOLEAP_MON[i];
total += NOLEAP_MON[mon] - day +1;
}
total = - total;
} else {
total = 0;
for(i=1980; i<year; i++) {
total += 365;
if(isLeap(i)) total ++;
}
if(isLeap(year)) {
for(i=1; i<mon; i++) total += LEAP_MON[i];
total += day;
} else {
for(i=1; i<mon; i++) total += NOLEAP_MON[i];
total += day;
}
}
return total;
}
int main() {
int year = 2017;
int day;
printf("Please enter the day of the year: ");
scanf("%d", &day);
int total = countDays(year, 1, 1) + day;
int weekday = total % 7;
printf("The day is %s.\n", weekname[weekday]);
return 0;
}
思路二:分别输入年份,月份和天数(这个月的任意一天)
#include <stdio.h>
int main()
{
int year, month, day;
int days = 0;
int week;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
printf("请输入日期:");
scanf("%d", &day);
// 计算输入日期与2017年1月1日之间的天数差
for (int i = 2017; i < year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
days += 366;
} else {
days += 365;
}
}
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days += 31;
break;
case 4:
case 6:
case 9:
case 11:
days += 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days += 29;
} else {
days += 28;
}
break;
}
}
days += day - 1;
// 计算星期几
week = (days + 7) % 7;
switch (week) {
case 0:
printf("星期日\n");
break;
case 1:
printf("星期一\n");
break;
case 2:
printf("星期二\n");
break;
case 3:
printf("星期三\n");
break;
case 4:
printf("星期四\n");
break;
case 5:
printf("星期五\n");
break;
case 6:
printf("星期六\n");
break;
}
return 0;
}
标签:case,int,31,C语言,break,案例,printf,include
From: https://blog.51cto.com/u_16174658/8066606