Day03
试题A 门牌制作

#include<stdio.h>
int main() {
int count = 0;
int a, b, c, d;
for (int i = 0; i <= 2020; i++) {
a = i / 1000;
b = (i / 100) % 10;
c = (i / 10) % 10;
d = i % 10;
if (a == 2) {
count++;
}
if (b == 2) {
count++;
}
if (c == 2) {
count++;
}
if (d == 2) {
count++;
}
}
printf("%d", count);
return 0;
}
答案:624
试题B 既约分数

/*
辗转相除法
*/
#include<stdio.h>
int main() {
int quotient = 0; //商
int remainder = 1; //余数
int count = 0; //计数
int tempA = 0, tempB = 0; //临时变量
for (int numerator = 1; numerator <= 2020; numerator++) { //分子
for (int denominator = 1; denominator <= 2020; denominator++) { //分母
tempA = numerator;
tempB = denominator;
while (remainder != 0) {
quotient = tempA / tempB;
remainder = tempA % tempB;
tempA = tempB;
tempB = remainder;
}
if (tempA == 1) {
count++;
}
remainder = 1;
}
}
printf("%d", count);
return 0;
}
答案:2481215
/*
更相损减术
*/
#include<stdio.h>
#include<math.h>
int main() {
int count1 = 0; //计数:有几个既约分数
int count2 = 0; //计数:共约去几个2
int tempA = 0, tempB = 0; //临时变量
int difference = 1; //差
int temp;
for (int numerator = 1; numerator <= 2020; numerator++) {
for (int denominator = 1; denominator <= 2020; denominator++) {
tempA = numerator;
tempB = denominator;
while ((tempA % 2 == 0) && (tempB % 2 == 0)) {
tempA = tempA / 2;
tempB = tempB / 2;
count2++;
}
while (difference != 0) {
if (tempA < tempB) {
temp = tempA;
tempA = tempB;
tempB = temp;
}
difference = tempA - tempB;
tempA = difference;
}
if ((tempB * int(pow(2, count2))) == 1) {
count1++;
}
difference = 1;
count2 = 0;
}
}
printf("%d", count1);
return 0;
}
答案:2481215
试题C 蛇形填数

#include<stdio.h>
#define M 40 //填完有a[19][19]斜行最多有第40行第40列的元素
int main() {
int a[M][M] = { 0 };
int count = 1; //给数组赋值,依次加一
int i, j;
for (int k = 0;k < 780; k++) { //下角标的和,填完有a[19][19]斜行最多到780
for (i = 0; i < M; i++) {
for (j = 0; j < M; j++) {
//反对角线方向填数字
//偶数行反对角线,下角标加起来和为奇数
if (i + j == k && k % 2 == 1) {
a[i][j] = count;
count++;
}
//奇数行反对角线,下角标加起来和为偶数
if (i + j == k && k % 2 == 0) {
a[j][i] = count;
count++;
}
}
}
}
printf("%d\n", a[19][19]); //容易出错,第20行第20列
return 0;
}
找规律

试题D 跑步锻炼

#include<stdio.h>
int main() {
int year = 2000, month = 1, day = 1, week = 5;
int d[13] = { 0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int answer = 0;
for (; year <= 2020; year++) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
d[2] = 29;
}
for (; month <= 12; month++) {
if (year == 2020 && month == 10) {
goto outloop;
}
for (; day <= d[month]; day++) {
week = (week + 1) % 7;
if (week == 1 || day == 1) {
answer = answer + 2;
}
else {
answer++;
}
}
day = 1;
}
month = 1;
d[2] = 28;
}
outloop:
printf("%d", answer + 2);
return 0;
}
答案:8879
https://blog.csdn.net/qq_43738331/article/details/110729821这个博客代码写的更清晰

浙公网安备 33010602011771号