作业02
A
#include<stdio.h>
#include<math.h>
int main() {
int t;
scanf("%d",&t);
while(t--){// 记得思考一下t--为啥是对的
int a,b,c;
// 输入格式都是a+b=c,所以可以在scanf中手动填上+号和=号来进行占位
// 这样就能直接读到对应的a,b,c
scanf("%d+%d=%d",&a,&b,&c);
if (a + b == c) {
printf("Yes\n");
} else {
printf("No\n");
}
}
return 0;
}
B
解法一
#include<stdio.h>
#include<math.h>
#include<string.h>
char s[200];// 定义一个长度为200的字符串数组s,范围在0~199
int main() {
int t;
scanf("%d",&t);
while(t--){// 记得思考一下t--为啥是对的
// 输入一个手机号码,将这个号码中间四位数用*代替
// 这道题不适合用int输入,主要还是考察字符串的读入
scanf("%s",s);// 输入字符串
int len = strlen(s);
// 因为下标是从0开始的,所以应该是要把[3,6]这个区间的字符变成*
for (int i=3;i<=6;++i) {
s[i] = '*';
}
printf("%s\n",s);// 输出字符串
}
return 0;
}
解法二
#include <stdio.h>
int a[12];// 定义一个长度为12的数组
int main() {
int t;
scanf("%d",&t);
while(t--){
long long x; //
scanf("%lld",&x);// 1e4 1e5 2e6
int cnt = 0;
while(x){
int y = x % 10;// 取余数
a[cnt++] = y;
x /= 10;// 累除
}
// 这个时候的a数组就记录上了每一个数位的值了,但是是倒序的
// 比如 12345,被处理在a数组中就变成了a[0]=5,a[1]=4,a[2]=3,...
// [0,cnt-1]数组的有效范围是,因为我们是从0开始计数的
for(int i=cnt-1;i>=0;--i){
if (i<=7&&i>=4) {
printf("*");
} else {
printf("%d",a[i]);
}
}
printf("\n");
}
}
C
#include<stdio.h>
#include<math.h>
int main() {
int t;
scanf("%d",&t);
while(t--){// 记得思考一下t--为啥是对的
double n,m;
scanf("%lf %lf",&n,&m);
double c = (n + m) / 2.0; // 算一下aa的费用
if (c > n) { // 需要Alice给Bob钱
// 这题比较坑,需要如果答案没有小数的话,就必须要输出整数;
// 比如答案是1的时候,不能输出1.0,否则会有错
// 做一下判断 如果向下取整和向上取整不相同,就说明他是一个带小数的浮点数
// 否则就直接输出%d的整数
// ceil(1.5) = 2,floor(1.5) = 1;
if (ceil(c - n) != floor(c - n) ){
printf("Alice %.1lf\n",c - n);
} else {
// 输出之前把(c-n)强制转换成int,因为c-n的结果是double类型的,不转换输出会错
printf("Alice %d\n",(int)(c - n));
}
} else if (c > m) { // 需要Bob给Alice钱
// 处理同上
if (ceil(c - m) != floor(c - m) ){
printf("Bob %.1lf\n",c - m);
} else {
printf("Bob %d\n",(int)(c - m));
}
} else { // 不需要给钱
printf("None\n");
}
}
return 0;
}
D
#include<stdio.h>
#include<math.h>
int main() {
int t;
scanf("%d",&t);
while(t--){// 记得思考一下t--为啥是对的
int n;
scanf("%d",&n);
double k = sqrt(n);
// 把开根后的值k进行 向上取整和向下取整的比较,如果相同,则说明k是一个整数
// 例如如果k是1.5 ceil(k) = 2, floor(k) = 1
// 如果k是2 ceil(k) = floor(k) = 2
if (ceil(k) == floor(k)) {
puts("Yes");
} else {
puts("No");
}
}
return 0;
}
E
#include<stdio.h>
#include<math.h>
int main() {
int t;
scanf("%d",&t);
while(t--){// 记得思考一下t--为啥是对的
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if (a >= c) {
// 如果手上的数量已经大于等于c个,那么就只需要0天
// 特判一下,continue,进入while的下一次循环
printf("0\n");
continue;
}
// 直接算一下至少还要多少天才能到达(c-a)个硬币,向上取整一下,
// b要*1.0,把int转成double浮点
double ans = ceil((c - a) / (b * 1.0));
// 输出前记得把ans也转成int,因为答案是整数天
printf("%d\n",(int)ans);
}
return 0;
}
F
#include<stdio.h>
#include<math.h>
int main() {
int t;
scanf("%d",&t);
while(t--){// 记得思考一下t--为啥是对的
int n;
scanf("%d",&n);
int hh = n/3600;
int mm = n%3600/60;
int ss = n%3600%60;
// 这题主要考察怎么输出这种 自动补0的时钟格式
// 使用%02d就可以了,2表示把这个整数输出成2位数,0表示当不够2位数时,前面要补0
// 具体的%02d的格式建议去百度或者gpt查询
printf("%02d:%02d:%02d\n",hh,mm,ss);
}
return 0;
}
G
#include<stdio.h>
#include<math.h>
int main() {
int t;
scanf("%d",&t);
while(t--){// 记得思考一下t--为啥是对的
// 很简单的一道题,题目限定了x 和 y不会等于0
// 直接用if else if else 直接做就好
int x,y;
int ans = 0;
scanf("%d%d",&x,&y);
if (x > 0 && y > 0) {
ans = 1;
} else if (x < 0 && y > 0) {
ans = 2;
} else if (x < 0 && y < 0) {
ans = 3;
} else {
ans = 4;
}
printf("%d\n",ans);
}
return 0;
}
H
#include<stdio.h>
#include<math.h>
int main() {
long long a1,b1,c1; //这道题要用long long,否则两个1e9的int 相乘,就会炸精度
long long a2,b2,c2;
scanf("%lld %lld %lld",&a1,&b1,&c1);
scanf("%lld %lld %lld",&a2,&b2,&c2);
// 这题就是考你的数学理论。不会的话复习一下吧
// 下边的代码是gpt帮我写的 建议自己理解下噢 自己写一下吧
// 判断两条直线是否平行或共线
long long parallelCondition1 = a1 * b2 - a2 * b1;
long long parallelCondition2 = a1 * c2 - a2 * c1;
long long parallelCondition3 = b1 * c2 - b2 * c1;
if (parallelCondition1 == 0) {
if (parallelCondition2 == 0 && parallelCondition3 == 0) {
puts("collinear");
} else {
puts("parallel");
}
} else {
// 判断两条直线是否垂直
long long verticalCondition1 = a1 * a2;
long long verticalCondition2 = b1 * b2;
long long verticalCondition3 = verticalCondition1 + verticalCondition2;
if (verticalCondition3 == 0) {
puts("vertical");
} else {
puts("intersect");
}
}
return 0;
}