【題解】TQC+ 程式語言C
===
[Code Judger](https://www.codejudger.com/)
102:蘋果汁23.34元
--
```c=
#include <stdio.h>
int main(){
int n;
scanf("%d", &n);
printf("%.2f", n*23.34);
}
```
104:兩浮點數和
--
```c=
#include <stdio.h>
int main(){
float a, b;
scanf("%f %f", &a, &b);
printf("total=%.2f", a+b);
}
```
106:兩點距離
--
```c=
#include <stdio.h>
#include <math.h>
int main(){
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
printf("%.2f", sqrt(pow(a-c,2)+pow(b-d,2)));;
}
```
108:圓的直徑
--
```c=
#include <stdio.h>
int main(){
int n;
scanf("%d", &n);
printf("%-10d\n%-10.2f\n%-10.4f", n, n/2.0, (n/2.0)*(n/2.0)*3.1415);
}
```
110:輸入a,b,c
--
```c=
#include <stdio.h>
int main(){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a>=60 && a<100) printf("1\n");
else printf("0\n");
printf("%.2f\n", (b+1)/10.0);
if(a>=c) printf("%d", a);
else printf("%d", c);
}
```
202:輸入分數
--
```c=
#include <stdio.h>
int main(){
int n;
scanf("%d", &n);
if(n<0 || n>100) printf("error");
else if(n>60) printf("%d", n+10);
else printf("%d", n+5);
}
```
204:簡易計算機
--
```c=
#include <stdio.h>
int main(){
int a, b, ans;
char c;
scanf("%d %d %c", &a, &b, &c);
if(c=='+') ans=a+b;
else if(c=='-') ans=a-b;
else if(c=='*') ans=a*b;
else{
printf("error");
return 0;
}
printf("%d%c%d=%d",a,c,b,ans);
}
```
206:奇數和
--
```c=
#include <stdio.h>
int main(){
int a, b, ans=0;
scanf("%d %d", &a, &b);
while(a<=b) ans+=a, a+=2;
printf("%d", ans);
}
```
208:質數 (👍好題)
--
```c=
#include <stdio.h>
#include <string.h>
int main(){
int n, i, j;
scanf("%d", &n);
int prime[n];
memset(prime, 1, sizeof(prime));
prime[0]=0, prime[1]=0;
for(i=2; i<n; i++)
if(prime[i])
for(j=i*2; j<n; j+=i)
prime[j]=0;
for(i=2; i<n; i++)
if(prime[i])
printf("%d ", i);
}
```
210:最大公因數
--
題目:輸入 a、b,輸出 a、b 的最大公因數和最小公倍數。
```c=
#include <stdio.h>
#include <stdlib.h>
int main(){
int a, b;
scanf("%d %d", &a, &b);
int aa=a, bb=b;
while(b!=0){
int c=a%b;
a=b;
b=c;
}
printf("%d\n%d\n", a, aa*bb/a);
}
```
說明:使用輾轉相除法。
302:期中考分數
--
```c=
#include <stdio.h>
#include <stdlib.h>
char compute(int score){
if(score<0 || score>100)
return -1;
if(score >= 60) score += 5;
else score+=10;
return score;
}
int main(){
int score, final;
scanf("%d", &score);
final= compute(score);
printf("%d", final);
}
```
304:3的倍數
--
```c=
#include <stdio.h>
#include <stdlib.h>
int compute(int a[]){
int i,n=0;
for(i=0; i<6; i++)
if(a[i]%3==0) n++;
return n;
}
int main(){
int n , arr[6],i;
for(i=0; i<6; i++)scanf("%d",&arr[i]);
printf("%d\n",compute(arr));
}
```
306:n階乘
--
```c=
#include <stdio.h>
#include <stdlib.h>
int compute(int n){
if(n==0) return 1;
if(n>=1) return n*compute(n-1);
}
int main(){
int n;
scanf("%d", &n);
printf("%d!=%d", n,compute(n));
}
```
308:費氏數列
--
```c=
#include <stdio.h>
#include <stdlib.h>
int compute(int n){
if(n<=2) return 1;
else return compute(n-1)+compute(n-2);
}
int main(){
int n, i, a[10];
scanf("%d", &n);
for(i=0; i<n; i++){
a[i]=compute(i+1);
}
for(i=n-1; i>=0; i--)
printf("fib(%d)=%d\n",i+1,a[i]);
}
```
310:阿姆斯壯數
--
```c=
#include <stdio.h>
#include <stdlib.h>
int compute(int n){
int i;
int x= 0;
for(i=1; i<n; i++){
int count = 1, num = 0, tmp=i;
while(tmp>9){
count++;
tmp/=10;
}
tmp=i;
while(tmp){
num+=(int)(pow(tmp%10,count)+0.5);
tmp/=10;
}
if(num==i){
printf("%d\n",i);
x+=i;
}
}
return x;
}
int main(){
int n;
scanf("%d",&n);
if(n>0&&n<1000){
printf("%d\n",compute(n));
}else printf("error\n");
}
```
402:比對兩字串
--
```c=
#include <stdio.h>
#include <stdlib.h>
int main() {
char str1[128];
char str2[128];
int n,result;
gets(str1);
gets(str2);
scanf("%d",&n);
if(n<=strlen(str1)){
result=strncmp(str1,str2,n);
if(result>0){
printf("%s>%s\n",str1,str2);
}else if(result==0){
printf("%s=%s\n",str1,str2);
}else{
printf("%s<%s\n",str1,str2);
}
}
else{
printf("error\n");
}
return 0;
}
```
404:出現最多次的英文字母
--
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int a[26],i;
char str[50];
for(i=0; i<26; i++)
a[i]=0;
scanf("%s",str);
for(i=0; i<strlen(str); i++)
a[str[i]-'a']++;
int max=0, max_num=a[0];
for(i=1; i<26; i++){
if(a[i]>max_num){
max=i;
max_num=a[i];
}
}
printf("%c\n",max+'a');
printf("%d\n",max_num);
return 0;
}
```
406:鍵盤
--
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char input[50]={}, input2[50]={};
scanf("%s", input);
char big[]="QWERTYUIOPPASDFGHJKLLZXCVBNMM";
char small[]="qwertyuioppasdfghjkllzxcvbnmm";
int i,j;
for(i=0; i<strlen(input); i++){
if(input[i]>='A' && input[i]<='Z'){
for(j=0; j<strlen(big); j++){
if(input[i]==big[j]){
input2[i]=big[j+1];
break;
}
}
}else if(input[i]>='a' && input[i]<='z'){
for(j=0; j<strlen(small); j++){
if(input[i]==small[j]){
input2[i]=small[j+1];
break;
}
}
}
}
printf("%s\n",input2);
return 0;
}
```
408:字串反轉
--
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void compute(char str[]);
int main(){
char str1[20],str2[20],str3[40];
scanf("%s %s",str1,str2);
if(strlen(str1)<=3||strlen(str2)<=3||strlen(str1)>20||strlen(str2)>20)
printf("error\n");
else{
printf("%d\n%d\n",strlen(str1),strlen(str2));
strcpy(str3, str1);
strcat(str3, str2);
compute(str3);
}
return 0;
}
void compute(char str[]){
int i=0,j=0;
while(str[i]!='\0')
i++;
for(j=i-1; j>=0; j--)
printf("%c",str[j]);
}
```
410:字首轉為大寫
--
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(){
int line,line_count=0;
scanf("%d",&line);
FILE *read=fopen("read.txt","r");
FILE *write=fopen("write.txt","w");
char buf[100];
while(fgets(buf,100,read)!=NULL){
int i;
line_count++;
int len = strlen(buf);
if(isalpha(buf[0]))
buf[0]=toupper(buf[0]);
for(i=1;i<len;i++){
if(buf[i-1]==' '&&isalpha(buf[i])){
buf[i]= toupper(buf[i]);
}
}
printf("%s",buf);
fputs(buf,write);
if(line==line_count)
break;
}
fclose(read);
fclose(write);
return 0;
}
```
502
--
```c=
#include <stdio.h>
#include <stdlib.h>
int main(){
int num,a[10],i=0,sum=1,j;
scanf("%d", &num);
if(num==0)
printf("0=0\n");
else{
while(num>0){
a[i]=num%10;
sum*=a[i];
num/=10;
i++;
}
for(j=i-1; j>=0; j--){
printf("%d",a[j]);
if(j!=0)
printf("*");
}
printf("=%d\n",sum);
}
return 0;
}
```
504
--
```c=
#include <stdio.h>
#include <stdlib.h>
int main(){
int num, ori, remainder, res=0;
scanf("%d", &num);
ori=num;
while(num!=0){
remainder=num%10;
num/=10;
res=(res*10)+remainder;
}
if(res==ori)
printf("Yes\n");
else
printf("No\n");
return 0;
}
```
506
--
```c=
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int compute(int n);
int main(){
int n, i;
scanf("%d", &n);
if(compute(n)==1)
printf("-1\n");
else{
for(i=2; i<=n; i++){
while(n!=i){
if(n%i==0){
printf("%d*",i);
n/=i;
}else break;
}
}
printf("%d", n);
}
}
int compute(int n){
if(n==1)
return 0;
int i;
for(i=2; i<=sqrt(n); i++){
if(n%i==0)
return 0;
}
return 1;
}
```
508
--
```c=
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
char a[8],b[8];
int i,power=7,asum=0,bsum=0,sum=0;
scanf("%s",a);
scanf("%s",b);
for(i=0; i<8; i++,power--){
if(a[i]=='1') asum+=pow(2,power);
if(b[i]=='1') bsum+=pow(2,power);
}
sum+=asum+bsum;
printf("%d + %d = %d\n",asum,bsum,sum);
char ans[8];
power=7;
for(i=0; i<8; i++,power--){
if(sum-pow(2,power)>=0){
sum-=pow(2,power);
ans[i]=1;
}
else ans[i]=0;
}
for(i=0; i<8; i++){
printf("%d",ans[i]);
}
return 0;
}
```
510
--
```c=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int n, m, i, j;
scanf("%d %d", &n, &m);
int grid[n+2][m+2];
memset(grid, 0, sizeof(grid));
for(i=1; i<=n; i++){
for(j=1; j<=m; j++){
scanf("%d", &grid[i][j]);
}
}
for(i=1; i<=n; i++){
for(j=1; j<=m; j++){
if(grid[i][j] == 0)
printf(" ");
else if(grid[i+1][j]==0||
grid[i][j+1]==0||
grid[i-1][j]==0||
grid[i][j-1]==0)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
```
602
--
```c=
#include <stdio.h>
#include <stdlib.h>
int main(){
char s[50]={-1};
int i, count=0;
scanf("%s", s);
for(i=0; i<sizeof(s); i++){
if(isupper(s[i])){
printf("%c",s[i]);
count++;
}
}
printf("\n");
for(i=0; i<sizeof(s); i++){
if(islower(s[i]))
printf("%c",s[i]);
}
printf("\n");
printf("%d",count);
return 0;
}
```
604
--
```c=
#include <stdio.h>
#include <stdlib.h>
int main(){
int num[9], sum=0, i, j, tmp;
for(i=0; i<9; i++){
scanf("%d", &num[i]);
sum+=num[i];
}
for(i=0; i<9; i++){
for(j=i+1; j<9; j++){
if(num[i]>num[j]){
tmp=num[j];
num[j]=num[i];
num[i]=tmp;
}
}
}
for(i=0; i<9; i++)
printf("%d\n", num[i]);
printf("sum = %d\n", sum);
}
```
606
--
```c=
#include <stdio.h>
#include <stdlib.h>
int main(){
char id[3][6];
int i;
for(i=0; i<3; i++){
scanf("%s", id[i]);
if(((id[i][0]+id[i][2]+id[i][4]-48*3)+(id[i][1]+id[i][3]-48*2)*5)%26 == id[i][5]-64)
printf("Pass\n");
else
printf("Fail\n");
}
return 0;
}
```
608
--
```c=
#include <stdio.h>
#include <stdlib.h>
int compute(int *a){
long long int base = 0;
int i, point = 0;
for(i=0; i<10; i++){
if(base==0){
base+=1;
base = base << a[i]-1; //左移
}
else
switch(a[i]){
case 0:
break;
case 1:
base = base << 1;
base += 1; //2^0=1
break;
case 2:
base = base << 2;
base += 2; //2^1=2
break;
case 3:
base = base << 3;
base += 4; //2^2=4
break;
case 4:
base = base << 4;
base += 8; //2^3=8
break;
default:
break;
}
}
base = base >> 3; //右移
while(base != 0){
if(base & 1){ //& 是位運算符
point++;
base = base >> 1;
}else{
base = base >> 1;
}
}
return point;
}
int main(){
int a[10], i;
for(i=0; i<10; i++){
scanf("%d", &a[i]);
}
printf("score = %d\n",compute(a));
return 0;
}
```
610
--
```c=
#include <stdio.h>
#include <stdlib.h>
int main(){
int a1, b1, a2, b2, i, j;
scanf("%d %d",&a1,&b1);
int m1[a1][b1];
for(i=0; i<a1; i++)
for(j=0; j<b1; j++)
scanf("%d",&m1[i][j]);
scanf("%d %d",&a2,&b2);
int m2[a2][b2];
for(i=0; i<a2; i++)
for(j=0; j<b2; j++)
scanf("%d",&m2[i][j]);
if(b1!=a2) printf("error\n");
else{
int ans[a1][b2], k, sum;
for(i=0; i<a1; i++){
for(j=0; j<b2; j++){
for(sum=k=0; k<b1; k++)
sum+=m1[i][k]*m2[k][j]; //重點,一直要做的是k(因為for在內部)
ans[i][j]=sum;
}
}
for(i=0; i<a1; i++){
for(j=0; j<b2; j++){
if(j!=0) printf(" ");
printf("%d",ans[i][j]);
}
printf("\n");
}
}
return 0;
}
```
702
--
```c=
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
char s[10];
int ans=0, i, p;
scanf("%s", s);
for(i=0, p=strlen(s)-1; i<strlen(s); i++, p--){
if(s[i] == '1')
ans += pow(2, p);
}
printf("%d\n", ans);
}
```
704
--
```c=
#include <stdio.h>
#include <string.h>
int main(){
int n, i, j, d, count=0, check;
scanf("%d", &n);
int a[n], b[n];
memset(b, 0, sizeof(b));
for(i=0; i<n; i++){
scanf("%d", &d);
check = 0;
for(j=0; j<count; j++){
if(a[j] == d){
b[j]++;
check = 1;
break;
}
}
if(check == 0){
a[count] = d;
b[count]++;
count++;
}
}
for(i=0; i<count; i++){
if(b[i] > n/2){
printf("%d\n", a[i]);
return 0;
}
}
printf("error\n");
}
```
706
--
```c=
#include <stdio.h>
int main(){
int a[10], count=3, i, j;
for(i=0; i<4; i++) scanf("%d", &a[i]);
FILE *f = fopen("read.txt", "r");
while(fscanf(f, "%d", &a[++count]) != EOF);
for(i=0; i<count; i++){
for(j=i+1; j<count; j++){
if(a[i] > a[j]){
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
FILE *f2 = fopen("write.txt", "w");
for(i=0; i<count; i++){
printf("%d\n", a[i]);
fprintf(f2, "%d\n", a[i]);
}
}
```
708
--
```c=
#include <stdio.h>
#include <stdlib.h>
struct time{
int hour, minute;
};
int compute(struct time t[]);
int main(){
int i;
struct time t[3];
for(i=0; i<3; i++){
scanf("%d %d",&t[i].hour, &t[i].minute);
}
int am = compute(t);
printf("%d\n",am);
return 0;
}
int compute(struct time t[]){
int i;
int am = 0;
for(i=0; i<3; i++){
if(t[i].hour>=0 && t[i].hour<=11){
am++;
if(t[i].hour==0)
printf("AM 12:%d\n", t[i].minute);
else
printf("AM %d:%d\n", t[i].hour, t[i].minute);
}else{
if(t[i].hour == 12)
printf("PM 12:%d\n", t[i].minute);
else
printf("PM %d:%d\n", t[i].hour-12, t[i].minute);
}
}
return am;
}
```
710
--
```c=
#include <stdio.h>
typedef struct Frame{
int frame;
int myTime;
}FRAME;
void FIFO(FRAME * frame, int * p){
int i=0, j=0, k=0, sw=10, index=0;
for(i=0; i<10; i++){
j=0;
while(p[i]!=-1){
if(frame[j].frame==0){
frame[j].frame=p[i];
frame[j].myTime=i;
p[i]=-1;
}else if(frame[j].frame==p[i]){
p[i]=-1;
}else if(j==3){
for(k=0; k<4; k++){
if(frame[k].myTime<sw){
sw=frame[k].myTime;
index=k;
}
}
frame[index].frame=p[i];
frame[index].myTime=i;
p[i]=-1;
sw=10;
}
j++;
}
for(j=0; j<4; j++) printf("%-2d",frame[j].frame);
printf("\n");
}
}
int main(){
int i, p[10], s;
for(i=0; i<10; i++) scanf("%d", &p[i]);
FRAME frame[4];
for(i=0; i<4; i++) frame[i].frame = 0;
FIFO(frame, p);
}
```