部分编程总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了部分编程总结相关的知识,希望对你有一定的参考价值。
重要的话说三遍
#include <stdio.h>
int main(void)
{
printf("I‘m gonna WIN!\n");
printf("I‘m gonna WIN!\n");
printf("I‘m gonna WIN!\n");
return 0;
}
输出倒三角图案
#include <stdio.h>
int main(void)
{
printf("* * * *\n");
printf(" * * *\n");
printf(" * *\n");
printf(" *\n");
return 0;
}
日期格式化
#include <stdio.h>
int main(void)
{
int month,day,year;
scanf("%02d-%02d-%d",&month,&day,&year);
printf("%d-%02d-%02d",year,month,day);
return 0;
}
Say Hello to Integers
#include <stdio.h>
int main(void)
{
int a,b;
scanf("%d%d",&a,&b);
printf("Hello, %d and %d!",a,b);
return 0;
}
计算摄氏温度
#include <stdio.h>
int main(void)
{
int C,F;
scanf("%d",&F);
C=5*(F-32)/9;
printf("Celsius = %d",C);
return 0;
}
计算符号函数的值
#include <stdio.h>
#include <math.h>
int main(void)
{
int n;
scanf("%d",&n);
if(n<0)
{
printf("sign(%d) = -1",n);
}
else if( n==0 )
{
printf("sign(%d) = 0",n);
}
else
{
printf("sign(%d) = 1",n);
}
return 0;
}
计算分段函数[2]
#include <stdio.h>
#include <math.h>
int main(void)
{
double n,m;
scanf("%lf",&n);
if(n>=0)
{
m=sqrt(n);
printf("f(%.2f) = %.2f",n,m);
}
else
{
m=pow((n+1),2)+2*n+1/n;
printf("f(%.2f) = %.2f",n,m);
}
return 0;
}
回文数
#include <stdio.h>
int main(void)
{
int n,a,b,c,d;
scanf("%d",&n);
n>=10000 && n<=99999;
a=n/10000;
b=n/1000%10;
c=n/10%1000%10;
d=n%10000;
if(a==d,b==c)
{
printf("yes");
}
else
{
printf("no");
}
return 0;
}
算术入门之加减乘除
#include <stdio.h>
int main(void)
{
int a,b,x,y,n,m;
scanf("%d %d",&a,&b);
x = a+b;
y = a-b;
n = a*b;
if(a%b==0)
{
m=a/b;
printf("%d + %d = %d\n%d - %d = %d\n%d * %d = %d\n%d / %d = %d",a,b,x,a,b,y,a,b,n,a,b,m);
}
else
{
double m;
m=(double)a/b;
printf("%d + %d = %d\n%d - %d = %d\n%d * %d = %d\n%d / %d = %.2f",a,b,x,a,b,y,a,b,n,a,b,m);
}
return 0;
}
计算火车运行时间
#include <stdio.h>
int main(void)
{
int a,b,c,d,e,f,g,h;
a>=1000 && a<=9999;
b>=1000 && b<=9999;
scanf("%d %d",&a,&b);
c=a/100;
d=b/100;
e=a%100;
f=b%100;
g=d-c;
h=f-g;
if(f<e)
{
h=f+60-e;g=d-1-c;
}
else
{
h=f-e;g=d-c;
}
printf("%02d:%02d",g,h);
return 0;
}
输出三角形面积和周长
#include <stdio.h>
#include <math.h>
int main(void)
{
int a,b,c;
a>0,b>0,c>0;
double area,s,perimeter;
scanf("%d %d %d",&a,&b,&c);
if(a+b>c && a+c>b && b+c>a || a==b==c)
{
s=(a+b+c)/2.00;
area=pow(s*(s-a)*(s-b)*(s-c),0.5);
perimeter=a+b+c;
printf("area = %.2f; perimeter = %.2f",area,perimeter);
}
else
{
printf("These sides do not correspond to a valid triangle");
}
return 0;
}
三天打鱼两天晒网
#include <stdio.h>
int main(void)
{
int N,D;
N<1000 && N>0;
scanf("%d",&N);
D=N%5;
if(D>0 && D<=3)
{
printf("Fishing in day %d",N);
}
else
{
printf("Drying in day %d",N);
}
return 0;
}
出租车计价
#include <stdio.h>
int main(void)
{
int m;
double k,x,y;
scanf("%lf%d",&k,&m);
if(k<=3)
{
x=10;
}
else if(k>3 && k<=10)
{
x=10+2*(k-3);
}
else
{
x=10+7*2+3*(k-10);
}
if(m>=5)
{
y=2*(m/5);
}
else
{
y=0;
}
printf("%.0lf",x+y);
return 0;
}
阶梯电价
#include <stdio.h>
int main(void)
{
double x,y;
scanf("%lf",&x);
if(x<50 && x>0)
{
y=x*0.53;
printf("cost = %.2f",y);
}
else if(x>=50)
{
y=50*0.53+(x-50)*0.58;
printf("cost = %.2f",y);
}
else
{
printf("Invalid Value!");
}
return 0;
}
成绩转换
#include <stdio.h>
int main(void)
{
int x;
scanf("%d",&x);
switch(x/10)
{
case 10:
case 9:
printf("A");
break;
case 8:
printf("B");
break;
case 7:
printf("C");
break;
case 6:
printf("D");
break;
default:printf("E");
}
return 0;
}
比较大小
#include <stdio.h>
int main(void)
{
int a,b,c,t;
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
t=a;
a=b;
b=t;
}
if(a>c)
{
t=a;
a=c;
c=t;
}
if(b>c)
{
t=b;
b=c;
c=t;
}
printf("%d->%d->%d",a,b,c);
return 0;
}
计算天数
#include <stdio.h>
int main(void)
{
int year,month,day;
scanf("%d/%d/%d",&year,&month,&day);
switch(month-1)
{
case 11:
day=day+30;
case 10:
day=day+31;
case 9:
day=day+30;
case 8:
day=day+31;
case 7:
day=day+31;
case 6:
day=day+30;
case 5:
day=day+31;
case 4:
day=day+30;
case 3:
day=day+31;
case 2:
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
day=day+29;
}
else
{
day=day+28;
}
case 1:
day=day+31;
break;
}
printf("%d",day);
return 0;
}
高速公路超速处罚
#include <stdio.h>
int main(void)
{
int x,y;
double m;
scanf("%d %d",&x,&y);
m=(double)(x-y)*100/(double)y;
if(x<y || m<10)
{
printf("OK");
}
else if(m<50)
{
printf("Exceed %.0f%%. Ticket 200",m);
}
else
{
printf("Exceed %.0f%%. License Revoked",m);
}
return 0;
}
三角形判断
#include <stdio.h>
#include <math.h>
int main(void)
{
double x1,y1,x2,y2,x3,y3;
double A,L,S,m,n,l;
scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3);
m=pow(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)),0.5);
n=pow(((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)),0.5);
l=pow(((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)),0.5);
if((m+n>l)&& (m+l>n)&& (n+l>m))
{
double L=m+n+l;
double S=L/2;
double A=pow((S*(S-m)*(S-n)*(S-l)),0.5);
printf("L = %.2f, A = %.2f",L,A);
}
else
{
printf("Impossible");
}
return 0;
}
计算油费
#include <stdio.h>
int main(void)
{
int a,b;
char c;
double x,y,h;
scanf("%d %d %c",&a,&b,&c);
switch(b)
{
case 90:
x=6.95;
break;
case 93:
x=7.44;
break;
case 97:
x=7.93;
break;
}
switch(c)
{
case ‘m‘:
y=0.95;
break;
case ‘e‘:
y=0.97;
break;
}
h=x*y*a;
printf("%.2f",h);
return 0;
}
两个数的简单计算器
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a,b,c;
char op;
b!=0;
scanf("%d %c %d",&a,&op,&b);
switch(op)
{
case ‘+‘:
c = a + b;
break;
case ‘-‘:
c = a - b;
break;
case ‘*‘:
c = a * b;
break;
case ‘/‘:
c = a / b;
break;
case ‘%‘:
c = a % b;
break;
default:
printf("ERROR");
exit(0);
}
printf("%d",c);
return 0;
}
12-24小时制
#include <stdio.h>
int main(void)
{
int a,b;
scanf("%d : %d",&a,&b);
if(a==0)
{
printf("%d:%d AM\n",a,b);
}
else if(a==12)
{
printf("%d:%d PM\n",a,b);
}
else if(a<12)
{
printf("%d:%d AM\n",a,b);
}else if(a>12)
{
printf("%d:%d PM\n",a%12,b);
}
return 0;
}
求一元二次方程的根
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
double a, b, c;
scanf("%lf%lf%lf", &a, &b, &c);
double d, del, ans1, ans2;
d = b * b - 4.0 * a * c;
if(a == b && a == c && a == 0)
printf("Zero Equation\n");
else if(a == b && a == 0 && c != 0)
printf("Not An Equation\n");
else if(d == 0)
{
ans1 = (-b) / (2.0*a);
printf("%.2f\n", ans1);
}
else if(a == 0)
printf("%.2f\n", (-c) / b);
else if(d > 0)
{
del = sqrt(d);
ans1 = (-b+del) / (2.0*a);
ans2 = (-b-del) / (2.0*a);
printf("%.2f\n%.2f\n", ans1, ans2);
}
else if(d < 0)
{
del = sqrt(-d);
if(b != 0)
ans1 = (-b) / (2.0*a);
else
ans1 = 0;
ans2 = del / (2.0*a);
printf("%.2f+%.2fi\n%.2f-%.2fi\n", ans1, ans2, ans1, ans2);
}
return 0;
}
求整数均值
#include <stdio.h>
int main(void)
{
int a,b,c,d;
double Sum,Average;
scanf("%d %d %d %d",&a,&b,&c,&d);
Sum=a+b+c+d;
Average=Sum/4;
printf("Sum = %.0f; Average = %.1f",Sum,Average);
return 0;
}
厘米换算英尺英寸
#include <stdio.h>
int main(void)
{
int m,foot,inch;
scanf("%d",&m);
foot=m/30.48;
inch=(m/30.48-foot)*12;
printf("%d %d",foot,inch);
return 0;
}
是不是太胖了
#include <stdio.h>
int main(void)
{
int H;
double M;
scanf("%d",&H);
M=(H-100)*0.9*2;
printf("%.1f",M);
return 0;
}
A乘以B
#include <stdio.h>
int main(void)
{
int A,B,M;
scanf("%d %d",&A,&B);
M=A*B;
printf("%d",M);
return 0;
}
计算物体自由下落的距离
#include <stdio.h>
int main(void)
{
double height;
height=0.5*10*3*3;
printf("height = %.2f",height);
return 0;
}
将x的平方赋值给y
#include <stdio.h>
int main(void)
{
int x,y;
x=3;
y=x*x;
printf("%d = %d * %d\n%d * %d = %d",y,x,x,x,x,y);
return 0;
}
整数四则运算
#include <stdio.h>
int main(void)
{
int A,B;
scanf("%d %d",&A,&B);
printf("%d + %d = %d\n%d - %d = %d\n%d * %d = %d\n%d / %d = %d",A,B,A+B,A,B,A-B,A,B,A*B,A,B,A/B);
return 0;
}
整数152的各位数字
#include <stdio.h>
int main(void)
{
int a=152,b,c,d;
scanf("%d",&a);
b=a%10;
c=a/10%10;
d=a/100;
printf("152 = %d + %d*10 + %d*100",b,c,d);
return 0;
}
逆序的三位数
#include <stdio.h>
int main(void)
{
int n,a,b,c,d;
n<=999 && n>=100;
scanf("%d",&n);
a=n%10;
b=n/10%10;
c=n/100;
d=a*100+b*10+c;
printf("%1d",d);
return 0;
}
求奇数分之一序列前N项和
#include <stdio.h>
int main()
{
int denominator, i, N;
double item, sum;
while (scanf("%d", &N) != EOF)
{
denominator = 1;
sum = 0;
for (i = 1; i <= N; i++)
{
item = 1.0/denominator;
sum = sum+item;
denominator = denominator+2;
}
printf("sum = %f\n", sum);
}
return 0;
}
统计学生平均成绩与及格人数
#include<stdio.h>
int main()
{
int n,i,a,count=0;
float sum=0,average;
scanf("%d",&n);
if(n<0)
return 0;
else if(n==0)
{
printf("average = %.1f\n",0.0);
printf("count = %d\n",0);
return 0;
}
for(i=0;i<n;i++)
{
scanf("%d",&a);
if(a<0)
return 0;
else if(a>=0)
sum += a;
if(a>=60)
count++;
}
average = sum/n;
printf("average = %.1f\n",average);
printf("count = %d\n",count);
return 0;
}
奇偶分家
#include <stdio.h>
int main(void)
{
int N,i,n,a = 0,b = 0;
scanf("%d",&n);
for(i = 0;i < n;i++)
{
scanf("%d",&N);
if(N % 2 == 0)
{
a++;
}
else
{
b++;
}
}
printf("%d %d",b,a);
return 0;
}
到底是不是太胖了
#include<stdio.h>
#include<math.h>
int n,a,b,c;
int main(void)
{
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&a,&b);
c=(a-100)*0.9*2;
if(abs(b-c)<(c*0.1))
{
printf("You are wan mei!\n");
}
if((abs(b-c)>=(c*0.1)) && b<c)
{
printf("You are tai shou le!\n");
}
if((abs(b-c)>=(c*0.1))&&b>c)
{
printf("You are tai pang le!\n");
}
}
return 0;
}
统计字符
#include<stdio.h>
int main()
{
char str[10];
int i=0,letter=0,blank=0,digit=0,other=0;
while(i<10)
{
str[i++]=getchar();
}
for(i=0;i<10;i++)
{
if(str[i]==‘ ‘)
{
blank++;
}
else if(str[i]==‘\n‘)
{
blank++;
}
else if(str[i]>=‘0‘&&str[i]<=‘9‘)
{
digit++;
}
else if(str[i]>=‘A‘&&str[i]<=‘Z‘)
{
letter++;
}
else if(str[i]>=‘a‘&&str[i]<=‘z‘)
{
letter++;
}
else
{
other++;
}
}
printf("letter = %d, blank = %d, digit = %d, other = %d",letter,blank,digit,other);
return 0;
统计学生成绩
#include <stdio.h>
int main(void)
{
int n,a,i,A=0,B=0,C=0,D=0,E=0;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d",&a);
if(a>=90)
{
A++;
}
else if(a>=80)
{
B++;
}
else if(a>=70)
{
C++;
}
else if(a>=60)
{
D++;
}
else
{
E++;
}
}
printf("%d %d %d %d %d",A,B,C,D,E);
}
return 0;
}
求交错序列前N项和
#include <stdio.h>
int main(void)
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i;
double j,k; /*j分母,i分子*/
double sum=1; /*sum表示总和*/
j=1;
for(i=2;i<=n;i++)
{
if(i%2==0)
{
j+=2;
k=-i/j;
}
else
{
j+=2;
k=i/j;
}
sum+=k;
}
printf("%.3f\n",sum);
}
return 0;
}
找出最小值
#include<stdio.h>
int main(void)
{
int n,i,l,min;
scanf("%d",&n);
scanf("%d",&l);
min=l;
for(i=1;i<n;i++)
{
scanf("%d",&l);
if(min>l)
min=l;
}
printf("min = %d",min);
return 0;
}
求给定精度的简单交错序列部分和
#include <stdio.h>
#include <math.h>
int main(void)
{
int dom,flog;
double eps,sum,t;
flog=1;
dom=1;
sum=0;
scanf("%lf",&eps);
do
{
t=flog*1.0/(3*dom-2);
sum+=t;
flog=-flog;
dom++;
}
while(fabs(t)>eps);
printf("sum = %.6lf",sum);
return 0;
}
猜数字游戏
#include<stdio.h>
int main()
{
int key, n;
scanf("%d %d",&key,&n);
int i;
int m;
int flag = 0;
for (i = 1;i <= n;i ++)
{
scanf("%d",&m);
if (m < 0)
break;
if (m > key)
printf("Too big\n");
if (m < key)
printf("Too small\n");
if (m == key)
{
if (i == 1)
{
flag = 1;
printf("Bingo!\n");
break;
}
if (i == 2 || i == 3)
{
flag = 1;
printf("Lucky You!\n");
break;
}
if (i > 3)
{
flag = 1;
printf("Good Guess!\n");
break;
}
}
}
if (flag == 0)
printf("Game Over\n");
return 0;
}
求奇数和
#include <stdio.h>
int main()
{
int n = 0, sum = 0;
do
{
scanf("%d", &n);
if (n % 2 == 1)
sum += n;
} while (n > 0);
printf("%d\n", sum);
return 0;
}
韩信点兵
#include <stdio.h>
int main(void)
{
int i;
for(i = 1;;i++)
{
if(i%5==1 && i%6==5 && i%7==4 && i%11==10)
{
break;
}
}
printf("%d",i);
return 0;
}
爬动的蠕虫
#include <stdio.h>
int main(void)
{
int N,U,D,head=1,time=0;
scanf("%d %d %d",&N,&U,&D);
while(1)
{
head+=U;
time++;
if(head>N)break;
head-=D;
time++;
}
printf("%d",time);
return 0;
}
掉入陷阱的数字
#include <stdio.h>
int cal(int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
return sum*3+1;
}
int main()
{
int n,res,i=0;
scanf("%d", &n);
for (;;)
{
res = cal(n);
printf("%d:%d\n", ++i, res);
if (n == res) break;
n = res;
}
return 0;
}
寻找250
#include <stdio.h>
int main(void)
{
int a,k=0,i=0,f[100000];
while(scanf("%d",&a)!=EOF)
{
f[k++]=a;
}
for(i=0;i<k;i++)
{
if(f[i]==250)
{
printf("%d",i+1);
break;
}
}
return 0;
}
换硬币
#include <stdio.h>
int main(void)
{
int n;//用于用户输入的分钱数
int x,y,z;//x是1分,y是2分,z是5分(硬币个数)
int count=0;//用于保存有多少种方法
int total;//多少硬币
scanf("%d",&n);
for(z=n/5;z>0;z--)
for(y=n/2;y>0;y--)
for(x=n/1;x>0;x--)
{
if(x+2*y+5*z==n)
{
total=x+y+z;
count++;
printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n",z,y,x,total);
}
}
printf("count = %d\n",count);
return 0;
}
输出三角形字符阵列
#include <stdio.h>
int main(void)
{
int i,j,n;
char c=‘A‘;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=n;j>=i;j--)
printf("%c ",c++);
printf("\n");
}
return 0;
}
找完数
#include<stdio.h>
int main()
{
int m,n,j,i,sum=0,k,flag=0;
scanf("%d %d",&m,&n);
for(m;m<=n;m++)
{
sum = 0;
for(j=1;j<m;j++)
{
if(m%j==0)
{
sum = sum + j;
}
}
if(sum==m)
{
flag = 1;
printf("%d = 1",m);
for(k = 2;k < m;k++)
{
if(m % k == 0)
{
printf(" + %d",k);
}
}
printf("\n");
}
}
if(flag == 0)
{
printf("None");
}
return 0;
}
打印九九口诀表
#include <stdio.h>
int main(void)
{
int n,i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d*%d=%-4d",j,i,j*i);
printf("\n");
}
return 0;
}
小于m的最大的10个素数
#include<stdio.h>
int main()
{
int m,h, i, x = 0;
scanf("%d",&h);
for (i = h-1; i > 1; i--)
{
for (m = (i - 1); m > 0; m--)
{
if(i % m == 0) break;
}
if(m == 1)
{
x++;
printf("%6d", i);
}
if(x == 10) break;
}
return 0;
}
计算阶乘和
#include <stdio.h>
int main(void)
{
int n,j,i,item,sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
item=1;
for(j=1;j<=i;j++)
item=item*j;
sum+=item;
}
printf("%d",sum);
return 0;
}
特殊a串数列求和
#include <stdio.h>
int main(void)
{
int i,a,c=0,n,s=0;
scanf("%d %d",&a,&n);
for(i=1;i<=n;i++)
{
c=c*10+a;
s+=c;
}
printf("s = %d",s);
return 0;
}
最大公约数和最小公倍数
#include <stdio.h>
int main(void)
{
int a,b,i,j,min,max,t,gcd;
scanf("%d %d",&a,&b);
if(a>b)
{
min=b;
}
else
{
min=a;
}
for(i=1;i<=min;i++)
{
if(a%i==0 && b%i==0)
{
gcd=i;
}
}
if(a>b)
{
max=a;
}
else
{
max=b;
}
i=a;
j=b;
while(a%b!=0)
{
t=a%b;
a=b;
b=t;
}
a=i*j/b;
printf("%d %d",gcd,a);
return 0;
}
兔子繁衍问题
#include <stdio.h>
int main(void)
{
int n,month=1,sum=1,now=0,pro=0;/*now为现在的;pro为未来的*/
scanf("%d",&n);
if(n==sum)
{
printf("%d\n",month);
}
else
{
month++;
now=pro+sum;
while(now<n)
{
month++;
pro=sum;
sum=now;
now=pro+sum;
}
printf("%d\n",month);
}
return 0;
}
求整数的位数及各位数字之和
#include<stdio.h>
int main(void)
{
int n, s = 0, i = 0, a = 0;
scanf("%d", &n);
while (n != 0)
{
s += (n % 10);
n /= 10;
i += 1;
}
printf("%d %d\n", i,s);
return 0;
}
未 完 ...
^ - ^ !
以上是关于部分编程总结的主要内容,如果未能解决你的问题,请参考以下文章
全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段