高级语言程序设计课程第六次个人作业
这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2024C/
这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/2024C/homework/13303
学号:102300108
姓名:陈茜蕾
1
问题:k++导致出错
解决:改成k+=1
2
问题:忘记在字符串末尾加'\0'
解决:后面补上了
3
问题:一开始直接拿sizeof(arr)作为长度
解决:后面改成sizeof(arr) / sizeof(arr[0])
4
5
6
7
8
9
#include <stdio.h>
#include<string.h>
#pragma warning (disable:4996)
int prime[1000] = { 0 };
bool isPrime(int n){
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
int split(int n, int* a, int* b) {
for (int i = 0; prime[i] < n; i++) {
for (int j = 0; prime[j] < n; j++) {
if (prime[i] + prime[j] == n) {
*a = prime[i];
*b = prime[j];
return 1;
}
}
}
return 0;
}
int main() {
int n = 0;
scanf("%d", &n);
for (int i = 2,cnt=0; i <= n; i++) {
if (isPrime(i)) {
prime[cnt] = i;
cnt++;
}
}
int a = 0;
int b = 0;
if (split(n, &a, &b)) {
printf("%d=%d+%d", n, a, b);
}
else {
printf("不能被分解");
}
}
10
11
#include <stdio.h>
#include<string.h>
#pragma warning (disable:4996)
bool judgeCube(int arr[][100],int n) {
int temp = 0;
//temp初始值设定为第一行的和
for (int i = 0; i < n; i++) {
temp += arr[0][i];
}
int sum = 0;
//判断行的和是否相等
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
sum += arr[i][j];
}
if (sum != temp) {
return false;
}
sum = 0;//置0
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) {
sum += arr[i][j];
}
if (sum != temp) {
return false;
}
sum = 0;
}
//对角线
for (int i = 0; i < n; i++) {
sum += arr[i][i];
}
if (sum != temp) {
return false;
}
sum = 0;
//另一条对角线
for (int i = 0; i < n; i++) {
sum += arr[i][n - i - 1];
}
if (sum != temp) {
return false;
}
sum = 0;
return true;
}
int main() {
int n = 0;
scanf("%d", &n);
int arr[100][100];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
}
}
if (judgeCube(arr, n)) {
printf("YES");
}
else {
printf("NO");
}
}
12
总结
对指针还有运用不熟练的地方,通过这次练习让我更加熟练地运用指针来进行编程。