&&&&題目:
作一while搭配switch,可一直選擇case的程式,並可使用者終止
Input: 1
Output: wanpeeleng
Input: 2
Output: yenya
Input: 3
Output: poohd
\\\\\\\\\\\\\\\\\\\\\\
########答案:
#include
#include
int main(){
char i;
scanf("%c", &i);
while(i!='4'){
switch(i)
{
case'1':
printf("wanpeeleng");
break;
case'2':
printf("yenya");
break;
case'3':
printf("poohd");
break;
}
scanf("%c", &i);
}
system("pause");
}
//////////////////////////////////////////////
&&&&&&&&&&題目:
矩陣:
任意輸入9個數字,輸出時的格式要為3X3矩陣型式
Input:1,2,3,4,5,6,7,8,9
Output: 1 2 3
4 5 6
7 8 9
\\\\\\\\\\\\\\\\\\\\\\
########答案:
#include
#include
int array[9];
int main(){
int i;
for(i=1;i<=9;i++){
scanf("%d",&array[i]);
}
for(i=1;i<=9;i=i+3){
printf("%d %d %d\n",array[i],array[i+1],array[i+2]);
}
system("pause");
}
//////////////////////////////////////////////
&&&&&&&&&&題目:
N! 用遞迴實作
5!
=5*(5-1)!
=5*4*(4-1)!
=5*4*3*(3-1)!
=5*4*3*2*1
\\\\\\\\\\\\\\\\\\\\\\
########答案:
#include
#include
int fact(int n);
int main(){
int n;
scanf("%d",&n);
int a;
a=fact(n);
printf("%d",a);
system("pause");
return 0;
}
int fact(int n){
if(n<=1){
return 1;
}
else {
return n*fact(n-1);
}
}
//////////////////////////////////////////////
&&&&&&&&&&題目:
File I/O:
從檔案讀入一串數字,將這些數字倒過來輸出在同一檔案中。
Ex: data.txt 的內容: 1,2,3,4,5,6,7,8,9
經過程式轉換後
data.txt 的內容: 9,8,7,6,5,4,3,2,1
\\\\\\\\\\\\\\\\\\\\\\
########答案:
#include
#include
int main(){
FILE *fp1;
char buff[50];
fp1 = fopen("brabrabra.txt","r");
int i=0;
double num[9];
while(!feof(fp1)){
fscanf(fp1, "%s\n", buff);
num[i]=atof(buff);
i++;
}
fp1 = fopen("brabrabra.txt","w");
for(i=9;i>=1;i--){
fprintf(fp1, "%f\n", num[i]);
}
fclose(fp1);
}
請先 登入 以發表留言。