当前位置:网站首页>C语言 头哥习题答案截图
C语言 头哥习题答案截图
2022-06-26 16:29:00 【TUSTer_】
一维数组和二维数组
第1关 排序问题
#include<stdio.h>
int main()
{
/*********Begin*********/
int a[10];
int i,j,t;
printf("\n");
for (i=0;i<10;i++) scanf("%d",&a[i]);
printf("\n");
for (j=0;j<9;j++)
for(i=0;i<9-j;i++)
if(a[i]>a[i+1])
{
t=a[i];a[i]=a[i+1];a[i+1]=t;
}
printf("\n");
for(i=9;i>=0;i--) printf("%d ",a[i]);
printf("\n");
return 0;
}第2关 查找整数
#include<stdio.h>
int a[1009];
int main()
{
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
int pre;
scanf("%d", &pre);
for(int i = 1; i <= n; i ++)
{
if(a[i] == pre)
{
printf("%d", i);
return 0;
}
}
printf("-1");
return 0;
}
第3关 计算数组中元素的最大值及其所在的行列下标值
#include<stdio.h>
int a[1000][1000];
int main()
{
printf("Input m, n:");
int n, m;
scanf("%d,%d", &n, &m);
printf("Input %d*%d array:\n", n , m);
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
scanf("%d", &a[i][j]);
int MAX = -999999;
int r, c;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
if(a[i][j] > MAX)
{
MAX = a[i][j];
r = i;
c = j;
}
printf("max=%d, row=%d, col=%d", MAX, r, c);
return 0;
}
第4关 删除最大值
#include<stdio.h>
int main()
{
int a[10];
int MAX = -1;
for(int i = 0; i < 10; i ++)
scanf("%d", &a[i]);
for(int i = 0; i < 10; i ++)
{
if(a[i] > MAX) MAX = a[i];
}
for(int i = 0; i < 10; i ++)
{
if(a[i] == MAX) continue;
printf("%d ", a[i]);
}
return 0;
}
第5关 杨辉三角
#include<stdio.h>
int a[10][10];
int main()
{
int num = 1;
for(int i = 0; i < 10; i ++) a[i][0] = 1;
for(int i = 1; i < 10; i ++)
{
for(int j = 1; j < 10; j ++)
{
if(j == num)
{
num ++;
a[i][j] = 1;
break;
}
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
}
}
num = 1;
for(int i = 0; i < 10; i ++)
{
for(int j = 0; j < 10; j ++)
{
if(j == num)
{
num ++;
break;
}
if(j == num - 1) printf("%d", a[i][j]);
else printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
三种循环语句的使用
第1关 分数序列和
#include <stdio.h>
int main( )
{
int a, b, c, k, n = 5;
double s;
s = 0.0; a = 2; b = 1;
for ( k = 1; k <= n; k++ )
{
/*****以下一行有错误*****/
s = s + (double)a / (double)b;
/*****以下一行有错误*****/
c=a;a=b+c;b=c;
}
printf( "s%d = %lf\n", n, s);
return 0;
}第2关 公式计算(1)
#include<stdio.h>
int main()
{
int n,a=0;
double s;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
a=a+i;
s=s+1/(double)a;
}
printf("s = %lf",s);
return 0;
}第3关 公式计算(2)
/********** Begin **********/
#include<stdio.h>
#include<math.h>
int main()
{
int m;
scanf("%d",&m);
double log(double x);
double s=0;
for(double i=1;i<=m;i++)
{
s=s+log(i);
}
s=sqrt(s);
printf("s = %lf",s);
return 0;
}
第4关 求同时被7或11整除的整数
/********** Begin **********/
#include<stdio.h>
int main()
{
int x;
for(int i=1;i<=1000;i++)
{
if (i%7==0||i%11==0)
{
if(i%7==0&&i%11==0)
{
}
else
{
x++;
printf("%-5d",i);
}
}
if(x==8)
{
printf("\n");
x=0;
}
}
return 0;
}第5关 公式计算(3)
/********** Begin **********/
#include<stdio.h>
int main()
{
int n;
double s=1,sum=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
sum=sum*i;
s=s+1/sum;
}
printf("s = %.6lf",s);
return 0;
}循环结构程序设计1
第1关 小球自由落体运动
#include<stdio.h>
#include<math.h>
int main(void)
{
/*********Begin*********/
float M,N,sum,high,x;
scanf("%f",&M);
scanf("%f",&N);
int i;
sum=M;
for (i=1;i<N;i++)
{
high=pow(0.5,i)*M;
x=pow(0.5,i)*M;
sum=sum+high+x;
}
high=pow(0.5,N)*M;
sum=sum;
printf("%.2f %.2f",high,sum);
/*********End**********/
return 0;
}
第2关 求解出n以内所有能被5整除的正整数的乘积
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int N ,i,sum=1;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
if(i%5==0)
{
sum=sum*i;
}
}
printf("%d",sum);
/*********End**********/
return 0;
}第3关 最大公约数和最小公倍数
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int a,b,c,m,t;
scanf("%d%d",&a,&b);
if(a,b)
{
t=a;a=b;
b=t;
}
m=a*b;
c=a%b;
while(c!=0)
{
a=b;
b=c;
c=a%b;
}
printf("最大公约数是:%d\n",b);
printf("最小公倍数是:%d",m/b);
/*********End**********/
return 0;
}第4关 字符串中各类字符数的统计
#include<stdio.h>
int main(void)
{
/*********Begin*********/
char m;
int a=1;
int k=0,s=0,e=0,n=0;
while(a)
{
m=getchar();
if(m==' ')
k+=1;
else if((m>='a'&& m<='z')||(m>='A'&&m<='Z'))
s+=1;
else if(m>='0'&&m<='9')
n+=1;
else
e+=1;
if(m=='\n')
a=0;
}
printf("%d %d %d %d",s,n,k,e-1);
/*********End**********/
return 0;
}
顺序结构程序设计-进阶
第1关 数字分离
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int n, a, b, c;
scanf("%d",&n);
a=n%10;//这个数的个位
n/=10;
b=n%10;//这个数的十位
n/=10;
c=n;//这个数的百位
printf("%d %d %d",c,b,a);
/*********End**********/
return 0;
}
第2关 求三角形的面积
#include<stdio.h>
#include<math.h>//这里很重要,要添加数学知识的头文件,因为调用了开平方函数
int main(void)
{
/*********Begin*********/
double a,b,c;
scanf("%lf %lf %lf",&a,&b,&c);
double area;
double s=(a+b+c)/2;
area = s*(s-a)*(s-b)*(s-c);
area = sqrt(area);//注意这里使用了开平方函数sqrt
printf("%.3f",area);
/*********End**********/
return 0;
}
第3关 计算两个正整数的最大公约数
#include<stdio.h>
int MaxCommonFactor( int a, int b)
{
int c;
if(a<=0||b<=0)
return -1;
while(b!=0)
{
c=a%b;
a=b;
b=c;
}
return a;
}
int main(void)
{
/*********Begin*********/
int a,b;
scanf("%d,%d",&a,&b);
int c=MaxCommonFactor(a,b);
printf("%d\n",c);
/*********End**********/
return 0;
}
顺序结构程序设计-基础
第1关 加法运算
#include<stdio.h>
int main(void)
{
int a,b,c;
//Please input a,b:
/*********Begin*********/
scanf("%d,%d",&a,&b);
c=a+b;
printf("%d+%d=%d\n",a,b,c);
/*********End**********/
return 0;
}
第2关 不使用第3个变量,实现两个数的对调
#include<stdio.h>
int main(void)
{
int a,b;
//Enter a and b:
scanf("%d%d",&a,&b);
printf("a=%d b=%d\n",a,b);
/*********Begin*********/
a=a^b;
b=a^b;
a=a^b;
/*********End**********/
printf("a=%d b=%d\n",a,b);
return 0;
}第3关 用宏定义常量
#include<stdio.h>
int main(void)
{
/*********Begin*********/
const int N=30;//定义常量用const,即定义题中价格为30.
int x;
scanf("%d",&x);
printf("%d",x*N);
/*********End**********/
return 0;
第4关 计算总成绩和平均成绩
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int a,b,c,d,e;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
int s=a+b+c+d+e;//s代表总成绩
double p=s*1.0/5;//double 用于定义浮点数(小数),p代表平均成绩
printf("%d %.2f",s,p);//%.nf代表输出n位小数
/*********End**********/
return 0;
}第5关 立体几何计算题
#include<stdio.h>
int main(void)
{
/*********Begin*********/
const double PI=3.14;//定义常量pi
double r, h, c, s, v;
scanf("%lf,%lf",&r,&h);//注意输入之间有逗号
c=2*PI*r;
s=4*PI*r*r;
v=PI*r*r*h;
printf("C1=%.2f\n",c);//保留两位小数
printf("Sb=%.2f\n",s);
printf("Vb=%.2f\n",v);
/*********End**********/
return 0;
}
字符数组及其字符串应用
1目标删除
#include "stdio.h"
#include "string.h"
int main()
{
char s[80];
int i,j;
scanf("%s",s);
printf("The original string: \n");
puts(s);
/***** 以下一行有错误 *****/
for(i=j=0;s[i]!='\0';i++)
{
if(s[i]!= 'c')
/***** 以下一行有错误 *****/
s[j++]=s[i];
}
/***** 以下一行有错误 *****/
s[j]='\0';
printf("The string after deleted: \n");
puts(s);
return 0;
}
第2关:去除空格符
#include<stdio.h>
int main(){
int i=0,j=0,k;
char str[30];
scanf("%[^\n]",str);
for(k=0;k<29;k++)//循环最大空格数
{
j=0;
for(i=0;i<30;i++)// 最大单词数
{
if((str[i]==32&&str[i+1]!=32)||(str[i]==32&&str[i+1]==32))//判断空格
{
j=1;
}
if(j==1)//将后续字符往前挪
{
str[i] = str[i+1];
}
}
}
printf("%s \n",str);
return 0;
}
第3关:字符串操作
#include <stdio.h>
int main() {
int i = 0, j = 0;
char s[20], t[20];
scanf("%s", s);
for (i = 0; i <= 20; i++) {
if (i % 2 != 0 || s[i] % 2 == 0)//奇数位置的字符或ASCII码为偶数的字符
t[j++] = s[i];
}
printf("%s", t);
return 0;
}第4关:找最长字符串
/********** Begin **********/
#include <stdio.h>
#include <string.h>
int main()
{
int N;
char s[100][100];
int i;
int max_i, max_len = 0;
scanf("%d",&N);
for(i = 0; i < N; i ++)
{
scanf("%s",s[i]);//输入
}
for(i = 0; i < N; i ++)
{
int l = strlen(s[i]);
if(max_len <l)
{
max_len = l;
max_i = i;
}
}
printf("%s\n", s[max_i]);//输出最长字符串
return 0;
}
第5关:单词排序
#include <stdio.h>
#include <string.h>
int main()
{
//she its can ibm1 bbc NBA nhk2 BOY jxf
//c python java c++ android caffe keras pytorch php
int N,i,k;
char j,s[100][100],a[100][100];
printf("排序前的字符串:\n");
for(i=0;i<10;i++)
{
scanf("%s",s[i]);//输入
printf("%s",s[i]);
if(i<9)
printf("\n");
fflush(stdout);
}
for(i=0;i<9;i++)
{
k=i;
for(j=i+1;j<10;j++)
{
if(strcmp(s[k],s[j])>0)
k=j;
}
if(k!=i)
{
char temp[100];
strcpy(temp, s[k]);
strcpy(s[k], s[i]);
strcpy(s[i], temp);
}
}
printf("排序后的字符串:");
for(i=0;i<10;i++)
{
printf("%s\n",s[i]);
}
return 0;
}选择结构程序设计
第1关 排序
#include<stdio.h>
int main(void)
{
int a,b,c,temp1,temp2,temp3;
/*********Begin*********/
printf("请输入三个整数:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
temp1=a;
a=b;
b=temp1;
}
if (a>c)
{
temp2=a;
a=c;
c=temp2;
}
if (b>c)
{
temp3=b;
b=c;
c=temp3;
}
printf("从小到大排列为:%d,%d,%d",a,b,c);
/*********End**********/
return 0;
}
第2关 选择结构-闰年判断
#include<stdio.h>
int main(void)
{
int a;
/*********Begin*********/
scanf("%d",&a);
if (a%4!=0)
printf("%d 不是闰年!",a);
else if (a%100==0&&a%400!=0)
printf("%d 不是闰年!",a);
else
printf("%d 是闰年!",a);
/*********End**********/
return 0;
}第3关 选择结构-分段函数问题
#include<stdio.h>
int main(void)
{
/*********Begin*********/
float x;
float a;
scanf("%f",&x);
if (x<0&&x!=-3)
{
a=x*x+x-6;
printf("%.3f",a);
}
else if (x>=0&&x<10&&x!=2&&x!=3)
{
a=x*x-5*x+6;
printf("%.3f",a);
}
else
{
a=x*x-x-1;
printf("%.3f",a);
}
/*********End**********/
return 0;
}第4关 学生成绩等级换算
#include<stdio.h>
int main(void)
{
float x;
/*********Begin*********/
scanf("%f",&x);
if (x>=90&&x<=100)
{
printf("A");
}
if (x<90&&x>=80)
{
printf("B");
}
if (x>=70&&x<79)
{
printf("C");
}
if (x>=60&&x<69)
{
printf("D");
}
if (x<60&&x>=0)
{
printf("E");
}
if (x<0||x>100)
{
printf("Score is error!");
}
/*********End**********/
return 0;
}
循环结构程序设计2
第1关 C循环-求平均成绩
#include<stdio.h>
int main()
{
int s;
float x,sum,average;
sum=0;
scanf("%d",&s);
if(s>0)
{
for(int i=0;i<s;i++)
{
scanf("%f",&x);
sum=sum+x;
}
average=sum/s;
printf("the number of students:the scores:average=%.2f",average);
}
else
printf("the number of students:the scores:average=0.00");
return 0;
}
第2关 C循环-求各位数字之积
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int num=0,sum=1;
scanf("%d",&num);
while(num!=0)
{
sum*=num%10;
num/=10;
}
printf("%d",sum);
/*********End**********/
return 0;
}第3关 C循环-求阶乘之和
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int n=0,sum=0;
int m=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
for (int j=1;j<=i;j++)
{
m=m*j;
}
sum+=m;
m=1;
}
printf("%d",sum);
/*********End**********/
return 0;
第4关 C循环-水仙花数
#include<stdio.h>
#include<math.h>
int main(void)
{
/*********Begin*********/
int a=0,b=0,c=0,m=0,t=0;
scanf("%d",&t);
for(int i=100;i<=999;i++)
{
a=i/100;
b=(i-100*a)/10;
c=i-100*a-10*b;
m=pow(a,3)+pow(b,3)+pow(c,3);
if (m==i)
printf("%d ",i);
}
return 0;
}第5关 C循环-寻找完数
#include<stdio.h>
int main(void)
{
/*********Begin*********/
int n=0;
scanf("%d",&n);
int i,j,sum;
for (i=1;i<=n;i++)
{
sum=0;
for(j=1;j<i;j++)
{
if(i%j==0)
sum+=j;
}
if(sum==i)
printf("%d\n",i);
}
/*********End**********/
return 0;
}
第6关 分数求和
#include<stdio.h>
#include<math.h>
int main(void)
{
/*********Begin*********/
double a=1,b=-1;
double sum=0;
for(int i=1;i<=100;i++)
{
sum+=(a/i)*pow(b,i+1);
}
printf("%.3lf",sum);
/*********End**********/
return 0;
}C语言基本语法入门练习题
第1关 求绝对值
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
scanf("%d",&a);
if (a<0)
b=-a;
else b=a;
printf("%d\n",b);
return 0;
}第2关 求m以内的奇数之和
#include <stdio.h>
int main()
{
int n,m, sum;
scanf("%d",&m);
n=1;
sum=0;
while(n<=m)
{
sum=sum+n;
n+=2;
}
printf("%d\n",sum);
return 0;
}
第3关 排除此数还是输出此数
#include <stdio.h>
int main()
{
//获取参数方式 scanf
//int x =0;
//int y = 0;
//scanf("%d", &x);
//结果输出使用prinf
//printf("%d",x);
// 请在此添加你的代码
/********** Begin *********/
int x;
scanf("%d",&x);
if(x%3==0||x/100==3)
{
printf("%d",x);
}
else
printf("排除此数");
return 0;
/********** End **********/
}第4关 医院收费.
#include <stdio.h>
int main()
{
float a,b,c,d,e,f;
scanf("%f %f %f %f",&a,&b,&c,&e);
d = a + b*c;
f = e - d;
printf("请输入药品费、床位费/天、住院天数:应付款:%.2f\n实付款:应找零:%.2f",d,f);
return 0;
}第5关 相反次序重新组合
//注意
//1:该程序每次运行的时间必须小于200毫秒,否则会超时,程序超时将不会测试剩余的测试集
//2:该程序每次运行使用的内存不能超过1M,否则会返回错误
//3:该程序每次运行输出的结果最多显示1000个字符(多余的不显示),每行末尾的所有空格用□表示
#include <stdio.h>
int main()
{
int a,b,c,d,e,f,g,h;
scanf("%4d",&a);
b = a/1%10;
c = a/10%10;
d = a/100%10;
e = a/1000%10;
f = a/10000%10;
g = a/100000%10;
h = a/1000000%10;
if(a>=1)
{
printf("请输入一个4位整数:重新组合后:%d%d%d%d",b,c,d,e);
}
return 0;
}
C语言程序设计编辑与调试环境
第1关 打印输出 Hello World
#include<stdio.h>
int main()
{
/********* Begin *********/
printf("Hello World");
/********* End *********/
return 0;
}第2关 打印输出图形
#include<stdio.h>
int main(void)
{
/********* Begin *********/
printf(" *\n");
printf(" ***\n");
printf(" OK\n");
printf(" Hello!\n");
printf("*********\n");
return 0;
}
第3关 求3个数的最大值
#include<stdio.h>
int main()
{
/********* Begin *********/
int max(int x,int y,int z);
int a,b,c,f;
scanf("%d,%d,%d",&a,&b,&c);
f=max(a,b,c);
printf("max=%d",f);
/********* End *********/
return 0;
}
int max(int x,int y,int z)
{
int e;
if(x>y)e=x;
else e=y;
if(e>z)e=e;
else e=z;
return e;
}
第4关 熟悉C语言调试过程
#include<stdio.h>
int main(void)
{int x;
int y=2,z=3;
scanf("%d",&x);
if(x==y+z)
printf("*****");
else
printf("#####" );
return 0;
} 函数的应用
第1关:求平方根
#include <stdio.h>
#include <math.h>
/***** 以下一行有错误 *****/
double fun(double a, double x0)
{
double x1, y;
x1=(x0+a/x0)/2.0;
/***** 以下一行有错误 *****/
if(fabs(x1-x0)>=0.00001)
y=fun(a,x1);
else
y=x1;
return y;
}
int main()
{
double x;
scanf("%lf",&x);
printf("The square root of %lf is %lf\n",x,fun(x,1.0));
return 0;
}第2关:判断素数

求最大公约数和公倍数

函数处理组

边栏推荐
- How to implement interface current limiting?
- LeetCode Algorithm 24. 两两交换链表中的节点
- Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
- 7 user defined loss function
- Pybullet robot simulation environment construction 5 Robot pose visualization
- Big talk Domain Driven Design -- presentation layer and others
- R language plot visualization: plot visualizes the normalized histogram, adds the density curve KDE to the histogram, and uses geom at the bottom edge of the histogram_ Adding edge whisker graph with
- Redis的ACID
- Common properties of XOR and addition
- Redis migration (recommended operation process) 1
猜你喜欢

Natural language inference with attention and fine tuning Bert pytorch

振动式液量检测装置

Arduino UNO + DS1302简单获取时间并串口打印

Ideal path problem

了解下常见的函数式接口

大话领域驱动设计——表示层及其他

The details of the first pig heart transplantation were fully disclosed: human herpes virus was found in the patient, the weight of the heart doubled after death, and myocardial cell fibrosis

基于Kubebuilder开发Operator(入门使用)

Cloud platform monitoring system based on stm32+ Huawei cloud IOT design

1-12vmware adds SSH function
随机推荐
【207】Apache崩溃的几个很可能的原因,apache崩溃几个
4 custom model training
TCP congestion control details | 1 summary
Practice of federal learning in Tencent micro vision advertising
Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
今年高考英语AI得分134,复旦武大校友这项研究有点意思
国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
心情不好,我就这样写代码
牛客小白月赛50
若依如何实现接口限流?
Codeforces Round #802 (Div. 2)
[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
stm32h7b0替代h750程序导致单片机挂掉无法烧录程序问题
[untitled]
基于Kubebuilder开发Operator(入门使用)
mha 切换(操作流程建议)
若依微服务特殊字符串被过滤的解决办法
(1) Keras handwritten numeral recognition and recognition of self written numbers
The details of the first pig heart transplantation were fully disclosed: human herpes virus was found in the patient, the weight of the heart doubled after death, and myocardial cell fibrosis
C. Inversion Graph