本文主要是介绍2023年西工大noj(C程序设计)100题代码参考,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
西工大noj2023年100题
- 已更新到100题,3道WA,飞机、字符串替换、日出日落
- 代码托管仓库和pdf代码总结
- 1-10
- 1.Hello World
- 2.A+B
- 3.数据类型大小及范围
- 4.平均值
- 5.进制转换
- 6.浮点数输出
- 7.动态宽度输出
- 8.计算地球上两点之间的距离
- 9.风寒指数
- 10.颜色模型转变
- 11-20
- 11.倍数和
- 12.分数的加、减、乘、除法
- 13.乘数模
- 14.对称数
- 15.比率
- 16.操作数
- 17.级数和
- 18.组合数
- 19.幂数模
- 20.方阵
- 21-30
- 21.毕达哥拉斯三元组
- 22.倒水
- 俄罗斯农夫乘法
- 24.查找数列
- 25.阶乘倍数
- 26.最大数字
- 27.余数和
- 28.方案数
- 29.竖式乘法
- 30.好数字
- 31-40
- 31.素数
- 32.运动会
- 33.可变参数累加
- 34哈沙德数
- 35.冰雹序列
- 36.基恩数
- 37.可变参数平均
- 38.光线追踪
- 39.二进制表示
- 40.佩尔数
- 41-50
- 41.【专业融合:管理】货运优化
- 42.完美矩阵
- 43.【专业融合:航空】飞机起飞速度(没有AC)
- 44.【专业融合:物理】蒙特卡罗方法求积分
- 45.素数筛选法
- 46.【专业融合:计算机】波士顿房价预测
- 47.稀疏矩阵
- 48.回文数之和
- 49.【专业融合:数学】行列式值
- 50.【专业融合:管理】航空旅行
- 51-60(一看到字符串就摆烂用C艹来写了,以后有时间再更新C的写法)
- 51删除前后缀
- 52Atol转换
- 53前后缀移除
- 54大小写交换
- 55字符串后缀
- 56Kids A+B
- 57字符串替换(WA)
- 58字符串切片
- 59元宇宙A+B
- 60分离字符串
- 61-70
- 61【专业融合:生物】DNA双螺旋结构
- 62【专业融合:网安】加密字串
- 63三元搜索
- 64循环排序
- 65【专业融合:自动化】PID控制
- 66有效表达式
- 67专业融合:电子Arduino显示
- 68【专业融合:建筑】长安
- 69时钟A-B
- 70【专业融合:通信】GPS通讯协议
- 70-80
- 71【专业融合:材料】晶体密度
- 72【专业融合:机械】几何约束
- 73【专业融合:化学】原子计数
- 74【专业融合:航海】水下声学定位
- 75【专业融合:动能】热能计算
- 76【专业融合:热血】火箭发射模拟
- 77成绩单
- 78【专业融合:天文】日出日落时间(WA)
- 79【专业融合:数学】中位数
- 80【专业融合:航天】卫星定位
- 80-90
- 81【算法策略:动态规划】上楼梯
- 82【算法策略:动态规划】挑选
- 83【算法策略:回溯】和字符串
- 84【算法策略:贪心】汤包
- 85【算法策略:优雅的策略】危险的组合
- 86【算法策略:动态规划】打字机
- 87【算法策略:分治】子数组最大和
- 88【算法策略:贪心】绝对差
- 89【算法策略:回溯】游乐园
- 90【算法策略:贪心】三角形
- 91-100
- 91【考试题型:字符串】左右操作
- 92【考试题型:输入输出】气体扩散
- 93【考试题型:文件】平方根
- 94【考试题型:结构体】空中交通管制
- 95【考试题型:循环(迭代)】圆周率Π
- 96【考试题型:分支选择】马赫数
- 97【考试题型:数组】重复元素
- 98【考试题型:枚举】机场翻牌显示
- 99【考试题型:算法策略(递归分治贪心动态规划)】零钞
- 100【考试题型:函数(递归)】阿克曼函数
已更新到100题,3道WA,飞机、字符串替换、日出日落
代码托管仓库和pdf代码总结
西工大考研复试机试也可以参考一下,可能会有类似题目。
github仓库:link
70-100题的题目截图在github仓库和pdf总结中可见。
如果github日常抽风无法正常访问,可以访问下面的gitee仓库。
gitee仓库:link
查看pdf请访问github:下载pdf
1-10
1.Hello World
#include <stdio.h>int main()
{printf("Hello World");
}
2.A+B
#include <stdio.h>int main(){int a,b;scanf("%d %d",&a,&b);int c = a+b;printf("%d",c);
}
3.数据类型大小及范围
#include <stdio.h>
#include <limits.h>int main() {int choice;scanf("%d", &choice);switch (choice) {case 1:printf("%zu,%d,%d\n", sizeof(char), CHAR_MIN, CHAR_MAX);break;case 2:printf("%zu,%d,%u\n", sizeof(unsigned char), 0, UCHAR_MAX);break;case 3:printf("%zu,%d,%d\n", sizeof(short), SHRT_MIN, SHRT_MAX);break;case 4:printf("%zu,%d,%u\n", sizeof(unsigned short), 0, USHRT_MAX);break;case 5:printf("%zu,%d,%d\n", sizeof(int), INT_MIN, INT_MAX);break;case 6:printf("%zu,%u,%u\n", sizeof(unsigned int), 0, UINT_MAX);break;case 7://printf("%zu,%d,%d\n", sizeof(int), INT_MIN, INT_MAX);printf("%zu,%ld,%ld\n", sizeof(long), LONG_MIN, LONG_MAX);break;case 8:printf("%zu,%u,%lu\n", sizeof(unsigned long), 0UL, ULONG_MAX);//printf("%zu,%u,%u\n", sizeof(unsigned int), 0, UINT_MAX);break;case 9:printf("%zu,%lld,%lld\n", sizeof(long long), LLONG_MIN, LLONG_MAX);break;case 10:printf("%zu,%u,%llu\n", sizeof(unsigned long long), 0ULL, ULLONG_MAX);break;default:printf("无效的编号\n");break;}return 0;
}
4.平均值
#include <stdio.h>int main(){long long a,b;scanf("%lld %lld",&a,&b);long long c=a/2+b/2;printf("%lld\n",c);
}
5.进制转换
#include<stdio.h>
int main()
{int num;scanf("%d",&num);printf("%X,%o",num,num); return 0;}
6.浮点数输出
#include <stdio.h>int main(){double a;scanf("%lf",&a);printf("%.6lf,%.2lf,%.8lf",a,a,a);}//1234567.8912345678
7.动态宽度输出
#include <stdio.h>int main(){long long m,n,t,num;int zero=0;num=0;scanf("%lld %lld",&m,&n);t=m;while(t>0){t=t/10;num++;}if(num==n||num>n){printf("%lld\n",m);}else{num=n-num;while(num--){printf("%d",zero);}printf("%lld\n",m);}}
8.计算地球上两点之间的距离
#include <stdio.h>
#include <math.h>#define EARTH_RADIUS 6371// 地球半径(公里)// 使用Haversine公式计算两个地点之间的距离(直接使用弧度)
// 将角度转换为弧度
double degreesToRadians(double degrees) {return degrees * M_PI/ 180.0;
}/*double calculateHaversineDistance(double lat1, double lon1, double lat2, double lon2) {double dlat = lat2 - lat1;double dlon = lon2 - lon1;double a = sin(dlat/2) * sin(dlat/2) + cos(lat1) * cos(lat2) * sin(dlon/2) * sin(dlon/2);double c = 2 * atan2(sqrt(a), sqrt(1-a));return EARTH_RADIUS * c;
}*/int main() {double lat1, lon1, lat2, lon2;scanf("%lf %lf", &lat1, &lon1);scanf("%lf %lf", &lat2, &lon2);double lat=degreesToRadians(lat2-lat1);double lon=degreesToRadians(lon2-lon1);lat1=degreesToRadians(lat1);lat2=degreesToRadians(lat2);double havlat=sin(lat/2)*sin(lat/2);//havlat=round(havlat*10000.0)/10000.0;double havlon=sin(lon/2)*sin(lon/2);//havlon=round(havlon*10000.0)/10000.0;double coslat=cos(lat1)*cos(lat2);//coslat=round(coslat*10000.0)/10000.0;double havdr=havlat+coslat*havlon;//havdr=round(havdr*10000.0)/10000.0;double dr=acos(1-2*havdr);//dr=round(dr*10000.0)/10000.0;double d=dr*EARTH_RADIUS;d=round(d*10000.0)/10000.0;// double distance = calculateHaversineDistance(lat1, lon1, lat2, lon2);printf("%.4lfkm\n", d);return 0;
}/*
34.260958 108.942369
55.755825 37.617298*/
9.风寒指数
#include <stdio.h>
#include <math.h>int main(){double v,t;scanf("%lf %lf",&v,&t);double result=13.12+0.6215*t-11.37*pow(v,0.16)+0.3965*t*pow(v,0.16);printf("%.0lf",result);
}
10.颜色模型转变
#include <stdio.h>
#include <stdlib.h>
#include <math.h>int main()
{double H,S,V,MIN,MAX;double R,G,B;scanf("%lf %lf %lf", &R, &G, &B);R=R*100.000000;G=G*100.000000;B=B*100.000000;V=fmax(R,G);V=fmax(V,B);MAX=V;V=MAX/255.0000000;MIN=fmin(R,G);MIN=fmin(MIN,B);if(V==0){S=0;}else{S=100.00000000*(MAX-MIN)/MAX;}if(MAX==R){H=0+60.0000000*((G-B)/(MAX-MIN));}else if(MAX==G){H=120.000000+60.0000000*((B-R)/(MAX-MIN));}else if(MAX==B){H=240.0000000+60.000000*((R-G)/(MAX-MIN));}if(H<0){H=H+360.000000;}printf("%.4lf,%.4lf%%,%.4lf%%\n", round(H * 10000.0) / 10000.0, round(S * 10000.0) / 10000.0, round(V * 10000.0) / 10000.0);
}
11-20
11.倍数和
#include <stdio.h>long long solve(long long n){long long result =0;for(int i=1; i<n; i++){if(i%3==0||i%5==0){result+=i;}}return result;
}long long sum_of_multiples(long long n) {n--; long long sum3 = (n / 3) * (3 + (n / 3) * 3) / 2;long long sum5 = (n / 5) * (5 + (n / 5) * 5) / 2;long long sum15 = (n / 15) * (15 + (n / 15) * 15) / 2;return sum3 + sum5 - sum15;
}int main(){long long T,n;scanf("%lld",&T);int arr[100000];for(long long i=0;i<T;i++){scanf("%lld",&n);//printf("%lld\n",solve(n));//printf("%lld\n",sum_of_multiples(n));arr[i]=sum_of_multiples(n);}for(long long i=0;i<T;i++){printf("%lld\n",arr[i]);}
}
12.分数的加、减、乘、除法
#include <stdio.h>
#include <string.h>//求最大公约数
int gcd(int x,int y){if(y==0){return x;}return gcd(y,x%y);}int main(){int a1,a2,b1,b2;int result1,result2,common_divisor;scanf("%d/%d",&a1,&a2);scanf("%d/%d",&b1,&b2);//加法result1=a1*b2+a2*b1;result2=a2*b2;common_divisor=gcd(result1,result2);result1/=common_divisor;result2/=common_divisor;printf("(%d/%d)+(%d/%d)=%d/%d\n",a1,a2,b1,b2,result1,result2);//减法result1=a1*b2-a2*b1;result2=a2*b2;common_divisor=gcd(result1,result2);result1/=common_divisor;result2/=common_divisor;printf("(%d/%d)-(%d/%d)=%d/%d\n",a1,a2,b1,b2,result1,result2);//乘法result1=a1*b1;result2=a2*b2;common_divisor=gcd(result1,result2);result1/=common_divisor;result2/=common_divisor;printf("(%d/%d)*(%d/%d)=%d/%d\n",a1,a2,b1,b2,result1,result2);//除法result1=a1*b2;result2=a2*b1;common_divisor=gcd(result1,result2);result1/=common_divisor;result2/=common_divisor;printf("(%d/%d)/(%d/%d)=%d/%d\n",a1,a2,b1,b2,result1,result2);return 0;}
13.乘数模
#include <stdio.h>int main(){long long a,b,m;scanf("%lld %lld %lld", &a, &b, &m);long long result;result = (a%m)*(b%m)%m;printf("%lld\n", result);}
14.对称数
#include <stdio.h>char str[1000]="0";
int len=0;int judge(int i){if(str[i]=='1'&&str[len-i-1]=='1'){return 1;}else if(str[i]=='9'&&str[len-i-1]=='6'){return 1;}else if(str[i]=='6'&&str[len-i-1]=='9'){return 1;}return 0;
}int main(){char ch;while((ch = getchar()) !='\n'){str[len]=ch;len++;}int flag=1;//printf("%s\n",str);if(len==1){if(str[0]=='1')flag=1;}else{for(int j=0;j<len;j++){//printf("%d\n",judge(j));if(judge(j)==0){//printf("%d\n",judge(j));flag=0;break;}}}if(flag==0){printf("No\n");}else{printf("Yes\n");}return 0;}
15.比率
#include <stdio.h>//求最大公约数
long long gcd(long long x,long long y){if(y==0){return x;}return gcd(y,x%y);}int main(){double n;scanf("%lf",&n);n=n*100000000000;long long x=(long long )n;long long common_divisor=gcd(100000000000,x);long long a=x/common_divisor;long long b=100000000000/common_divisor;printf("%lld/%lld",a,b);return 0;}
16.操作数
#include <stdio.h>long long solve(long long n){long long result = 0;while(n>0){result = result + n%10;n=n/10;}return result;
}int main(){long long n;scanf("%lld", &n);long long num=0;while(n>0){n=n-solve(n);num++;}printf("%lld\n", num);}
17.级数和
#include <stdio.h>int main() {int n;double sum = 1.2;scanf("%d", &n);printf("1.2");for (int i = 2; i <= n; i++) {if((i+1)%10==0){printf("+%d.%d", i, (i+1)/10);}else{printf("+%d.%d", i, i + 1);}if(i>=9){sum+=i+((double)(i+1))/100.0;}else{sum+=i+((double)(i+1))/10.0;}}printf("=%.2lf\n", sum);return 0;
}
18.组合数
#include <stdio.h>int main(){int n;int sum=0;scanf("%d",&n);for(int i=0;i<=9;i++)for(int j=0;j<=9;j++) for(int k=0;k<=9;k++)for(int x=0;x<=9;x++){if(i+j+k+x==n){sum++;}}printf("%d",sum);
}
19.幂数模
#include <stdio.h>int main(){long long a,b,m;scanf("%lld %lld %lld", &a, &b, &m);long long result=1;while(b!=0){if(b%2){result=(result*a)%m;}a=(a*a)%m;b=b/2;}printf("%lld\n", result);}
20.方阵
#include <stdio.h>int main(){long long n,k,j;scanf("%lld",&n);if(n==0){printf("0\n");}else{for(long long i=0;i<n;i++){for(k=i;k>0;k--){printf("%lld ",k);}for(j=0;j<n-i-1;j++){printf("%lld ",j);}printf("%lld\n",j);}}return 0;}
21-30
21.毕达哥拉斯三元组
#include <stdio.h>int main(){int n;scanf("%d",&n);for(int i=1;i<n;i++)for(int j=i+1;j<n;j++){int k=n-i-j;if(i*i+j*j==k*k){printf("%d\n",i*j*k);}}}
22.倒水
#include <stdio.h>
#include <math.h>
#define MAX 101int m,n,d;
int min_steps=-1;
int used[MAX][MAX]={0};void dfs(int x,int y,int steps){//若找到符合条件,则进行比较,找最小操作数if(x==d||y==d){if(steps <min_steps||min_steps==-1)min_steps=steps; return;}//避免无限深搜,用数组标记搜索过的样例if(used[x][y]==1){return;}used[x][y]=1;//倒空一个杯子dfs(0,y,steps+1);dfs(x,0,steps+1);//装满一个杯子dfs(m,y,steps+1);dfs(x,n,steps+1);//把水从x倒入y里int sum=x+y;int x_next,y_next;if(sum>n){x_next=sum-n;y_next=n;}else{x_next=0;y_next=sum;}dfs(x_next,y_next,steps+1);//把水从y倒入x里if(sum>m){x_next=m;y_next=sum-m;}else{x_next=sum;y_next=0;}dfs(x_next,y_next,steps+1);used[x][y]=0;}int main(){scanf("%d %d %d",&m,&n,&d);dfs(0,0,0);printf("%d\n",min_steps);}
俄罗斯农夫乘法
#include <stdio.h>int main(){long long x,y;scanf("%lld %lld",&x,&y);long long sum=0;while(x>0){printf("%lld %lld\n",x,y);if(x%2){sum+=y;}x=x/2;y=y*2;}printf("%lld",sum);}
24.查找数列
#include <stdio.h>int main(){int n;scanf("%d",&n);int i=1;while(1){if(n-i>0){n=n-i;i++;}else{break;}}printf("%d\n",n);}
25.阶乘倍数
#include <stdio.h>
#include <math.h>int judge(int num){for(int i=2;i<sqrt(num);i++){if(num%i==0){return 1;}}return 0;
}int main(){long long k;scanf("%lld",&k);long long sum=1;long long n=2;if(judge(k)==0&&k>20){printf("%lld\n",k);}else{while(1){sum=sum*n;//printf("%lld\n",sum);if(sum%k==0){printf("%lld\n",n);break;}n++;}}return 0;}
26.最大数字
#include <stdio.h>int judge(int num){int t1=9;while(num>0){int t2=num%10;num/=10;if(t1<t2){return 0;}t1=t2;}return 1;}int main(){int n;scanf("%d",&n);for(int i=n;i>=0;i--){if(judge(i)){printf("%d\n",i);break;}}}
27.余数和
#include <stdio.h>int main(){int n,k;scanf("%d %d",&n,&k);int sum=0;for(int i=1;i<=n;i++){sum+=k%i;}printf("%d\n",sum);}
28.方案数
#include <stdio.h>int n;int judge(int start){int sum=0;for(int i=start;i<n;i++){sum+=i;if(sum==n){return 1;}else if(sum>n){return 0;}}return 0;
}int main(){int nums = 1;scanf("%d",&n);for(int i=1;i<=n/2;i++){nums+=judge(i);}printf("%d\n",nums);}
29.竖式乘法
#include <stdio.h>int count(int n){if(n==0){return 1;}int lens=0;while(n>0){n=n/10;lens++;}return lens;}int main(){int a,b;int result;scanf("%d %d",&a,&b);result = a*b;int len=count(result);len++;int a_len=count(a);int b_len=count(b);for(int i=0;i<len-a_len;i++){printf(" ");}printf("%d\n",a);printf("x");for(int i=0;i<len-b_len-1;i++){printf(" ");}printf("%d\n",b);for(int i=0;i<len;i++){printf("-");}printf("\n");for(int i=0;i<b_len;i++){int tmp=b%10;b=b/10;tmp=a*tmp;int t_len=count(tmp);if(i==b_len-1){printf("+");t_len+=1;}for(int j=0;j<len-i-t_len;j++){printf(" ");}printf("%d",tmp);for(int k=0;k<i;k++){printf(" ");}printf("\n");}for(int i=0;i<len;i++){printf("-");}printf("\n");printf(" %d",result);}
30.好数字
#include <stdio.h>
#include <math.h>
int main(){long long n;scanf("%lld",&n);long long sum=1;long long mod=pow(10,9);mod+=7;for(long long i=0;i<n;i++){if(i%2==0)sum=(sum*5)%mod;elsesum=(sum*4)%mod;}printf("%lld\n",sum);}
31-40
31.素数
#include <stdio.h>
#include <math.h>int is_prime(int n){if(n==2||n==3||n==5){return 1;}if(n%2==0||n%3==0||n%5==0||n==1){return 0;}//枚举素因子,能被整除说明不是素数。for(int i=5;i<=sqrt(n);i+=6){if(n%i==0||n%(i+2)==0){return 0;}}return 1;
}int main(){int a,b;scanf("%d %d",&a,&b);int num=0;for(int i=a;i<=b;i++){if(is_prime(i)){num++;}}printf("%d\n",num);}
32.运动会
#include <stdio.h>
#include <math.h>// 计算欧拉函数
int eulerTotient(int n) {int result=n; // 初始化结果为 n// 遍历从 2 到 sqrt(n) 的所有质数for (int i=2; i<=sqrt(n);i++) {// 如果 i 是 n 的因子if (n%i == 0) {// 不断除以 i,直到它不再是 n 的因子while (n%i == 0) {n/=i;}// 更新结果result-=result/i;}}// 如果 n 是一个大于 1 的质数if (n>1) {result-=result/n;}return result;
}int main(){int n;int sum=0;scanf("%d",&n);for(int i=0;i<n;i++){sum+=eulerTotient(i);}sum=sum*2+1;printf("%d\n",sum);}
33.可变参数累加
#include <stdio.h>
#include <stdarg.h>int sum(int a,...){va_list args;va_start(args, a);int result = 0;result+=a;int next;while(next=va_arg(args, int)){result+=next;}return result;}int main(){int a,b,c,d,e,f;scanf("%d %d %d %d %d %d", &a , &b , &c , &d , &e , &f);int result=sum(a,b,0)-sum(c,d,e,f,0);printf("%d\n", result);}
34哈沙德数
#include <stdio.h>int HarshadNumber(int n){if(n==1){return 0;}int t=n,s=0;while(t){s=s+t%10;t=t/10;}if(s && n%s==0) return n/s;return 0;
}int judge(int n){int tmp=HarshadNumber(n);//printf("%d",tmp);if(tmp==0) return 0;else return 1+judge(tmp);}int main(){int num;scanf("%d", &num);printf("%d\n", judge(num));
}
35.冰雹序列
#include <stdio.h>void collatz(int n){if(n==1){printf("%d",n);return;} if(n%2==1){printf("%d ",n);collatz(3*n+1);} else{printf("%d ",n);collatz(n/2);}
}int main(){int n;scanf("%d",&n);collatz(n);}
36.基恩数
#include <stdio.h>int lsKeith(int N) {int len = 0;int originalN = N;int sequence[100]; int a[100];int sum=0;while(N>0){sequence[len++] = N%10;N=N/10;}int index=0;for(int i=len-1; i>=0; i--){a[index++]=sequence[i];sum+=sequence[i];}int start=0;while(1){a[index++]=sum;sum=sum+sum-a[start++];if(sum==originalN){return 1;}if(sum>originalN){return 0;}}return 0;
}int main() {int N;scanf("%d", &N);if (lsKeith(N) == 1) {printf("Yes\n");} else {printf("No\n");}return 0;
}
37.可变参数平均
#include <stdio.h>
#include <stdarg.h>double avg(int n, ...) {va_list args;va_start(args, n);double sum = 0.0;for (int i = 0; i < n; i++) {sum += va_arg(args, double);}va_end(args);return sum / n;
}int main() {double a, b, c, d, e;scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &e);double result = avg(2, a, b) - avg(3, c, d, e);printf("%.4lf\n", result);return 0;
}
38.光线追踪
在洛谷找到原题,有完美的数学解法。
洛谷
下面代码就当是小丑写的
#include <stdio.h>long long calculateTotalLength(long long N, long long X) {long long totalLength = N;long long N_X=N-X;//镜墙while(N_X>0){if(N_X%X==0){//镜墙的长度刚好可以让一种光长的光线反射到原点long long n=N_X/X;totalLength+=(2*n-1)*X;break;}else if(N_X/X>=1){//不能,则需要计算差多少,最后置换反射光长和镜墙的长度long long n=N_X/X;totalLength+=2*n*X;int tmp=N_X-X*n;N_X=X;X=tmp;}}return totalLength;}int main() {long long N, X;scanf("%lld %lld", &N, &X); long long totalLength;if(N-X>=X)totalLength=calculateTotalLength(N, X);elsetotalLength=calculateTotalLength(N, N-X);printf("%lld\n", totalLength); return 0;
}
39.二进制表示
#include <stdio.h>void binaryRepresentation(int n) {if (n == 0) {return;} else if (n == 1) {printf("2(0)");} else if (n == 2) {printf("2");} else {int power = 0;int temp = 1;while (temp * 2 <= n) {temp *= 2;power++;}if (temp == n) {printf("2(");binaryRepresentation(power);printf(")");} else {if(power == 1) {//括号内的单独为1的数字不需要转换为2(0)printf("2+");}else{printf("2(");binaryRepresentation(power);printf(")+");}binaryRepresentation(n - temp);}}
}int main() {int n;scanf("%d", &n);binaryRepresentation(n);printf("\n");return 0;
}
40.佩尔数
#include <stdio.h>int PB(int n){int p0=0,p1=1,pn,i;for(i=0;i<=n;i++)if(i==0) pn=p0;else if(i==1) pn=p1;else{pn=2*p1+p0;p0=p1;p1=pn;}return pn;
}int PA(int n){if(n==0) return 0;else if(n==1) return 1;if(n%2){return 2*PB(n-1)+PA(n-2);}else{return 2*PA(n-1)+PB(n-2);}
}int pell(int n){if(n%2)return PA(n);elsereturn PB(n);
}int main(){int n;scanf("%d",&n);printf("%d\n",pell(n));
}
41-50
41.【专业融合:管理】货运优化
#include <stdio.h>int main(){int a[7];int a_3[4]={0,5,3,1};while(1){scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]);if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0){break;}int sum=0;sum+=a[6];sum+=a[5];sum+=a[4];int sub=a[2]-a[4]*5;sum+=a[3]/4;if(a[3]%4){sum++;}int x=a_3[a[3]%4];if(sub>=0){if(a[2]-sub>x){sum+=(a[2]-sub-x)/9;if((a[2]-sub-x)%9){sum++;}}}x=36*sum-(a[3]*9+a[2]*4+a[4]*16+a[5]*25+a[6]*36);if(a[1]>x){sum+=(a[1]-x)/36;if((a[1]-x)%36){sum++;}} printf("%d\n",sum);}return 0;}/*
1 1 1 1 1 1
2 2 2 2 2 2
1 2 3 4 5 6
0 0 0 0 0 0
*/
42.完美矩阵
#include <stdio.h>
#include <math.h>int a[300][300];
int counts=0;int is_perfect(int x1, int y1, int x2, int y2){int zeros=0,ones=0;for(int i=x1;i<=x2;i++)for(int j=y1;j<=y2;j++){if(i==x1||i==x2||j==y1||j==y2){if(a[i][j]!=1){return 0;}}else{if(a[i][j]==0){zeros++;}else{ones++;}}}if(abs(zeros-ones)<=1){return 1;}else{return 0;}}void sums(int n,int m){int MIN=fmin(n,m);for(int k=MIN;k>1;k--){for(int i=0;i<n-k+1;i++)for(int j=0;j<m-k+1;j++){if(k==2){int x1=i,x2=i+k-1,y1=j,y2=j+k-1;if(a[x1][y1]==1&&a[x1][y2]==1&&a[x2][y1]==1&&a[x2][y2]==1){counts++;}}else if(is_perfect(i,j,i+k-1,j+k-1)){counts++;}}}}int main(){int n,m;scanf("%d %d",&n,&m);for(int i=0;i<n;i++)for(int j=0;j<m;j++){scanf("%d",&a[i][j]);}sums(n,m);printf("%d\n",counts);return 0;}/*
4 4
1 1 1 1
1 0 1 1
1 1 0 1
1 1 1 1
这道题的描述有问题,只用判定方阵即可,不需要严格到矩阵。
43.【专业融合:航空】飞机起飞速度(没有AC)
43还没有AC
44.【专业融合:物理】蒙特卡罗方法求积分
#include <stdio.h>
#include <stdlib.h>
#include <math.h>int main(){int m,N;double a,b;scanf("%d %lf %lf %d",&m,&a,&b,&N);srand(RAND_MAX);double sum=0;double result;if(m==1){for(int i=0;i<1999;i++){double randomValue=a + ((double)rand() / RAND_MAX) * (b - a);double x1=pow(randomValue,4);double x2=exp(-randomValue);sum+=x1*x2;}result=(b-a)*sum/N;}else if(m==2){for(int i=0;i<1999;i++){double randomValue=a + ((double)rand() / RAND_MAX) * (b - a);sum+=pow(randomValue,2)+1;}result=(b-a)*sum/N;}else if(m==3){for(int i=0;i<1999;i++){double randomValue=a + ((double)rand() / RAND_MAX) * (b - a);sum+=cos(randomValue);}result=(b-a)*sum/N;}else if(m==4){for(int i=0;i<1999;i++){double randomValue=a + ((double)rand() / RAND_MAX) * (b - a);sum+=pow(randomValue,0.5)*(randomValue-2);}result=(b-a)*sum/N;}else if(m==5){for(int i=0;i<1999;i++){double randomValue=a + ((double)rand() / RAND_MAX) * (b - a);sum+=2*sin(randomValue)-5*cos(randomValue);}result=(b-a)*sum/N;}printf("%.6lf\n", result);}
现在1999的位置上本来是N,搜到一个评论说只要样本为1999就能AC,结果还真行,无语了。
45.素数筛选法
#include <stdio.h>
#define N 10000000
int vis[N+1]={0};
int pr[N+1];//存质数
int cnt=0;void Euler_sieve(int n)
{int i,j;for(i=2;i<=n;i++){if(vis[i]==0){pr[cnt++]=i;}for(j=0;j<cnt;j++){if(i*pr[j]>n)//超出范围停止break;vis[i*pr[j]]=1;if(i%pr[j]==0)//找到最小质因子后停止搜索break;}}
}int main(){int n;scanf("%d",&n);Euler_sieve(n);printf("%d\n",cnt);/*for(int i=0;i<cnt;i++){printf("%d\n",pr[i]);}*/return 0;
}
46.【专业融合:计算机】波士顿房价预测
#include <stdio.h>//最小二乘法
struct point{int x;int y;
};int main(){int n;scanf("%d",&n);struct point arr[n];double sum_x=0,sum_y=0;for(int i=0; i<n; i++){scanf("%d %d",&arr[i].x,&arr[i].y);sum_x+=arr[i].x;sum_y+=arr[i].y;}double x_m=sum_x/n;double y_m=sum_y/n;double a=0,b=0;double tmp1,tmp2;for(int i=0; i<n; i++){tmp1+=(arr[i].x-x_m)*(arr[i].y-y_m);tmp2+=(arr[i].x-x_m)*(arr[i].x-x_m);}b=tmp1/tmp2;a=y_m-b*x_m;printf("Y=%.4lf+%.4lf*X",a,b);}/*
7
150 6450
200 7450
250 8445
300 9450
350 11450
400 15450
600 18450
*/
47.稀疏矩阵
#include <stdio.h>int main() {int n, m;scanf("%d %d", &n, &m);int matrix[n][m];for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {scanf("%d", &matrix[i][j]);}}int non_zero_count = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (matrix[i][j] != 0) {non_zero_count++;}}}if (non_zero_count <= 0.05 * n * m || non_zero_count == n || non_zero_count == m) {printf("Yes\n");} else {printf("No\n");}return 0;
}/*
4 4
5 0 0 0
0 8 0 0
0 0 3 0
0 6 0 0
*/
48.回文数之和
#include <stdio.h>int is_palindrome(int num){int source=num;int result=0;while(num>0){result=result*10+num%10;num=num/10;}if(source==result) return 1;return 0;
}int convert(int num,int k){int result=0;int base=1;while(num>0){result=result+(num%k)*base;num=num/k;base*=10;}return result;
}int main() {int n,k;scanf("%d %d",&n,&k);int sum=0;for(int i=1;i<n;i++){if(is_palindrome(i)&&is_palindrome(convert(i,k)))sum+=i; }printf("%d\n",sum);return 0;
}
49.【专业融合:数学】行列式值
#include <stdio.h>int laplace(int a[100][100],int r,int c,int n);
int determinant(int a[100][100],int n);int main(){int n;int a[100][100];int result;scanf("%d",&n);for(int i=0; i<n; i++)for(int j=0; j<n; j++)scanf("%d",&a[i][j]);result=determinant(a,n);printf("%d\n",result);return 0;}int determinant(int a[100][100],int n){int result=0;int tmp=1;int subset;if(n==1){result=a[0][0];}else{for(int i=0;i<n;i++){subset=laplace(a,i,0,n);result+=a[i][0]*tmp*subset;tmp*=-1;}}return result;
}
int laplace(int a[100][100],int r,int c,int n){int result=0;int subset[100][100];int s_i,s_j;for(int i=0;i<n;i++)for(int j=0;j<n;j++){s_i=i;s_j=j;if(i!=r&&j!=c){if(i>r) i--;if(j>c) j--;subset[i][j] = a[s_i][s_j];i=s_i;j=s_j;}}if(n>=2){result=determinant(subset,n-1);}return result;
}/*
3
2 6 3
1 0 2
5 8 4
*/
50.【专业融合:管理】航空旅行
#include <stdio.h>int main(){int n;scanf("%d",&n);int a[3];int d,e;for(int i=0;i<n;i++){int flag=0;scanf("%d %d %d %d %d",&a[0],&a[1],&a[2],&d,&e);for(int j=0;j<2;j++)for(int k=0;k<2-j;k++){if(a[k]>a[k+1]){int tmp=a[k];a[k]=a[k+1];a[k+1]=tmp;}}if(a[2]+a[1]<=d&&a[0]<=e){flag=1;}if(flag) printf("YES\n");else printf("NO\n");}}/*
3
1 1 1 15 5
8 7 6 15 5
8 5 7 15 6
*/
51-60(一看到字符串就摆烂用C艹来写了,以后有时间再更新C的写法)
51删除前后缀
#include <bits/stdc++.h>using namespace std;void str_removeprefix(string str,string words){while(str.compare(0,words.length(),words)==0&&str.length()>=words.length()){str.erase(0,words.length());} cout << str << endl; }void str_removesuffix(string str,string words){while(str.compare(str.length()-words.length(),words.length(),words)==0&&str.length()>=words.length()){str.erase(str.length()-words.length());}cout << str << endl;
}int main(){string str,words;getline(cin,str);getline(cin,words);str_removeprefix(str,words);str_removesuffix(str,words);}/*
antiantianwartiantianti
anti
*/
52Atol转换
#include <bits/stdc++.h>using namespace std;int Atol(string str){long long res=0;int len = str.length();int sign = 1;int i=0;while(str[i]==' '){i++;}if(str[i]=='+'){sign=1;i++;}else if(str[i]=='-'){sign=-1;i++;}while(str[i]>='0' && str[i]<='9'){res=res*10+(str[i]-'0');if(res*sign>INT_MAX) return INT_MAX;if(res*sign<INT_MIN) return INT_MIN;i++;}return res*sign;}int main(){string str;getline(cin,str);cout<<Atol(str)<<endl;}/*
-123x+123
*/
53前后缀移除
#include <bits/stdc++.h>
using namespace std;void str_lstrip(string& str, string& chars) {int pos = str.find_first_not_of(chars);if (pos != string::npos) {str.erase(0, pos);}}void str_rstrip(string& str, string& chars) {int pos = str.find_last_not_of(chars);if (pos != string::npos) {str.erase(pos + 1);}
}void str_strip(string& str, string& chars) {str_lstrip(str, chars);str_rstrip(str, chars);
}int main() {string str;string chars;string tmp;getline(cin, str);tmp=str;getline(cin, chars);str_lstrip(str, chars);cout << str << endl;str=tmp;str_rstrip(str, chars);cout << str << endl;str=tmp;str_strip(str, chars);cout << str << endl;return 0;
}/*
www.example.com
cmowz.
*/
54大小写交换
#include <bits/stdc++.h>using namespace std;void str_swapcase(string &str){for(int i=0; i<str.length();i++){if(str[i]>='A'&&str[i]<='Z'){str[i]+='a'-'A';}else if(str[i]>='a'&&str[i]<='z'){str[i]-='a'-'A';}}
}int main(){string str;getline(cin, str);str_swapcase(str);cout << str <<endl;
}/*
Hello world
*/
55字符串后缀
#include <bits/stdc++.h>using namespace std;int str_endswith(string str,string suffix){if(str.length()<suffix.length()) return 0;int len=str.length()-suffix.length();str.erase(0,len);if(str==suffix)return 1;return 0;}int main(){string str,suffix;getline(cin, str);getline(cin, suffix);if(str_endswith(str, suffix))cout<<"Yes"<<endl;elsecout<<"No"<<endl;}/*
hello world!
world!
*/
56Kids A+B
#include <bits/stdc++.h>using namespace std;map<string, int> numberMap = {{"zero", 0}, {"one", 1}, {"two", 2}, {"three", 3}, {"four", 4},{"five", 5}, {"six", 6}, {"seven", 7}, {"eight", 8}, {"nine", 9},{"ten", 10}, {"eleven", 11}, {"twelve", 12}, {"thirteen", 13},{"fourteen", 14}, {"fifteen", 15}, {"sixteen", 16}, {"seventeen", 17},{"eighteen", 18}, {"nineteen", 19}, {"twenty", 20}, {"thirty", 30},{"forty", 40}, {"fifty", 50}, {"sixty", 60}, {"seventy", 70},{"eighty", 80}, {"ninety", 90}
};map<int,string> enmap={{0,"zero"},{1,"one"},{2,"two"},{3,"three"},{4,"four"},{5,"five"},{6,"six"},{7,"seven"},{8,"eight"},{9,"nine"},{10,"ten"},{11,"eleven"},{12,"twelve"},{13,"thirteen"},{14,"fourteen"},{15,"fifteen"},{16,"sixteen"},{17,"seventeen"},{18,"eighteen"},{19,"nineteen"},{20,"twenty"},{30,"thirty"},{40,"forty"},{50,"fifty"},{60,"sixty"},{70,"seventy"},{80,"eighty"},{90,"ninety"}
};int convert(string a){int num;int pos=a.find('-');if(pos!=-1){string x=a.substr(0,pos);string y=a.substr(pos+1);num=numberMap[x]+numberMap[y];}else{num=numberMap[a];}return num;
}void convert_E(int a){//cout<<"a:"<<a<<endl;int x=a/10;int y=a%10;//cout<<x<<" "<<y<<endl;if(x>1&&y)cout<<enmap[x*10]<<"-"<<enmap[y]<<endl;else if(x>=1)cout<<enmap[a]<<endl;elsecout<<enmap[y]<<endl;
}int main(){string a,b;int num_a,num_b;cin>>a>>b;num_a=convert(a);num_b=convert(b);int res=num_a+num_b;convert_E(res);}/*
twenty-seven fifty-four
*/
57字符串替换(WA)
#include <bits/stdc++.h>using namespace std;void str_replace(string str,string olds,string news){string res="";while(1){int pos=str.find(olds);if(pos==-1){res+=str;break;}else{res=res+str.substr(0,pos)+news;str=str.substr(pos+olds.length());}}cout<<res<<endl;}int main(){string str,olds,news;getline(cin,str);getline(cin,olds);getline(cin,news);str_replace(str,olds,news);}/*
xx is the best,xx for xxer
xx
nwpu
*/
58字符串切片
#include <bits/stdc++.h>using namespace std;void str_slice(string src,int start,int stop,int step){string res="";if(start<0)start=src.length()+start;if(stop<0)stop=src.length()+stop;if(stop>=start)for (int i = start; i < stop; i += step) {res += src[i];}elsefor (int i = start; i > stop; i += step) {res += src[i];}cout<<res<<endl;}int main() {string src;cin >> src;int t;cin >> t;for (int i = 0; i < t; ++i) {int n;cin >> n;int start, stop, step;if (n == 3) {cin >> start >> stop >> step;} else if (n == 2) {cin >> start >> stop;step = 1;} else if (n == 1) {cin >> start;stop = src.length();step = 1;}str_slice(src, start, stop, step);}return 0;
}/*
ABCDEFGHI
8
2 2 7
2 -7 -2
2 2 -5
3 2 7 2
3 6 1 -2
2 0 3
1 6
3 -1 -10 -1
*/
59元宇宙A+B
#include <bits/stdc++.h>using namespace std;int convert_char_to_int(char ch){if(ch>='0'&&ch<='9')return ch-'0';else if(ch>='A'&&ch<='Z')return ch-'A'+10;}char convert_int_to_char(int num){if(num>=0&&num<=9){return num+'0';}else if(num>=10&&num<=36){return num+'A'-10;}
}string add(string a,string b){long long num1=0,num2=0;for(int i=0;i<a.length();i++){num1=num1*36+convert_char_to_int(a[i]);} for(int i=0;i<b.length();i++){num2=num2*36+convert_char_to_int(b[i]);}long long res=num1+num2;string str="";if(res==0){return "0";}while(res>0){str+=convert_int_to_char(res%36);res=res/36;} return str;}int main(){string a,b;cin>>a>>b;string str=add(a,b);for(int i=str.length()-1;i>=0;i--){cout<<str[i];}cout<<endl;
}
60分离字符串
#include <bits/stdc++.h>using namespace std;void str_split(string str,string sep){while(1){int pos=str.find(sep);if(pos==-1){cout<<str<<endl;break;}else{cout<<str.substr(0,pos)<<endl;str=str.substr(pos+sep.length());}}
}int main(){string str,sep;getline(cin,str);getline(cin,sep);str_split(str,sep);}
61-70
61【专业融合:生物】DNA双螺旋结构
#include <stdio.h>void print_dna1(){printf(" AT \n");printf(" T--A \n");printf(" A----T \n");printf("T------A\n");printf("T------A\n");printf(" G----C \n");printf(" T--A \n");printf(" GC \n");
}void print_dna2(){printf(" CG \n");printf(" C--G \n");printf(" A----T \n");printf("A------T\n");printf("T------A\n");printf(" A----T \n");printf(" A--T \n");printf(" GC \n");
}void print_dna3(){printf(" AT \n");printf(" C--G \n");printf(" T----A \n");printf("C------G\n");printf("C------G\n");printf(" T----A \n");printf(" G--C \n");printf(" AT \n");
}int main(){int n;scanf("%d",&n);for(int i=1;i<=n/2;i++){if(i%3==1) print_dna1();else if(i%3==2) print_dna2();else if(i%3==0) print_dna3();}return 0;}
62【专业融合:网安】加密字串
#include <bits/stdc++.h>using namespace std;int main(){string s;int x;cin>>s;cin>>x;int alphabet[26]={0};for(int i=0;i<s.length();i++){alphabet[s[i]-'a']++;}for(int i=0;i<s.length();i++){char tmp;if(alphabet[s[i]-'a']%2){tmp=((int)(s[i]-x-'a')%26+26)%26+'a';}else{tmp=((int)(s[i]+x-'a')%26+26)%26+'a';}s[i]=tmp;}cout<<s<<endl;}
63三元搜索
#include <stdio.h>int main(){int n;scanf("%d",&n);int a[n];for(int i=0; i<n; i++){scanf("%d",&a[i]);}int key;scanf("%d",&key);int left=0,right=n-1;int mid1,mid2;int result;while(1){if(key>a[right]){result=-1;break;}if(key<a[left]){result=-1;break;}mid1=left+(right-left)/3;mid2=right-(right-left)/3;if(a[mid1]==key) {result=mid1;break;}if(a[mid2]==key){ result=mid2;break;}if(mid1==mid2){result=-1;break;}if(key<a[mid1]) right=mid1-1;else if(key>a[mid2]) left=mid2+1;else if(key>a[mid1]&&key<a[mid2]){left=mid1+1;right=mid2-1;}}printf("%d in [%d]",key,result);
}/*
9
1 2 3 4 5 6 7 8 9
6
*/
64循环排序
#include <stdio.h>int main(){int n;scanf("%d",&n);int a[n];for(int i=0; i<n; i++){scanf("%d",&a[i]);}for(int j=0; j<n-1; j++){int item= a[j];int pos= j;for(int i=j+1; i<n;i++){if(a[i]<item) pos++;}if(pos==j) continue;int tmp=a[pos];a[pos]=item;item=tmp;while(pos!=j){pos=j;for(int k=j+1;k<n;k++){if (a[k] < item) {pos++;}}while (item == a[pos]) {pos++;}int temp = a[pos];a[pos] = item;item = temp;}}for (int i = 0; i < n; ++i) {printf("%d ", a[i]);}}
/*
8
1 8 3 9 10 10 2 4
*/
65【专业融合:自动化】PID控制
#include <stdio.h>typedef struct PIDController{double Kp,Ki,Kd;//比例、积分、微分系数double preError,integral;//前次误差、积分
}PIDData;//PID数据类型void PIDInit(PIDData *pid) {pid->Kp = 0;pid->Ki = 0;pid->Kd = 0;pid->preError = 0;pid->integral = 0;
}double PIDCalculate(PIDData *pid,double setpoint,double measuredValue){double error=setpoint-measuredValue;pid->integral+=error;double diff=error-pid->preError;pid->preError=error;double output=pid->Kp*error+pid->Ki*pid->integral+pid->Kd*diff;return output;}int main(){double setpoint,measuredValue;PIDData pid;int n;PIDInit(&pid);scanf("%lf %lf %lf",&pid.Kp,&pid.Ki,&pid.Kd);scanf("%lf %lf",&setpoint,&measuredValue);scanf("%d",&n);for(int i=1; i<=n;i++){double output=PIDCalculate(&pid,setpoint,measuredValue);//这里本应该打印ouput变量才对,但是noj上的输出是measuredValuemeasuredValue+=output;printf("%d %.6lf\n",i,measuredValue);}}/*
0.1 0.01 0.05
100 0
100
*/
66有效表达式
#include <stdio.h>int n;
int sum=0;
int flag1=0,flag2=0;
void solve(int x){if(x==2*n){sum++;return ;}else{if(flag1+1<=n) { flag1++;solve(x+1);flag1--;}if(flag1-flag2>0&&flag2+1<=n) {flag2++;solve(x+1);flag2--;}}}int main(){scanf("%d",&n);flag1=1;solve(1);printf("%d\n",sum);}
67专业融合:电子Arduino显示
#include <stdio.h>int a[10]={6,2,5,5,4,5,6,3,7,6};int convert(int x){int res=0;if(x==0) return a[x];while(x){res+=a[x%10];x=x/10;}return res;
}int calculate(int n){n=n-4;int sum=0;for(int i=0; i<=2000;i++)for(int j=0;j<=2000;j++){int m=i+j;if(convert(i)+convert(j)+convert(m)==n) {//printf("%d+%d=%d\n",i,j,m);sum++;}}return sum;
}int main(){int n;scanf("%d", &n);printf("%d\n", calculate(n));
}
68【专业融合:建筑】长安
#include <stdio.h>
int bx,by,px,py;
int sum=0;
int next[2][2]={{0,1},{1,0}};
void calculate(int x,int y){//printf("x: %d y: %d\n",x,y);if(x==bx&&y==by){sum++;return ;}else{for(int i=0;i<2;i++){int x1=x+next[i][0];int y1=y+next[i][1];if((x1==px&&y1==py)||x1>bx||y1>by){continue;}calculate(x1,y1);}}}int main(){while(scanf("%d %d %d %d",&bx,&by,&px,&py)){if(bx==0&&by==0&&px==0&&py==0){break;}sum=0;//初始坐标需要选择(1,1)calculate(1,1);printf("%d\n",sum);}}/*
8 6 5 3
8 6 8 6
8 6 9 6
0 0 0 0*/
69时钟A-B
#include <stdio.h>
#include <time.h>int main() {int yearA, monthA, dayA;scanf("%d %d %d", &yearA, &monthA, &dayA);int yearB, monthB, dayB;scanf("%d %d %d", &yearB, &monthB, &dayB);// 构建tm结构体表示A和B的日期时间struct tm timeA = {0};struct tm timeB = {0};timeA.tm_year = yearA - 1900; timeA.tm_mon = monthA - 1; timeA.tm_mday = dayA;timeB.tm_year = yearB - 1900;timeB.tm_mon = monthB - 1;timeB.tm_mday = dayB;time_t timestampA = mktime(&timeA);time_t timestampB = mktime(&timeB);double difference = difftime(timestampA, timestampB);printf("%.6lf\n", difference);return 0;
}/*
2021 1 2
2021 1 1
*/
70【专业融合:通信】GPS通讯协议
NMEA-0183是一种组合电气和数据规范 (https://en.wikipedia.org/wiki/NMEA_0183),
用于海洋电子设备 (例如回声测深
仪、声纳、风速计、陀螺罗盘、自动驾驶仪、GPS 接收器和许多其他类型的仪器)之间的通信。GPS接收机根据NMEA-0183协议
的标准规范,将位置、速度等信息通过串口传送到PC机、PDA等设备。
NMEA-0183协议定义的语句非常多,但是常用的只有GPGGA、GPGSA、GPGSV、GPRMC、GPVTG、GPGLL等。其中
GPRMC语句的格式如下,GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,A50
整条语句是一个文本行,行中以逗号隔开各个字段,每个字段的大小(长度)不一
(1) 字段0:
GPRMC,语句ID,表明该语为RecommendedMinimumSpecificGPS/TRANSITData,含义是推荐最小定位信息。
(2)字段1:UTC时间,hhmmss.sss格式
(3)字段2: 状态,A=定位,V=未定位
(4)字段3,纬度ddmm.mmmm,度分格式(前导位数不足则补0)
(5)字段4:纬度N(北纬)或S(南纬)
(6)字段5:经度dddmm.mmmm,度分格式(前导位数不足则补0)
(7)字段6:经度E(东经)或W(西经)
(8)字段7:速度,节,Knots
(9)字段8:方位角,度
(10)字段9:UTC日期,DDMMYY格式
(11)字段10:磁偏角,(000-180)度(前导位数不足则补0)
(12)字段11:磁偏角方向,E=东W=西(13)
(13)字段16:校验值这里,*为校验识别符,其后面两位数为校验和,代表"$“和”*"之间所有字符(不包括这两个字符)的异或值的十六进制值,如上面这条例句的校验和是十六进制的50。
C语言^运算符的作用是异或。
将“$”和“*”之间所有的字符做运算(第一个字符和第二个字符异或,结果再和第三个字符异或,依此类推)之后的值对65536取余后的结果,
应该和后面的两个十六进制数字的值相等,否则的话说明这条语句在传输中发生了错误。注意这个十六进制值字母是大写的。
程序的功能是读入一系列的GPS语句,其中包合GPRMC,也包含其他语句,最后一行总是END。
程序仅处理GPRMC语句,计算校验和,找出校验正确的语句,提取字段2的UTC时间,换算成北京时间 (+8小时)输出,小数点忽略。如果是校验错误的语句,输出erTor。如果不是GPRMC语句,什么也不做。
Input
输入若干行GPS语句字符串,直到“END”为止。
Output
依据前述要求输出相应结果。样例解释如下
第1行,GPRMC语句,从到之间的异或值十六进制为50,与末尾校验值相等,为有效的
GPRMC语句。UTC时间为\02小时48分13.640秒//,换算成北京时间为10:48 :13(输出结果)第2行,不是GPRMC语句,什
么也不做。
第3行,GPRMC语句,从到之间的异或值十六进制为53,与末尾校验值不相等,输出error
第4行,不是GPRMC语句,什么也不做。第5行,GPRMC语句,从
到* 之间的异或值十六进制为48,与末尾校验值相等,为有效的GPRMC语句。UTC时间为“11小时17分24.681秒”,换算成北京时
间为19:17:24 (输出结果)
第6行,不是GPRMC语句,什么也不做。
第7行,END,结束。
Sample Input
$GPRMC,024813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,A50
$GPGSV,3,1,11,10,63,137,17,07,61,098,15,05,59,290,20,08,54,157,3070
$GPRMC,194548.127,A,5230.657,N,01325.713,E,3968.7,122.8,200220,000.0,W44
$GPGGA,092750.000,5321.6802,N,00630.3372,w,1,8,1.03,61.7,M,55.2,M,76
$GPRMC,111724.681,A,5231.801,N,01329.267,E,1289.3,000.0,291123,000.0,W48
$GNVTG,112.99,T,109.99,M,0.15,N,0.08,K,A3B
END
Sample Output
10:48:13
error
19:17:24
#include <bits/stdc++.h>using namespace std;string out[100];
int k=0;int check(string str){int i,result;for(result=str[1],i=2;str[i]!='*';i++){result^=str[i];}return result;
}int convert(string str){int res=0;res=stoi(str,0,16);//cout<<res<<endl;return res;
}void convert_BeingTime(string utcTime){int hour=stoi(utcTime.substr(0,2));int B_hour=(hour+8)%24;if(B_hour/10==0)out[k++]="0"+to_string(B_hour)+":"+utcTime.substr(2,2)+":"+utcTime.substr(4,2);elseout[k++]=to_string(B_hour)+":"+utcTime.substr(2,2)+":"+utcTime.substr(4,2);}int main(){string str;while(cin>>str){if(str=="END") break;if(str.compare(0,6,"$GPRMC")==0){size_t asteriskPos = str.find('*');if(asteriskPos!=string::npos){int checksum=check(str);int senchecksum=convert(str.substr(asteriskPos + 1, 2));if(checksum!=senchecksum) {out[k++]="error";}else{// 提取UTC时间字段string utcTime = str.substr(7, 6);convert_BeingTime(utcTime);}}}}for(int i=0;i<k;i++){cout<<out[i]<<endl;}
}/*
$GPRMC,014813.640,A,3158.4608,N,11848.3737,E,10.05,324.27,150706,,,A*50
$GPGSV,3,1,11,10,63,137,17,07,61,098,15,05,59,290,20,08,54,157,30*70
$GPRMC,194548.127,A,5230.657,N,01325.713,E,3968.7,122.8,200220,000.0,W*44
$GPGGA,092750.000,5321.6802,N,00630.3372,w,1,8,1.03,61.7,M,55.2,M,,*76
$GPRMC,111724.681,A,5231.801,N,01329.267,E,1289.3,000.0,291123,000.0,W*48
$GNVTG,112.99,T,109.99,M,0.15,N,0.08,K,A*3B
END
*/
70-80
71【专业融合:材料】晶体密度
转自 pastebin
#include <bits/stdc++.h>
#include <cmath>
#include <math.h>using namespace std;struct Atom{string name;double mass;double APF;//原子堆积因子double r;//原子半径
};Atom elemList[] =
{{ "Po", 208.998, 0.52360, 1.68 },{ "Li", 6.941, 0.68017, 1.52 },{ "Na", 22.989770, 0.68017, 1.86 },{ "Cr", 51.9961, 0.68017, 1.28 },{ "Mn", 54.938049, 0.68017, 1.27 },{ "Fe", 55.845, 0.68017, 1.26 },{ "Mo", 95.94, 0.68017, 1.39 },{ "Ta", 180.9749, 0.68017, 1.46 },{ "Al", 26.981538, 0.74048, 1.43 },{ "Ca", 40.078, 0.74048, 1.97 },{ "Ni", 58.6934, 0.74048, 1.24 },{ "Cu", 63.546, 0.74048, 1.28 },{ "Ge", 72.64, 0.74048, 1.22 },{ "Ag", 107.8682, 0.74048, 1.44 },{ "Pt", 195.078, 0.74048, 1.39 },{ "Au", 196.96655, 0.74048, 1.44 },{ "Pb", 207.2, 0.74048, 1.75 }
};int main(){int n;cin>>n;string atoms;for(int i=0; i<n; i++){cin >> atoms;for(int j=0;j<17;j++){if(elemList[j].name==atoms){double V=4.0/3.0*M_PI*pow(elemList[j].r,3);double density=10.0*elemList[j].mass*elemList[j].APF/6.022/V;printf("%.2lf\n",density);break;}}}}/*
3
Po
Mo
Cu
*/
72【专业融合:机械】几何约束
#include <stdio.h>
#include <math.h>struct Point {double x, y;
};struct Segment {struct Point start, end;int index; // 线段的索引号
};// 判断两线段是否相交
int doIntersect(struct Segment s1, struct Segment s2) {double x1 = s1.start.x, y1 = s1.start.y;double x2 = s1.end.x, y2 = s1.end.y;double x3 = s2.start.x, y3 = s2.start.y;double x4 = s2.end.x, y4 = s2.end.y;double a1 = y2 - y1;double b1 = x1 - x2;double c1 = a1 * x1 + b1 * y1;double a2 = y4 - y3;double b2 = x3 - x4;double c2 = a2 * x3 + b2 * y3;double determinant = a1 * b2 - a2 * b1;if (determinant == 0) {// 平行线段return 0;} else {double intersectX = (b2 * c1 - b1 * c2) / determinant;double intersectY = (a1 * c2 - a2 * c1) / determinant;// 检查交点是否在线段内if (intersectX >= fmin(x1, x2) && intersectX <= fmax(x1, x2) &&intersectX >= fmin(x3, x4) && intersectX <= fmax(x3, x4) &&intersectY >= fmin(y1, y2) && intersectY <= fmax(y1, y2) &&intersectY >= fmin(y3, y4) && intersectY <= fmax(y3, y4)) {return 1;} else {return 0;}}
}int main() {int n;scanf("%d", &n);struct Segment segments[n];// 读取线段信息for (int i = 0; i < n; ++i) {scanf("%lf %lf %lf %lf", &segments[i].start.x, &segments[i].start.y,&segments[i].end.x, &segments[i].end.y);segments[i].index = i + 1;}int intersectionCount = 0;// 检查线段相交for (int i = 0; i < n - 1; ++i) {for (int j = i + 1; j < n; ++j) {if (doIntersect(segments[i], segments[j])) {printf("X: #%d #%d\n", segments[i].index, segments[j].index);intersectionCount++;}}}// 输出相交的总数printf("n=%d\n", intersectionCount);return 0;
}/*
5
1 5 4 5
2 5 10 1
3 2 10 3
6 4 9 4
7 1 8 1
*/
73【专业融合:化学】原子计数
#include <bits/stdc++.h>using namespace std;struct Atom {string name;int count;
};void resolve(string str,unordered_map<string,int>& atoms){int i=0;while(i<str.length()){string atom_e="";//检查首字母是否大写if(isupper(str[i])){atom_e+=str[i++];}//检查小写字母while(i<str.length()&&islower(str[i])){atom_e+=str[i++];}//读取元素数量int count=0;while(i<str.length()&&isdigit(str[i])){count =count*10+(str[i++]-'0');}if(!count){count=1;} atoms[atom_e]+=count; }
}int main() {string str;cin>>str;unordered_map<string ,int> atoms;resolve(str,atoms);vector<Atom> elements;for (const auto& entry : atoms) {elements.push_back({entry.first, entry.second});}sort(elements.begin(), elements.end(), [](const Atom& a, const Atom& b) {return a.name < b.name;});for (const auto& element : elements) {cout << element.name << " " << element.count << endl;}}
/*
Fe2H3OH
*/
74【专业融合:航海】水下声学定位
#include <stdio.h>
#include <math.h>#define PI 3.1415926double solve_area(double AB,double BC,double CD,double DA,double diagonal){double s_ABC = (AB + BC + diagonal)/2;double s_ADC = (CD + DA + diagonal)/2;double area_ABC = sqrt(s_ABC * (s_ABC - AB) * (s_ABC - BC) * (s_ABC - diagonal));double area_ADC = sqrt(s_ADC * (s_ADC - CD) * (s_ADC - DA) * (s_ADC - diagonal));return area_ABC +area_ADC;
}double solve_angle(double AB,double BC,double CD,double DA,double diagonal,double area){double angle=(4 *area )/(BC * BC + DA * DA - AB * AB - CD * CD);return atan(angle)*180.0/PI;}int main(){double ab,bc,cd,da,ac;scanf("%lf %lf %lf %lf %lf",&ab,&bc,&cd,&da,&ac);double area=solve_area(ab,bc,cd,da,ac);double angle=solve_angle(ab,bc,cd,da,ac,area);printf("%.6lf %.1lf",area,angle);
}/*
7 5 5 7 6
*/
75【专业融合:动能】热能计算
#include <stdio.h>int main() {double Ti, Tf;scanf("%lf %lf", &Ti, &Tf);double m_liquid, c_liquid;scanf("%lf %lf", &m_liquid, &c_liquid);double m_container, c_container;scanf("%lf %lf", &m_container, &c_container);double delta_T = Tf - Ti;double Q = (m_liquid * c_liquid + m_container * c_container) * delta_T;double percentage_container = (m_container * c_container * delta_T / Q) ;double percentage_liquid = (m_liquid * c_liquid * delta_T / Q) ;printf("%.2lfkJ,%.2lf%%,%.2lf%%\n", Q / 1000, percentage_container, percentage_liquid);return 0;
}/*
20 80
0.250 4186
0.500 900
*/
76【专业融合:热血】火箭发射模拟
#include <stdio.h>int main() {//火箭初始质量、火箭自身干质量、燃烧时间、有效排气速度cE、重力double initialMass, dryMass, burnTime, exhaustVelocity, gravity;scanf("%lf %lf %lf %lf %lf", &initialMass, &dryMass, &burnTime, &exhaustVelocity, &gravity);// 推进剂质量double propellantMass = initialMass - dryMass;double time = 0.0;double altitude = 0.0;double velocity = 0.0;// 时间步长double timestep = 0.1;double Mass_Flow=propellantMass/burnTime;while (time <= burnTime) {double thrust = Mass_Flow * exhaustVelocity;// 加速度double acceleration = (thrust / (dryMass + propellantMass)) ;/*//题目要求-g,但样例过不了double acceleration = (thrust / (dryMass + propellantMass))-gravity;*/// 速度增量double velocityIncrement = acceleration * timestep;velocity += velocityIncrement;// 海拔高度增量double altitudeIncrement = velocity * timestep;altitude += altitudeIncrement;propellantMass -= Mass_Flow* timestep;time += timestep;}printf("%.3lfkm\n", altitude/1000.0);return 0;
}
/*
100000 10000 100 4000 9.81
*/
77成绩单
#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct tagStudent{char id[11];//学号char name[31];//姓名int score;//成绩
};int compare(const void *a,const void *b){int diff=((struct tagStudent*)b)->score - ((struct tagStudent*)a)->score;if(diff==0){return strcmp(((struct tagStudent *)a)->id, ((struct tagStudent *)b)->id);}return diff;
}int main(){int n;scanf("%d",&n);struct tagStudent students[n];for(int i=0;i<n;i++){scanf("%s %s %d", students[i].id, students[i].name, &students[i].score);}qsort(students,n,sizeof(struct tagStudent),compare);for(int i=0;i<n;i++){printf("%s %s %d\n", students[i].id, students[i].name, students[i].score);}
}/*
6
2001900001 Jerry 88
2001900005 Tom 92
2001900006 Mi1la 85
2001900002 Alice 80
2001900003 Mickey 85
2001900004 Aladdin 83*/
78【专业融合:天文】日出日落时间(WA)
79【专业融合:数学】中位数
#include <stdio.h>
#include <stdlib.h>int compare(const void *a, const void *b) {return (*(int *)a - *(int *)b);
}// 计算中位数
double calculateMedian(int *array, int size) {// 将数组排序qsort(array, size, sizeof(int), compare);// 计算中位数if (size % 2 == 1) {return array[size / 2];} else {int middle1 = array[size / 2 - 1];int middle2 = array[size / 2];return (double)(middle1 + middle2) / 2;}
}int main() {int input;int *queue = NULL;int size = 0;while (1) {scanf("%d", &input);if (input > 0) {size++;queue = (int *)realloc(queue, size * sizeof(int));queue[size - 1] = input;} else if (input == 0){double median = calculateMedian(queue, size);for (int i = 0; i < size; ++i) {printf("%d ", queue[i]);}printf("%.6lf\n", median);} else if(input==-1){break;}}return 0;
}
80【专业融合:航天】卫星定位
#include <stdio.h>
#include <math.h>#define N 11
#define c 299792.458double X[N],A[N],B[N],C[N],T[N];void scanf1(double A[N],int n){for(int i=0;i<n;i++){scanf("%lf",&A[i]);}
}void print1(double A[N],int n) { //输出一个矢量int i,tmp;double a;for (i=0; i<n-1; i++){tmp=(int)(A[i]*10000);a=(double)tmp/10000.0;printf("%.4lf,",a);}tmp=(int)(A[n-1]*10000);a=(double)tmp/10000.0;printf("%.4lf",a);}void print2(double A[N][N],int n) { //输出一个矩阵int i, j;for (i=0; i<n; i++) {for (j=0; j<n; j++)printf("%.7lf ", A[i][j]);printf("\n");}
}// 计算代数余子式函数,结果=dest
int GetCoFactor(double dest[N][N], double src[N][N], int row, int col, int n)
{int i, j;int colCount=0,rowCount=0;for(i=0; i<n; i++ ) {if( i!=row ) {colCount = 0;for(j=0; j<n; j++ )if( j != col ) { //当j不是元素时dest[rowCount][colCount] = src[i][j];colCount++;}rowCount++;}}return 1;
}// 递归计算行列式,结果=返回值
double CalcDeterminant(double mat[N][N], int n)
{int i,j;double det = 0; //行列式值double minor[N][N]; // allocate 余子式矩阵// n 必须 >= 0,当矩阵是单个元素时停止递归if( n == 1 ) return mat[0][0];for(i = 0; i < n; i++ ) {GetCoFactor(minor, mat, 0, i , n);det += ( i%2==1 ? -1.0 : 1.0 ) * mat[0][i] * CalcDeterminant(minor,n-1);}return det;
}
// 伴随矩阵法矩阵求逆 , 结果存放到 inv 数组
void MatrixInversion(double J[N][N], int n)
{int i,j;double det, temp [N][N], minor[N][N];double inv[N][N];det = 1.0/CalcDeterminant(J,n); //计算行列式for(j=0; j<n; j++)for(i=0; i<n; i++) {// 得到矩阵A(j,i)的代数余子式 GetCoFactor(minor,J,j,i,n);inv[i][j] = det*CalcDeterminant(minor,n-1);if( (i+j)%2 == 1)inv[i][j] = -inv[i][j];}//结果存回J矩阵for(j=0; j<n; j++)for(i=0; i<n; i++)J[i][j] = inv[i][j];
}// 由Xn计算函数Fn,结果存放到 F
void CalcF(double F[N],double X[N],int n) {double f;int i;for (i=0; i<n; i++) {switch (i+1) {case 1: f=X[0]*X[0]+X[1]*X[1]-2*X[0]-X[2]+1; //x^2+y^2-2x-z+1break;case 2: f=X[0]*X[1]*X[1]-X[0]-3*X[1]+X[1]*X[2]+2; //xy^2-x-3y+yz+2break;case 3: f=X[0]*X[2]*X[2]-3*X[2]+X[1]*X[2]*X[2]+X[0]*X[1]; //xz^2-3z+yz^2+xybreak;}F[i]=f;}
}void CalcF_re(double F[N],double X[N],int n) {double f;int i;for (i=0; i<n; i++) {switch (i+1) {case 1: f=(X[0]-A[0])*(X[0]-A[0])+(X[1]-B[0])*(X[1]-B[0])+(X[2]-C[0])*(X[2]-C[0])-(c*(T[0]-X[3]))*(c*(T[0]-X[3])); break;case 2: f=(X[0]-A[1])*(X[0]-A[1])+(X[1]-B[1])*(X[1]-B[1])+(X[2]-C[1])*(X[2]-C[1])-(c*(T[1]-X[3]))*(c*(T[1]-X[3])); break;case 3: f=(X[0]-A[2])*(X[0]-A[2])+(X[1]-B[2])*(X[1]-B[2])+(X[2]-C[2])*(X[2]-C[2])-(c*(T[2]-X[3]))*(c*(T[2]-X[3])); break;case 4: f=(X[0]-A[3])*(X[0]-A[3])+(X[1]-B[3])*(X[1]-B[3])+(X[2]-C[3])*(X[2]-C[3])-(c*(T[3]-X[3]))*(c*(T[3]-X[3])); }F[i]=f;}
}// 由Xn计算偏导数Jacobian矩阵F'n,结果存放到 J
void CalcJ(double J[N][N],double X[N],int n) {double f;int i,j;for (i=0; i<n; i++)switch (i+1) {case 1:for (j=0; j<n; j++) {switch (j+1) {case 1: f=2*X[0]-2;break; // J1.1=2x-2case 2: f=2*X[1];break; // J1.2=2ycase 3: f=-1;break; // J1.3=-1}J[i][j]=f;}break;case 2:for (j=0; j<n; j++) {switch (j+1) {case 1: f=X[1]*X[1]-1;break; // J2.1=y^2-1case 2: f=2*X[0]*X[1]-3+X[2];break; // J2.2=2xy-3+zcase 3: f=X[1];break; // J2.3=y}J[i][j]=f;}break;case 3:for (j=0; j<n; j++) {switch (j+1) {case 1: f=X[2]*X[2]+X[1];break; // J3.1=z^2+ycase 2: f=X[2]*X[2]+X[0];break; // J3.2=z^2+xcase 3: f=2*X[0]*X[2]-3+2*X[1]*X[2];break; // J3.3=2xz-3+2yz}J[i][j]=f;}break; }
}// 由Xn计算偏导数Jacobian矩阵F'n,结果存放到 J
void CalcJ_re(double J[N][N],double X[N],int n) {double f;int i,j;for (i=0; i<n; i++)switch (i+1) {case 1:for (j=0; j<n; j++) {switch (j+1) {case 1: f=2*(X[0]-A[0]);break; // J1.1=2(x-A1)case 2: f=2*(X[1]-B[0]);break; // J1.2=2(y-B1)case 3: f=2*(X[2]-C[0]);break; // J1.3=2(z-C1)case 4: f=2*c*c*(T[0]-X[3]);break;//J1.4=2*c^2(t1-d)}J[i][j]=f;}break;case 2:for (j=0; j<n; j++) {switch (j+1) {case 1: f=2*(X[0]-A[1]);break; // J1.1=2(x-A1)case 2: f=2*(X[1]-B[1]);break; // J1.2=2(y-B1)case 3: f=2*(X[2]-C[1]);break; // J1.3=2(z-C1)case 4: f=2*c*c*(T[1]-X[3]);break;//J1.4=2*c^2(t1-d)}J[i][j]=f;}break;case 3:for (j=0; j<n; j++) {switch (j+1) {case 1: f=2*(X[0]-A[2]);break; // J1.1=2(x-A1)case 2: f=2*(X[1]-B[2]);break; // J1.2=2(y-B1)case 3: f=2*(X[2]-C[2]);break; // J1.3=2(z-C1)case 4: f=2*c*c*(T[2]-X[3]);break;//J1.4=2*c^2(t1-d)}J[i][j]=f;}break;case 4:for (j=0; j<n; j++) {switch (j+1) {case 1: f=2*(X[0]-A[3]);break; // J1.1=2(x-A1)case 2: f=2*(X[1]-B[3]);break; // J1.2=2(y-B1)case 3: f=2*(X[2]-C[3]);break; // J1.3=2(z-C1)case 4: f=2*c*c*(T[3]-X[3]);break;//J1.4=2*c^2(t1-d)}J[i][j]=f;}break; }
}// 计算 J^-1* F,结果存放到 R
void CalcJF(double R[N], double J[N][N], double F[N], int n) {int i,j,k;for (i=0; i<n; i++) {R[i]=0.0;for (j=0; j<n; j++)R[i] = R[i] + J[i][j]*F[j];}
}// 计算 X=X0-R,结果存放到 X
void CalcX(double X[N],double X0[N],double R[N],int n) {int i;for (i=0; i<n; i++)X[i]=X0[i]-R[i];
}// 计算 A=B,结果存放到 A
void AequB(double A[N],double B[N],int n) {int i;for (i=0; i<n; i++)A[i]=B[i];
}// 计算 F-
double Ferror(double F[N], int n) {double m=0;int i;for (i=0; i<n; i++) {double t=fabs(F[i]);if (m<t) m = t;}return m;
}// Newton–Raphson method 牛顿迭代法求非线性方程组的根,存放到X0
void mvNewtons(double X0[N], int n, double e) {// Guess为初始猜测值 e为迭代精度要求int k;double J[N][N],Y[N][N];double X[N],R[N],F[N];//X0一开始为初始猜测值for (k=1; k<=20; k++) { //限定20次迭代/*printf("-------------- n=%d\n",k);printf("X\n");print1(X0,n); //输出X0*/CalcF_re(F,X0,n); //计算F矩阵/*printf("F\n"); //观察 Fprint1(F,n); //输出F*/CalcJ_re(J,X0,n); //计算Jacobian矩阵F'n(x0)/*printf("J\n");print2(J,n); //观察 J*/MatrixInversion(J, n); // 求J的逆矩阵 J^-1CalcJF(R,J,F,n); // R=J^-1 * FCalcX(X,X0,R,n); // X=X0-RAequB(X0,X,n); // X0=X 下次迭代if (Ferror(F,n)<e) break; //达到精度要求,终止迭代}
}int main() {int n=4;scanf("%lf %lf %lf",&A[0],&B[0],&C[0]);scanf("%lf %lf %lf",&A[1],&B[1],&C[1]);scanf("%lf %lf %lf",&A[2],&B[2],&C[2]);scanf("%lf %lf %lf",&A[3],&B[3],&C[3]);scanf1(T,n);scanf1(X,n);mvNewtons(X,n,1e-6); //根存放在Xprint1(X,3);return 0;
}/*
15600 7540 20140
18760 2750 18610
17610 14630 13480
19170 610 18390
0.07074 0.07220 0.07690 0.07242
0 0 6370 0
*/
80-90
81【算法策略:动态规划】上楼梯
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int solve(int N,int M,int *bad){int dp[N+1];memset(dp,0,sizeof(dp));for(int i=0;i<M;i++){dp[bad[i]]=-1;}dp[0]=1;if(dp[1]!=-1)dp[1]=1;else dp[1]=0;for(int i=2;i<=N;i++){if(dp[i]==-1){dp[i]=0;}else{dp[i]=(dp[i-1]+dp[i-2])%1000000007;}}return dp[N];}int main(){int N,M;scanf("%d %d",&N,&M);int *bad=(int *)malloc(M*sizeof(int));for(int i=0;i<M;i++){scanf("%d",&bad[i]);}printf("%d\n",solve(N,M,bad));
}
82【算法策略:动态规划】挑选
#include <stdio.h>
#include <time.h>#include <limits.h>#include <stdlib.h>int max(int a, int b) {return (a > b) ? a : b;
}void quickSort(long long arr[], long long left, long long right) {if (left >= right) return;srand(time(NULL));long long idx = rand() % (left - right) + left;long long flag = arr[idx], head = left - 1, tail = right + 1;while (head < tail) {do head++; while(arr[head] < flag);do tail--; while(arr[tail] > flag);if (head < tail) {long long tmp = arr[head];arr[head] = arr[tail];arr[tail] = tmp;}}quickSort(arr, left, tail);quickSort(arr, tail + 1, right);
}int main() {long long n;scanf("%lld", &n);long long arr[n];// 输入序列for (int i = 0; i < n; i++) {scanf("%lld", &arr[i]);}quickSort(arr,0,n-1);// 初始化动态规划数组int dp[n];dp[0] = arr[0];int MAX=0;// 动态规划递推for (int i = 1; i < n; i++) {/*当arr[i]和前一个数字相等时,判断正负数,即相加后是否变小即可,选大的填入*/if(arr[i]==arr[i-1]){dp[i]=max(dp[i-1],dp[i-1]+arr[i]);}/*存在111112333334555555这种情况*/else{int j=i-1;while(j>=0&&arr[j]==arr[i]-1) j--;if(j>=0){dp[i]=arr[i]+dp[j];}else{dp[i]=arr[i];}}MAX=max(MAX,dp[i]);}printf("%d\n", MAX);return 0;
}
83【算法策略:回溯】和字符串
#include <stdio.h>
#include <string.h>
#include <stdbool.h>long long substrToNum(char str[], int pos, int len) {long long num = 0;for (int i = 0; i < len; ++i)num = num * 10 + str[pos + i] - '0';return num;
}long long getLen(long long n) {int cnt = 0;do ++cnt, n /= 10; while(n);return cnt;
}bool backTracking(char str[], int strLen, int begin, int len1, int len2) {if (begin + len1 + len2 >= strLen) return true;long long num1 = substrToNum(str, begin, len1);long long num2 = substrToNum(str, begin + len1, len2);long long num3 = substrToNum(str, begin + len1 + len2, getLen(num1 + num2));printf("%lld,%lld,%lld\n", num1, num2, num3);if (num1 + num2 == num3) return backTracking(str, strLen, begin + getLen(num1), getLen(num2), getLen(num3));return false;
}void partition(char str[]) {int strLen = strlen(str);for (int i = 1; i <= strLen / 2; ++i) {if (backTracking (str, strLen, 0, i, i)) {printf("true\n");return;}}printf("false\n");
}int main() {char str[1000] = "";scanf("%s", str);partition(str);return 0;
}
84【算法策略:贪心】汤包
#include <stdio.h>
#include <stdlib.h>
#include <string.h>struct customer{int t;int d;int etime;int index;
};int main(){int n;scanf("%d",&n);struct customer a[n];for(int i=0; i<n; i++){a[i].index=i+1;scanf("%d %d",&a[i].t,&a[i].d);a[i].etime=a[i].t+a[i].d;}for(int i=0;i<n-1;i++){for(int j=0;j<n-1-i;j++){if((a[j].etime>a[j+1].etime)||((a[j].etime==a[j+1].etime)&&a[j].index>a[j+1].index)){int tmp=a[j].etime;a[j].etime=a[j+1].etime;a[j+1].etime=tmp;tmp=a[j].index;a[j].index=a[j+1].index;a[j+1].index=tmp;}}}for(int i=0;i<n;i++){printf("%d ",a[i].index);}}/*
3
3 3
1 3
2 3
*/
85【算法策略:优雅的策略】危险的组合
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int n;
int num;
int a[30];void dfs(int i,int flag){if(i==n) {if(flag>=1) num++;return;}for(int j=0;j<2;j++){a[i]=j;if(i>=2&&a[i-1]==a[i]&&a[i-1]==a[i-2]&&a[i]==1){dfs(i+1,flag+1);}else{dfs(i+1,flag);}}}int main(){while(1){scanf("%d",&n);num=0;if(n<=0) break;dfs(0,0);printf("%d\n",num);}
}
86【算法策略:动态规划】打字机
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define MAX_LEN 1000int main() {char str[MAX_LEN];scanf("%s", str);int len = strlen(str);for (int i = 0; i < len; ++i) {if (str[i] == 'm' || str[i] == 'w') {printf("0\n");return 0;}}long long dp[MAX_LEN];dp[0] = 1;dp[1] = 1;/*动态规划过程:以nn为例子,dp[i]代表前i个字符串的总可能性,注意空字符串也算一种可能性识别到"nn"时有两种情况选择,一种是保持nn,此时要单独看待input[1],也就是第2个n,此时的字符串可能性即为dp[1];另一种是转换为w,此时字符串的可能性为dp[0],也就是空字符串的可能性。*/for (int i = 1; i < len; ++i) {dp[i + 1] = dp[i];if ((str[i] == 'n' && str[i - 1] == 'n') || (str[i] == 'u' && str[i - 1] == 'u')) {dp[i + 1] += dp[i - 1];}}printf("%lld\n", dp[len]);return 0;
}
87【算法策略:分治】子数组最大和
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main(){int n;scanf("%d",&n);int a[n];int dp[n];for(int i=0; i<n; i++){scanf("%d",&a[i]);dp[i]=a[i];}for(int i=1;i<n;i++){if(dp[i]+dp[i-1]>dp[i]){dp[i]=dp[i]+dp[i-1];}}int max=0;for(int i=0;i<n;i++){if(dp[i]>max) max=dp[i];}printf("%d\n",max);}/*
7
2 -4 1 9 -6 7 -3
*/
88【算法策略:贪心】绝对差
#include <stdio.h>
#include <stdlib.h>
#include <string.h>void swap(int *a, int *b) {int temp = *a;*a = *b;*b = temp;
}int partition(int arr[], int low, int high) {int pivot = arr[high];int i = low - 1;for (int j = low; j <= high - 1; j++) {if (arr[j] < pivot) {i++;swap(&arr[i], &arr[j]);}}swap(&arr[i + 1], &arr[high]);return i + 1;
}void quickSort(int arr[], int low, int high) {if (low < high) {int pivotIndex = partition(arr, low, high);quickSort(arr, low, pivotIndex - 1);quickSort(arr, pivotIndex + 1, high);}
}int main(){int n;scanf("%d",&n);int arr[n];for(int i=0; i<n; i++){scanf("%d",&arr[i]);}quickSort(arr,0,n-1);int min=9999999;for(int i=0;i<n-1;i++){if(abs(arr[i+1]-arr[i])<min){min=abs(arr[i+1]-arr[i]);}}printf("%d\n",min);}
89【算法策略:回溯】游乐园
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int n,m;
int map[1000][1000]={0};
int used[1000]={0};
int MAX=0;;int max(int x, int y){if(x>y) return x;return y;
}void dfs(int s,int sum){MAX=max(MAX,sum);for(int i=1;i<=n;i++){if(map[s][i]&&!used[i]){used[i]=1;dfs(i,sum+map[s][i]);used[i]=0;}}
}int main(){scanf("%d %d",&n,&m);int a,b,c;memset(map,0,sizeof(map));for(int i=0;i<m;i++){scanf("%d %d %d",&a,&b,&c);map[a][b]=c;map[b][a]=c;}for(int i=1;i<=n;i++){memset(used,0,sizeof(used));used[i]=1;dfs(i,0);}printf("%d\n",MAX);}/*
4 4
1 2 1
2 3 10
1 3 100
1 4 1000
*/
90【算法策略:贪心】三角形
#include <stdio.h>
#include <stdlib.h>
#include <string.h>//快排模板
void quicksort(int arr[], int l, int r)
{if (l >= r){return;}else{int i = l - 1;int j = r + 1;int x = arr[(l + r) / 2];while (i < j){do{i++;} while (arr[i] > x);do{j--;} while (arr[j] < x);if (i < j){int tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}}quicksort(arr, l, j);quicksort(arr, j + 1, r);}
}//----------------------------------------------------------------void is_Triangle(int arr[], int n){for(int i=0;i<n-2;i++){if(arr[i]<arr[i+1]+arr[i+2]){printf("%d %d %d\n",arr[i+2],arr[i+1],arr[i]);return ;}}printf("-1");}int main(){int n;scanf("%d", &n);int a[n];for(int i=0;i<n;i++){scanf("%d", &a[i]);}quicksort(a,0,n-1);is_Triangle(a,n);
}/*
6
1 8 5 2 4 3
*/
91-100
91【考试题型:字符串】左右操作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>//快排模板
void quicksort(char arr[], int l, int r)
{if (l >= r){return;}else{int i = l - 1;int j = r + 1;char x = arr[(l + r) / 2];while (i < j){do{i++;} while (arr[i] > x);do{j--;} while (arr[j] < x);if (i < j){char tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}}quicksort(arr, l, j);quicksort(arr, j + 1, r);}
}//----------------------------------------------------------------void swap(char arr[],int l,int r){for(int i=l;i<(r-l+1)/2+l;i++){char tmp=arr[i];int x=r-1-(i-l);arr[i]=arr[x];arr[x]=tmp;}
}int main(){char str[1000];scanf("%s", str);int n=strlen(str);quicksort(str, 0,n/2-1);int right_s=n%2?n/2+1:n/2;swap(str,right_s,n);printf("%s\n",str);
}/*
abcdefX181292
*/
92【考试题型:输入输出】气体扩散
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int main(){double a,b;scanf("%lf %lf",&a,&b);double result=sqrt(b/a);printf("%.4lf\n",result);
}/*
349.03 352.04
*/
93【考试题型:文件】平方根
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int main(){int n;double result;scanf("%d",&n);FILE *file = fopen("rr.dat","w");for(int i=1;i<=n;i++){result=sqrt(i);fprintf(file, "%.6f\n", result);}fclose(file);file = fopen("rr.dat", "r");while (fscanf(file, "%lf", &result) == 1) {printf("%.6f ", result);}fclose(file);
}
94【考试题型:结构体】空中交通管制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>struct flight{char name[100];int x,y;
};double solve(int x1,int y1,int x2,int y2){return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}int main(){int n;scanf("%d", &n);struct flight a[n];for(int i=0;i<n;i++){scanf("%s %d %d",a[i].name,&a[i].x,&a[i].y);}double min=9999999;char flight1[100];char flight2[100];for (int i = 0; i < n - 1; i++) {for (int j = i + 1; j < n; j++) {double distance = solve(a[i].x, a[i].y, a[j].x, a[j].y);if (distance < min) {min = distance;snprintf(flight1, sizeof(flight1), "%s", a[i].name);snprintf(flight2, sizeof(flight2), "%s", a[j].name);}}}// 输出结果printf("%s-%s %.4lf\n", flight1, flight2, min);return 0;}/*
6
UAO57 2 3
AA044 12 30
BA1534 40 50
DL262 5 1
AF001 12 10
SK837 3 4
*/
95【考试题型:循环(迭代)】圆周率Π
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int main(){int n;scanf("%d",&n);double result=3.0;double flag=1.0;for(int i=2;i<=(n-1)*2;i+=2){result+=4.0/(double)(i*(i+1)*(i+2))*flag;flag=-flag;}printf("%.7lf", result);
}
96【考试题型:分支选择】马赫数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int main(){double V,T;scanf("%lf %lf",&V,&T);double c=331.3*sqrt(1.0+T/273.15);double M=(V*1000.0)/3600.0/c;printf("%.3lf ",M);if(M<=0.8){printf("subsonic");}else if(M>0.8&&M<=1.2){printf("transonic");}else if(M>1.2&&M<=5.0){printf("supersonic");}else if(M>5.0&&M<=10.0){printf("hypersonic");}}/*
920 -49.90
*/
97【考试题型:数组】重复元素
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int main(){int n;scanf("%d",&n);long long a[1001];for(int i=0;i<n;i++){scanf("%lld",&a[i]);}int sum=0;for(int i=1;i<n;i++){for(int j=0;j<i;j++){if(a[i]==a[j]) {sum++;break;}}}printf("%d\n",sum);
}/*
10
1 10 20 1 25 1 10 30 25 1
*/
98【考试题型:枚举】机场翻牌显示
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int main(){char str1[10];char str2[10];int sum=0;scanf("%s",str1);scanf("%s",str2);for(int i=0;i<6;i++){if(i<=1){sum+=(str2[i]-str1[i]+26)%26;}else {sum+=(str2[i]-str1[i]+10)%10;}}printf("%d\n",sum);}/*
MU2103
CA8326
*/
99【考试题型:算法策略(递归分治贪心动态规划)】零钞
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int main(){int n;scanf("%d",&n);int a[4]={10,5,2,1};int b[4];for(int i=0;i<4;i++){b[i]=n/a[i];if(b[i]!=0)n=n%a[i];}for(int i=3;i>=0;i--){if(b[i]!=0)printf("%d=%d\n",a[i],b[i]);}}
100【考试题型:函数(递归)】阿克曼函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int A(int x,int y){if(x==0) return y+1;if(y==0) return A(x-1,1);return A(x-1,A(x,y-1));
}int main(){int m,n;scanf("%d %d",&m,&n);int result=A(m,n);printf("%d\n",result);
}
这篇关于2023年西工大noj(C程序设计)100题代码参考的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!