發表文章

目前顯示的是 5月, 2018的文章

2018/5/14程式作業 第11題 for 四子一丙

第十一題 #include #include #include int main(void){ char gesture[4][10] = {"","剪刀","石頭","布"}; int iPlayer, iComp; srand(time(NULL)); while(1) { iComp = rand()%3 + 1; printf("\1-剪刀 2-石頭 3-布:\n"); scanf("%d", &iPlayer); if(iPlayer < 1 || iPlayer > 3) { printf("Invalid input!\n"); return 1; } printf("你:%s\t電腦: %s\n",gesture[iPlayer], gesture[iComp]); if(iPlayer - iComp == 0) printf("平手!\n"); else if(iPlayer - iComp == 1) printf("你贏了\n"); else if(iPlayer - iComp == -1) printf("我輸了 呼!\n"); } system("pause");return 0; }

2018/5/14程式作業 第10題 for 四子一丙

第十題 #include #include int main(void) { char type; float P; printf("請輸入用電類別\na為家庭用電\nb為工業用電\nc為營業用電\n"); scanf("%c",&type); printf("請輸入使用度數"); scanf("%f",&P); switch(type) { case 'a': if(P <= 100) P *=2.4; else if(P > 100 && P <= 300) P *=3.4; else if(P > 300) P *=4.4; break; case 'b': P *= 2.1; P += 150; break; case 'c': if(P >= 0 && P <= 300) P *= 6; else if(P > 300) P *= 6.8; break; } printf("應繳電費為%.1f", P); system("pause");return(0); }

2018/5/14程式作業 第9題 for 四子一丙

第九題 #include #include int main(void) { int A , B , C , D ,Max , Min; printf("請輸入相異的四個數"); scanf("%d %d %d %d",&A,&B,&C,&D); if(A > B && A > C && A > D) Max = A; else if(B > A && B > C && B > D) Max = B; else if(C > A && C > B && C > D) Max = C; else if(D > A && D > B && D > C) Max = D; if(A < B && A < C && A < D) Min = A; else if(B < A && B < C && B < D) Min = B; else if(C < A && C < B && C < D) Min = C; else if(D < A && D < B && D < C) Min = D; printf("Max為%d Min為%d", Max, Min); system("pause");return(0); }

2018/5/14程式作業 第8題 for 四子一丙

第八題 #include #include int IsPrime(int ipp) { int ix = 2,iTF = 0; while((ix*ix <= ipp ) && (iTF == 0)) { if(!(ipp%ix)) iTF = 1; ix++; } return(iTF); } int main(void) { int ip = 2,line = 0; while(ip <= 10000) { if(IsPrime(ip) == 0) { if(line == 15) { printf("\n"); line = 0; } line++; printf("%4d ",ip); } ip++; } system("pause");return(0); }

2018/5/14程式作業 第7題 for 四子一丙

第七題 #include #include int main(void) { int a,b,c; printf("請輸入三角形的三個邊abc"); scanf("%d %d %d",&a,&b,&c); if( a>0 & b>0 & c>0 & (a+b>c) & (a+c>b) & (b+c>a) ) { if( a==b && b==c ) printf("正三角"); else if ( (a*a == b*b + c*c) || (b*b == a*a + c*c) || (c*c == b*b + a*a) ) printf("直角三角"); else printf("其他三角"); } else printf("不是合法三角形"); system("pause"); return(0); }

2018/5/14程式作業 第6題 for 四子一丙

第六題 #include #include #include using namespace std; int main(void) { int score[500] = {0}, sum = 0; int i = 0; int range[6] = {0,0,0,0,0,0}; srand( (int)time(NULL)); for(i = 0;i <= 500; i++) { score[i] = rand()%101; sum += score[i]; } sum -= 999; sum /= (i - 1); printf("全班國文總平均為%d分\n",sum); for(i = 0 ;i <= 500; i++) { if(score[i] >= 0 && score[i] <= 59) range[0]++; else if(score[i] >= 60 && score[i] <= 69) range[1]++; else if(score[i] >= 70 && score[i] <= 79) range[2]++; else if(score[i] >= 80 && score[i] <= 89) range[3]++; else if(score[i] >= 90 && score[i] <= 99) range[4]++; else if(score[i] == 100) range[5]++; } printf(" 0~59分 %d 位學生\n",range[0]); printf("60~69分 %d 位學生\n",range[1]); printf("70~79分 %d 位學生\n",range[2]); printf("80~89分 %d 位學生\n",range[3]); printf("90~99分 %d 位學生\

2018/5/14程式作業 第5題 for 四子一丙

第5題 #include #include int main(void) { int score[500] = {0}, sum = 0; int i = 0; int range[6] = {0,0,0,0,0,0}; for(i = 0;score[i-1] != 999 ; i++) { scanf("%d", &score[i]); while((score[i] < 0) || (score[i] > 100) && (score[i] != 999)) { if((score[i] < 0) || (score[i] > 100 && (score[i] != 999))) { printf("請重新輸入\n"); scanf("%d", &score[i]); } } sum += score[i]; } sum -= 999; sum /= (i - 1); printf("全班國文總平均為%d分\n",sum); for(i = 0 ;score[i] != 999 ; i++) { if(score[i] >= 0 && score[i] <= 59) range[0]++; else if(score[i] >= 60 && score[i] <= 69) range[1]++; else if(score[i] >= 70 && score[i] <= 79) range[2]++; else if(score[i] >= 80 && score[i] <= 89) range[3]++; else if(score[i] >= 90 && score[i] <= 99) range[4]

2018/5/14程式作業 第4題 for 四子一丙

第四題 #include #include int main(void) { int ipp = 0, ix = 2, iTF = 0; float sum = 0, product = 1; printf("請輸入正整數"); scanf("%d", &ipp); if(ipp < 2 || ipp > 99999) { printf("您輸入的值太大了喔\n"); } else { while(ix <= ipp) { if(ipp%ix == 0) { sum += ix; product *= ix; iTF = 1; } ix++; } printf("所有因數和為%.0f\n所有因數積為%.0f\n", sum, product); } system("pause"); return(0); }

2018/5/14程式作業 第3題 for 四子一丙

第三題 #include #include int main(void) { char word[50]; int a = 0, b = 0, c = 0, d = 0; scanf("%[A-Za-z0-9 #]", &word); for(int i = 0; word[i] != 35 ;i++) { if(word[i] >= 97 && word[i] <= 122) a++; else if(word[i] >= 65 && word[i] <= 90) b++; else if(word[i] >= 48 && word[i] <= 57) c++; else if(word[i] == 32) d++; } printf("小寫字母出現%d次\n大寫字母出現%d次\n數字出現%d次\n空白出現%d次\n", a, b, c, d); system("pause"); return(0); }