add 1 - 2

This commit is contained in:
juan 2021-10-25 20:25:25 +08:00
parent 0bdac22c20
commit c4981436af
No known key found for this signature in database
GPG key ID: 5C1E5093C74F1DC7
2 changed files with 57 additions and 0 deletions

25
pset5/1-numbers.c Normal file
View file

@ -0,0 +1,25 @@
#include <stdio.h>
int main(void) {
// To store answer
int nums[10];
long long n;
int i = 0;
scanf("%lld", &n);
while (n > 0) {
nums[i] = n % 10;
n /= 10;
i++;
}
for (int j = i - 1; j >= 0; j--) {
if (j == 0) {
printf("%i\n", nums[j]);
} else {
printf("%i ", nums[j]);
}
}
return 0;
}

32
pset5/2-swap.c Normal file
View file

@ -0,0 +1,32 @@
#include <stdio.h>
// Function prototypes
void swap(int *a, int *b);
int main(void) {
// Initialize variables
int n;
int length = 0;
int nums[10];
// Get input
scanf("%d", &n);
// Tabulate nums[]
while (n > 0) {
nums[length] = n % 10;
n /= 10;
length++;
}
// See if anything is wrong
if (nums[0] == 0) {
printf("The number cannot be changed.\n");
} else { // Print answer
for (int i = 0; i < length; i++) {
printf("%i", nums[i]);
}
printf("\n");
}
return 0;
}