博主本人整理资料不易,如果文章对大家有用的话,恳请大家能够动动小手帮忙点个赞,如果能点个关注的话那就更好了…
#include
int main(){
float r,h,C,S,Sa,Sb,V;
scanf("%f,%f",&r,&h);
C=2*3.14*r;
S=3.14*r*r;
Sa=4*S;
Sb=4*3.14*r*r*r/3;
V=S*h;
printf("C=%5.2f\nS=%5.2f\nSa=%5.2f\nSb=%5.2f\nV=%5.2f",C,S,Sa,Sb,V);
}
#include
int main(){
float c,F;
printf("请输入一个华氏温度:\n");
scanf("%f",&F);
c=5.0*(F-32)/9.0;
printf("摄氏温度为:%5.2f°\n",c);
return 0;
}
#include
#define PI 3.14159
int main(){
float r,h,c1,cs,cvz;
printf("请输入圆的半径r和圆柱的高h:\n");
scanf("%f,%f",&r,&h);
c1=2.0*PI*PI;
cs=PI*r*r;
cvz=cs*h;
printf("圆周c1=%5.2f\n圆面积cs=%5.2f\n圆柱的体积cvz=%6.2f",c1,cs,cvz);
}
#include
int main(){
char ch;
printf("请输入一个字母:\n");
scanf("%c",&ch);
printf("与输入字母相对应的ASCII码是:%d",ch);
return 0;
}
#include
int main(){
int x,y;
printf("请输入x的值:\n");
scanf("%d",&x);
if(x<1){
y=x;
}else if(x>=1 && x<10){
y=2*x-1;
}else if(x>=100){
y=3*x-11;
}
printf("y的值为:%d\n",y);
return 0;
}
#include
int main(){
long int num;
int wan,qian,bai,shi,ge;
int place;
printf("请输入一个不多于5位的正整数:\n");
scanf("%d",&num);
if(num>9999){
place=5;
}else if(num>999){
place=4;
}else if(num>99){
place=3;
}else if(num>9){
place=2;
}else{
place=1;
}
printf("%d是%d位数\n",num,place);
wan=num/10000;
qian=num/1000%10;
bai=num/100%10;
shi=num/10%10;
ge=num%10;
switch(place){
case 5:
printf("分别打印出每位数字:\n");
printf("%d\n%d\n%d\n%d\n%d\n",wan,qian,bai,shi,ge);
printf("逆序打印出每位数字:\n");
printf("%d\n%d\n%d\n%d\n%d\n",ge,shi,bai,qian,wan);
break;
case 4:
printf("分别打印出每位数字:\n");
printf("%d\n%d\n%d\n%d\n",qian,bai,shi,ge);
printf("逆序打印出每位数字:\n");
printf("%d\n%d\n%d\n%d\n",ge,shi,bai,qian);
break;
case 3:
printf("分别打印出每位数字:\n");
printf("%d\n%d\n%d\n",bai,shi,ge);
printf("逆序打印出每位数字:\n");
printf("%d\n%d\n%d\n",ge,shi,bai);
break;
case 2:
printf("分别打印出每位数字:\n");
printf("%d\n%d\n",shi,ge);
printf("逆序打印出每位数字:\n");
printf("%d\n%d\n",ge,shi);
break;
case 1:
printf("分别打印出每位数字:\n");
printf("%d\n",ge);
printf("逆序打印出每位数字:\n");
printf("%d\n",ge);
break;
default:
break;
}
return 0;
}
#include
int main(){
int a,b,c,d,t;
printf("请输入4个正整数:\n");
scanf("%d,%d,%d,%d",&a,&b,&c,&d);
if(a>b){
t=a;
a=b;
b=t;
if(a>c){
t=a;
a=c;
c=t;
if(a>d){
t=a;
a=d;
d=t;
}
}
}
if(b>c){
t=b;
b=c;
c=t;
if(b>d){
t=b;
b=d;
d=t;
}
}
if(c>d){
t=c;
c=d;
d=t;
}
printf("从小到大的顺序为:%d,%d,%d,%d\n",a,b,c,d);
return 0;
}
#include
int main(){
char c;
int letter=0,space=0,dight=0,other=0;
while((c=getchar())!='\n'){
if((c>='a' && c<='z')||(c>='A' && c<='Z')){
letter++;
}else if(c==' '){
space++;
}else if(c>='0'&&c<='9'){
dight++;
}else{
other++;
}
}
printf("英文字母数为:%d\n空格数为:%d\n数字数为:%d\n其他字符数为:%d\n"
,letter,space,dight,other);
}
#include
int main(){
int m,n,p,r,t;
printf("请输入两个正整数m和n:\n");
scanf("%d,%d",&m,&n);
if(n>m){ //大数在前小数在后
t=n;
n=m;
m=t;
}
p=m*n; //用于求最小公倍数
while((m%n)!=0){
r=m%n;
m=n;
n=r;
}
printf("最大公约数为:%d\n",n);
printf("最小公倍数为:%d\n",p/n);
}
#include
int main(){
int a,b,m,p;
printf("请输入两个整数a和b:\n");
scanf("%d,%d",&a,&b);
m=a*a+b*b;
if(m>100){
p=m/100;
printf("百位以上的数字为:%d\n",p);
}else{
printf("两数之和为:%d\n",m);
}
return 0;
}
#include
int main(){
int num;
printf("请输入一个整数:\n");
scanf("%d",&num);
if(num%5==0 && num%7==0){
printf("yes\n");
}else{
printf("no\n");
}
return 0;
}
#include
int main(){
int x,y;
printf("请输入x的值:\n");
scanf("%d",&x);
switch(x<0){
case 1:
y=-1;
break;
case 0:
switch(x==0){
case 1:
y=0;
break;
default:
y=1;
break;
}
}
printf("y的值为:%d",y);
return 0;
}
#include
int main(){
int score,temp,log;
char grade;
log=1;
while(log){
printf("请输入成绩:\n");
scanf("%d",&score);
if(score>100 || score<0){
printf("\n 输入错误,请重试!\n");
}else{
log=0;
}
}
if(score==100){
temp=9;
}else{
temp=(score-score%10)/10;
}
switch(temp){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
grade='E';
break;
case 6:
grade='D';
break;
case 7:
grade='C';
break;
case 8:
grade='B';
break;
case 9:
grade='A';
break;
}
printf("score=%d,grade=%c\n",score,grade);
return 0;
}
#include
int main(){
int a,b;
char symbol;
printf("请输入原始数据:\n");
scanf("%d%c%di\n",&a,&symbol,&b);
printf("原使复数为:%d%c%di\n",a,symbol,b);
if(symbol=='-'){
symbol='+';
}else{
symbol='-';
}
printf("其共轭复数为:%d%c%di\n",a,symbol,b);
return 0;
}
#include
int main(){
int a,n,tn=0,sn=0,i=1;
printf("请输入a和n的值:\n");
scanf("%d,%d",&a,&n);
while(i<=n){
tn=tn+a; //用来表示第i个位置上的值
sn=sn+tn; //求和
a=a*10; //第i个位置为i的i位数
++i;
}
printf("a+aa+aaa+...+aa..a的值为:%d\n",sn);
return 0;
}
#include
int main(){
int n,t=1,s=0;
for(n=1;n<=20;n++){
t=t*n; //第几个位置时,该位置数的阶乘
s=s+t; //求和
}
printf("1!+2!+3!+4!+...+20!等于:%d\n",s);
return 0;
}
#include
int main(){
int n1=100,n2=50,n3=10;
float k;
float s1=0,s2=0,s3=0;
for(k=1;k<=n1;k++){
s1=s1+k;
}
for(k=1;k<=n2;k++){
s2=s2+k*k;
}
for(k=1;k<=n3;k++){
s3=s3+1/k;
}
printf("总和=%8.2f\n",s1+s2+s3);
return 0;
}
#include
int main(){
int num,a,b,c;
for(num=100;num<=999;num++){
a=num/100;
b=num%100/10;
c=num%100%10;
if(num==a*a*a+b*b*b+c*c*c){
printf("%d是水仙花数\n",num);
}
}
}
#include
int main(){
float price=0.8,sumprice=0.0,aveprice;
int k=0,num,sum=0;
for(num=2;num<=100;num=num*2){
++k;
sum=sum+num;
}
sumprice=sum*price;
aveprice=sumprice/k;
printf("每天平均花的钱数为:%5.2f元\n",aveprice);
return 0;
}
#include
int main(){
int i;
for(i=1;i<100;i++){
if(i*i%10==i || i*i%100==i){
printf("%3d",i);
}
}
}
#include
int main(){
int i,x,y;
long last=1;
printf("Input x,y:");
scanf("%d,%d",&x,&y);
for(i=1;i<=y;i++){
last=last*x%1000;
printf("The 3 digits:%ld\n",last);
}
}
#include
int main(){
int k[100];
int a,s,i,n;
for(a=2;a<1000;a++){
s=a;
n=0;
for(i=1;i<a;i++){
if((a%i)==0){
n++;
k[n]=i; //将其除数放到数组中
s=s-i; //该数前去其除数,然后判断最后结果是否为0
}
}
if(s==0){
printf("%d its factors are:",a);
for(i=1;i<=n;i++){
printf("%d,",k[i]);
}
printf("\n");
}
}
}
#include
int main(){
float sn=100,hn=sn/2;
int n;
for(n=2;n<=10;n++){
sn=sn+2*hn;
hn=hn/2;
}
printf("第10次反弹高为:%5.2f米\n",hn);
printf("第10次落地时共经过:%5.2f米\n",sn);
}
#include
int main(){
int i,j,k;
for(i=0;i<=3;i++){ //输出上面4行*号
for(j=0;j<=2-i;j++) //输出*号前面的空格
printf(" ");
for(k=0;k<=2*i;k++) //输出*号
printf("*");
printf("\n"); //输出完一行*号后换行
}
for(i=0;i<=2;i++){ //输出下面3行*号
for(j=0;j<=i;j++) //输出*号前面的空格
printf(" ");
for(k=0;k<=4-2*i;k++) //输出*号
printf("*");
printf("\n"); //输出完一行*号后换行
}
}
代码二:
#include
int main(){
int i,j,k;
for(i=0;i<=3;i++){
for(j=0;j<=2-i;j++)
printf(" ");
for(k=0;k<(2*(i+1)-1);k++)
printf("*");
printf("\n");
}
for(i=0;i<=2;i++){
for(j=0;j<=i;j++)
printf(" ");
for(k=0;k<(2*(3-i)-1);k++)
printf("*");
printf("\n");
}
}
#include
int c(int x,int y){
if((y==1)||(y==x+1))
return (1);
else
return c(x-1,y-1)+c(x-1,y);
}
int main(){
int i,j,n=13;
printf("N=");
while(n>12)
scanf("%d",&n);
for(i=0;i<=n;i++){
for(j=0;j<n-i;j++)
printf(" ");
for(j=1;j<i+2;j++)
printf("%6d",c(i,j));
printf("\n");
}
}
#include
int main(){
void IsPrime(int k);
int k;
printf("1到100之间的所有素数:\n");
for(k=2;k<=100;k++){
IsPrime(k);
}
}
void IsPrime(int k){
int j;
for(j=2;j<=k-1;j++){
if(k%j==0){
break;
}
}
if(j==k){
printf("%d,",k);
}
}
#include
int main(){
int a[10]={1,2,3,4,5,6,7,8,9,10};
int b[6]={2,4,6,8,11,13};
int c[16];
int i,j,k=0;
for(i=0;i<10;i++){
for(j=0;j<6;j++){
if(a[i]==b[j]){
break;
}
}
if(j==6){
c[k]=a[i];
k++;
}
}
for(i=0;i<k;i++){
printf("%d,",c[i]);
}
}
#include
int main(){
int a[3][4]={{1,2,3,4},{5,6,7,8},{1,2,3,4}};
int i,j,k,sum;
for(i=0;i<3;i++){
for(j=0;j<4;j++){
printf("%d",a[i][j]);
}
printf("\n");
}
sum=0;
for(i=0;i<4;i++){
sum=sum+a[0][i]+a[2][i];
}
for(j=1;j<2;j++){
sum=sum+a[j][0]+a[j][3];
}
printf("sum=%d",sum);
}
#include
#include
int main(){
char a[20];
char b[20];
int m,n,k=0;
gets(a);
gets(b);
m=strlen(a);
n=strlen(b);
while((a[k]==b[k])&&k<m&&k<n){
k++;
}
if(k==m&&k==n){
printf("0\n");
}
else if(a[k]>b[k]){
printf("1\n");
}else{
printf("-1\n");
}
return 0;
}
#include
#include
int main(){
char a[20];
int i,m,n=0;
gets(a);
m=strlen(a);
for(i=0;i<m;i++){
if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z')){
n++;
}
}
printf("字母的个数为:%d\n",n);
}
#include
int main(){
int a[10];
int i,m,j,k;
printf("请向数组中输入数据:\n");
for(i=0;i<10;i++){
scanf("%d",&a[i]);
}
printf("请输入要删除的数k:\n");
scanf("%d",&k);
for(i=0;i<10;i++){
if(k==a[i]){
m=i;
break;
}
}
for(j=m;j<10;j++){
a[j]=a[j+1];
}
printf("删除k后的数组为:\n");
for(i=0;i<9;i++){
printf("%d,",a[i]);
}
}
#include
int main(){
int n,i,k;
printf("输入数组的大小:n=\n");
scanf("%d",&n);
int arr1[n];
printf("向数组中填入数据:\n");
for(i=0;i<n;i++){
scanf("%d",&arr1[i]);
}
printf("请输入要移动的位置数:k=\n");
scanf("%d",&k);
int arr2[n];
int m=0;
for(i=n-k;i<n;i++){
arr2[m]=arr1[i];
m++;
}
for(i=k;i<n;i++){
arr2[i]=arr1[i-k];
}
for(i=0;i<n;i++){
printf("%d,",arr2[i]);
}
}
#include
#include
int main(){
char str[20];
gets(str);
int n,i;
n=strlen(str);
for(i=0;i<n/2;i++){
if(str[i]!=str[n-i-1])
break;
}
if(i==n/2){
printf("true");
}else{
printf("false");
}
}
#include
int main(){
char str[50],*p,*q;
gets(str);
p=str;
q=str;
while((*q)!='\0'){
q++;
}
q--;
while((*p)==(*q)&&p<q){
p++;
q--;
}
if(p==q||(q+1)==p){
printf("yes");
}else{
printf("no");
}
}
#include
#include
int main(){
void replace(char *str,char ch,char ch2);
char arr[50];
char ch,ch2;
printf("请输入字符串str:\n");
gets(arr);
printf("请输入字符ch和ch2:\n");
scanf("%c,%c",&ch,&ch2);
replace(arr,ch,ch2);
puts(arr);
}
void replace(char *str,char ch,char ch2){
char *p;
p=str;
while(*p!='\0'){
if(*p==ch){
*p=ch2;
}
p++;
}
}
#include
#include
int main(){
int delStr(char *str,char ch);
char str[80]="wearegoodfriends";
char ch;
int i;
scanf("%c",&ch);
printf("%s\n",str);
i=delStr(str,ch);
printf("%s\n",str);
}
int delStr(char *str,char ch){
int i,j,total,k,sum;
char *p;
i=0;
total=0;
p=str;
while(*(p+i)!='\0'){ //p1字符串是否结束
k=i;
sum=0;
while(*(p+k)==ch){ //k移动到不要删除的位置
k++;
sum++; //删除字符个数
}
total+=sum;
if(*(p+i)==ch){
for(j=i;*(p+j+sum)!='\0';j++){
*(p+j)=*(p+j+sum);
}
*(p+j)='\0';
}
i++;
}
return total;
}
#include
#include
int main(){
void reversestr(char *str);
char arr[50];
gets(arr);
reversestr(arr);
printf("%s",arr);
}
void reversestr(char *str){
char *p,*q;
char ch;
p=str;
q=str;
while(*q!='\0'){
q++;
}
q--;
while(p<q){
ch=*p;
*p=*q;
*q=ch;
p++;
q--;
}
}
#include
struct Book{
char name[50];
float price;
};
int main(){
int i;
struct Book book[10];
printf("请输入10本书的名称和定价:\n");
for(i=0;i<10;i++){
scanf("%s %f",book[i].name,&book[i].price);
}
struct Book minbook=book[0],maxbook=book[0];
for(i=1;i<10;i++){
if(book[i].price>maxbook.price){
maxbook=book[i];
}
if(book[i].price<minbook.price){
minbook=book[i];
}
}
printf("定价最高的书的名称为:%s,书的价格为:%5.2f\n",maxbook.name,maxbook.price);
printf("定价最低的书的名称为:%s,书的价格为:%5.2f\n",minbook.name,minbook.price);
}
struct student{
char num[10],name[10];
float s[4];
double ave;
};
要求:编写两个函数,函数fun1把10名学生放在一个结构体数组中。从键盘输入学生的学号、姓名和4门成绩,然后计算出平均分放在结构体对应的域中;函数fun2输出10名学生的记录。主函数调用函数fun1和函数fun2,不使用全局变量,函数之间的数据全部用参数传递。
#include
struct student{
char num[10],name[10];
float s[4];
float ave;
};
int main(){
void fun1(struct student stu[]);
void fun2(struct student stu[]);
struct student stu[3];
fun1(stu);
fun2(stu);
}
void fun1(struct student stu[]){
printf("请分别输入10名学生的学号、姓名和4分成绩:\n");
int i;
for(i=0;i<3;i++){
scanf("%s %s %f %f %f %f",stu[i].num,stu[i].name,&stu[i].s[0],
&stu[i].s[1],&stu[i].s[2],&stu[i].s[3]);
stu[i].ave=(stu[i].s[0]+stu[i].s[1]+stu[i].s[2]+stu[i].s[3])/4.0;
}
}
void fun2(struct student stu[]){
int i;
for(i=0;i<3;i++){
printf("学号:%s\n姓名:%s\n4门成绩:%f,%f,%f,%f\n平均分:%5.2f\n",
stu[i].num,stu[i].name,stu[i].s[0],stu[i].s[1],stu[i].s[2],stu[i].s[3],
stu[i].ave);
}
}
struct node *Merge(struct node *h1,struct node *h2){
struct node *p,*q,*r,*s,*h;
p=h1;
q=h2;
if(p->num<q->num){
h=s=p;
r=p;
p=p->next;
r->next=NULL;
}else{
h=s=q;
r=q;
q=q->next;
r-next=NULL;
}
while(p&&q){
if(p->num<q->num){
r=p;
p=p->next;
}else{
r=q;
q=q->next;
}
s->next=r;
s=s->next;
}
if(p){
s->next=p;
}else{
s->next=q;
}
return h;
}
#include
#include
int main(){
FILE *fp1,*fp2,*fp3;
char str[128];
if(fp1==fopen("t1.text","r")==NULL){
printf("不能打开文件\n");
exit(0);
}
if(fp2==fopen("t2.txt","r")==NULL){
printf("不能打开文件\n");
exit(0);
}
if(fp3==fopen("t3.txt","w")==NULL){
printf("不能打开文件\n");
exit(0);
}
while((strlen(fgets(str,128,fp2)))>0){
fputs(str,fp3);
printf("%s",str);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
#include
#include
int main(){
float a,b,c,d,x1,x2;
printf("请输入a,b,c的值:\n");
scanf("%f %f %f",&a,&b,&c);
if(a!=0){
d=sqrt(b*b-4*a*c);
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
printf("x1=%0.2f\tx2=%0.2f\n",x1,x2);
}
}
#include
int main(){
/**
思路:先把百年的年份拿出来,是400的倍数,是闰年,不是400的倍数,不是闰年。
非百年的年份按照4处理。
**/
int year;
printf("请输入年份:\n");
scanf("%d",&year);
//先处理百年年份
if(year%100==0){
//是400的倍数为闰年
if(year%400==0){
printf("y\n");
}else{
printf("n\n");
}
}
//再处理非百年的年份
else{
if(year%4==0){
printf("y\n");
}else{
printf("n\n");
}
}
}
#include
int main(){
int i,j;
for(i=1;i<=9;i++){ //外层循环控制行
for(j=1;j<=i;j++){ //内层循环控制列
printf("%dx%d=%d\t",i,j,i*j);
}
printf("\n");
}
}
#include
int main(){
int i,j,k;
for(i=1;i<=10;i++){
k=i;
for(j=0;j<=9;j++){
printf("%3d",j*10+k);
}
printf("\n");
}
}
#include
int main(){
int checkPrime(int number);
int num,i,flag=0;
printf("请输入一个整数:\n");
scanf("%d",&num);
for(i=2;i<=num/2;i++){
if(checkPrime(i)==1){
if(checkPrime(num-i)==1){
printf("%d是%d和%d两个素数的和\n",num,i,num-i);
flag=1;
}
}
}
if(flag==0){
printf("%d不能分解成两个素数",num);
}
}
//判断素数
int checkPrime(int number){
int i;
for(i=2;i<number;i++){
if(number%i==0){
return 0;
}
}
if(i==number){
return 1;
}
}
#include
#include
int main(){
int converTo(long long number);
long long num;
printf("请输入一个二进制数:\n");
scanf("%lld",&num);
printf("二进制数%lld转换为十进制数为:%d",num,converTo(num));
}
int converTo(long long number){
int reminder,decimalNumber=0,i=0;
while(number!=0){
reminder=number%10; //余数
number=number/10; //每次去掉最后一位再计算
decimalNumber=decimalNumber+reminder*pow(2,i);
i++;
}
return decimalNumber;
}
#include
#include
int main(){
long long convertDecimalToBinary(int number);
int num2;
printf("请输入一个数:\n");
scanf("%d",&num2);
printf("十进制数%d转换为二进制数为:%lld",num2,convertDecimalToBinary(num2));
}
//十进制转换为二进制数
long long convertDecimalToBinary(int number){
int reminder,i=1;
long long binaryNumber=0;
while(number!=0){
reminder=number%2; //每次记录二进制末尾数
binaryNumber=binaryNumber+reminder*i; //通过乘10法进位
number=number/2;
i=i*10;
}
return binaryNumber;
}