# 310
```
310
請撰寫一程式,包含名為compute()的函式,接收主程式傳遞的一個整數n(0 < n < 1000),compute()輸出所有小於n的阿姆斯壯數並回傳總和至主程式輸出。
阿姆斯壯數的定義:若為k位數的正整數,則其所有位數數字的k次方與該數相等。
輸入說明 一個整數n(0 < n < 1000)
輸出說明 所有小於n的阿姆斯壯數與其總和
範例輸入 999
範例輸出
1
2
3
4
5
6
7
8
9
153
370
371
407
1346
#include <stdio.h>
#include <math.h>
int compute(int);
int digits(int);
int main(){
int n;
scanf("%d", &n);
printf("%d", compute(n) );
}
//Calculate the Armstrong number
int compute(int a) {
int total = 0;
for (int i=1; i<a; ++i){
if (a<=10){
printf("%d\n", i);
total += i;
} else {
int count = 0;
int j = i;
int k = digits(j);
while(j != 0) {
count += pow(j % 10, k);
j /= 10;
}
if (i == count){
printf("%d\n", count);
total += i;
}
}
}
return total;
}
//Calculate how many digits the number n has
int digits(int n) {
int count = 0;
while(n != 0)
{
n /= 10;
++count;
}
return count;
}
402
請撰寫一程式,讓使用者輸入兩個相同長度的字串與一個正整數n,字串長度皆不超過128個字元,依ASCII碼表上的順序比對兩字串前n個字元,最後輸出兩字串前n個字元的比較結果。若使用者輸入正整數n超過字串長度,則輸出「error」。
輸入說明 兩個相同長度的字串及一個正整數
輸出說明 兩字串前n個字元的比較結果(大於、等於、小於)
範例輸入1
Apple ipad
Apple ipod
5
範例輸出1 Apple ipad = Apple ipod
範例輸入2
Apple ipad
Apple ipod
9
範例輸出2 Apple ipad < Apple ipod
範例輸入3
Apple ipad
Apple ipod
15
範例輸出3 error
/*
題庫答案沒有檢查
1.輸入的兩字串長度是否一樣
2.用以比較前n個字元的n 是否是正整數
*/
#include <stdio.h>
#include <string.h>
#define LEN 128
int main(){
char str1[LEN];
char str2[LEN];
fgets(str1, LEN, stdin); //p8-13
fgets(str2, LEN, stdin);
int n;
scanf("%d", &n);
// 去除最後的換行字元
str1[strlen(str1) - 1] = '\0';
str2[strlen(str2) - 1] = '\0';
if (n > strlen(str1) ){
printf("%s", "error");
} else {
int k = strncmp(str1, str2, n); //p8-19
if (k < 0) {
printf("%s < %s", str1, str2);
} else if (k == 0) {
printf("%s = %s", str1, str2);
} else {
printf("%s > %s", str1, str2);
}
}
//p8-30
//strlen(str1)的值型別為 size_t,其值不包括結束空字元
//printf("%lu", strlen(str1));
//printf("%d", n);
}
404
請撰寫一程式,讓使用者輸入一個長度不超過50字元的字串,此字串均為小寫字母,輸出該字串出現最多次的英文字母以及出現的次數。
提示:假設出現過最多次英文字母的次數唯一。
輸入說明 一個長度不超過50字元的字串,此字串均為小寫字母
輸出說明 該字串出現最多次的英文字母以及出現的次數
範例輸入 refrigerator
範例輸出
r
4
// 題庫答案沒有檢查 輸入的字串是否均為小寫字母
#include <stdio.h>
#include <string.h>
#define LEN 50
int maximun(int a[], int n, int *max);
int main(){
char str1[LEN];
fgets(str1, LEN, stdin);
//scanf("%LENs", str1);
// 去除最後的換行字元
str1[strlen(str1) - 1] = '\0';
size_t n = strlen(str1);
int frequency[n];
for (int i=0; i<n; ++i){
frequency[i]=1;
for (int j=i+1; j<n; ++j)
{
if (str1[j]==str1[i]){
++frequency[i];
}
}
}
int max=0;
//printf("%d", max);
int k = maximun(frequency, n, &max);
printf("%c\n%d", str1[k], max);
}
//n為array a[]的長度 = sizeof(a[]) / sizeof(int)
int maximun(int *a, int n, int *max){
int j=0;
for (int i=0; i<n; ++i){
if (*(a+i) > *max){
*max = a[i];
j=i;
}
}
return j;
}
406
請撰寫一程式,讓使用者輸入一個長度不超過50字元的字串,該字串包含英文大小寫,將每個字元依照鍵盤的位置,輸出它們右邊的大寫或小寫英文字母。若輸入字母的右邊並非英文字母,如「P」、「L」、「M」,則不做更動,原樣輸出。
鍵盤上的英文字母位置圖
輸入說明 一個長度不超過50字元的字串,字串包含英文大小寫
輸出說明 依照鍵盤位置,輸出每個字元右邊的大寫或小寫英文字母
範例輸入 NovemBer
範例輸出 MpbrmNrt
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LEN 50
int main(){
//abcde fghij klmno pqrst uvwxy z
//snvfr ghjok llmmp pwtdy ibecu x
char str2[] = "snvfrghjokllmmppwtdyibecux";
char str1[LEN];
fgets(str1, LEN, stdin);
// strip the last newline character
size_t n = strlen(str1);
str1[n-1] = '\0';
for (int i=0; i<n-1; ++i)
{
if ( islower(str1[i]) ){
printf("%c", str2[ str1[i] - 97 ]);
} else {
//printf("%c", toupper(str2[ str1[i] - 65 ]) );
printf("%c", str2[ str1[i] - 65 ]-32 );
}
}
}
408
請撰寫一程式,讓使用者輸入兩個長度大於3且不超過20的字串,輸出兩字串的長度以及兩字串連結後反轉的結果,若字串長度有誤,請輸出「error」。
輸入說明 兩個字串
輸出說明 字串長度與字串連結後反轉的結果
範例輸入1
abcd
efghijk
範例輸出1
4
7
kjihgfedcba
範例輸入2
ab
cdefgh
範例輸出2 error
#include <stdio.h>
#include <string.h>
#define LEN 20
int main(){
char str1[LEN];
char str2[LEN];
fgets(str1, LEN, stdin);
fgets(str2, LEN, stdin);
// strip the last newline character
str1[strlen(str1)-1] = '\0';
str2[strlen(str2)-1] = '\0';
while (strlen(str1) < 3 || strlen(str2) < 3) {
printf("%s", "error");
return 0;
}
printf("%lu\n%lu\n", strlen(str1), strlen(str2));
strcat(str1,str2);
for (int i = strlen(str1)-1; i>=0; --i){
printf("%c", str1[i] );
}
}
```