2013年10月18日 星期五

TQC+ C 選擇敘述與迴圈 201 ~ 210

TQC+ C 試題總覽:LINK
Github 備份:LINK

/* TQC+ C - 201 */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
printf("請輸入i值: ");
scanf("%d", &i);
if (i%2 == 0)
/* 判斷是否相同使用==,非數學符號的=*/
printf("%d是偶數\n", i);
else
printf("%d是奇數\n", i);
system("PAUSE");
return 0;
}
view raw TQCplus_C_201 hosted with ❤ by GitHub
/* TQC+ C - 202 */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int score, adjust;
printf("請輸入分數:");
scanf("%d", &score);
if (score >= 60)//注意if(...)條件式後面不可以有分號
adjust = score + 10;
else
adjust = score + 5;
printf("調整後的分數為%d", adjust);
system("PAUSE");
return 0;
}
view raw TQCplus_C_202 hosted with ❤ by GitHub
/* TQC+ C - 203 */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int floor;
printf("請問您住哪一層樓(1-4): ");
scanf("%d", &floor);
switch(floor) {
case 1:
printf("您住在一樓\n");
break;
case 2:
printf("您住在二樓\n");
break;
case 3:
printf("您住在三樓\n");
break;
case 4:
printf("您住在四樓\n");
break;
default:
printf("您輸入的樓層不正確");
}
system("PAUSE");
return 0;
}
view raw TQCplus_C_203 hosted with ❤ by GitHub
/* TQC+ C - 204 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char ch = NULL;
printf("請問您的身份:以字母表示分別如下:\nU:表示大學生,M:表示研究生,P:表示博士生 ");
scanf("%c", &ch);
switch (ch) {
case 'U':
printf("您是大學生\n");
break;
case 'M':
printf("您是研究生\n");
break;
case 'P':
printf("您是博士生\n");
break;
default:
printf("您輸入身份不正確\n");
}
system("PAUSE");
return 0;
}
view raw TQCplus_C_204 hosted with ❤ by GitHub
/* TQC+ C - 205 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i, num;
int num1 = 0, num2 = 0, num3 = 0, num4 = 0, others = 0;
for (i=1; i<=10; i++) {
/* 條件式為小於等於10,因為初始值為1 */
printf("\n班長候選人如下:\n1: 小王\n2: 小李\n3: 小蔡\n4: 小陳\n");
printf("請問您要選哪位候選人(1-4):");
scanf("%d", &num);
switch (num) {
case 1:
num1++;
break;
case 2:
num2++;
break;
case 3:
num3++;
break;
case 4:
num4++;
break;
default:
printf("您輸入的號碼不正確\n");
others++; /* 補上廢票的計算 */
}
printf("目前得票數\n小王:%d, 小李:%d, 小蔡:%d, 小陳:%d, 廢票:%d\n", num1, num2, num3, num4, others);
}
system("PAUSE");
return 0;
}
view raw TQCplus_C_205 hosted with ❤ by GitHub
/* TQC+ C - 206 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i=1, total=0;
while (i <= 100)
{
total += i;
i++;
/* 修改程式的邏輯,因為i從1開始,
* 所以要先加到total裡面,
* 之後再做i++的動作
*/
}
printf("1加到100的總和:%d\n", total);
system("PAUSE");
return 0;
}
view raw TQCplus_C_206 hosted with ❤ by GitHub
/* TQC+ C - 207 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i=1, total=0;
/* i從1開始加 */
while (i <= 100) {
/* 判斷式為小於等於100,因為100也要加進去 */
total += i;
i++;
}
printf("1加到100的總和:%d\n", total);
system("PAUSE");
return 0;
}
view raw TQCplus_C_207 hosted with ❤ by GitHub
/* TQC+ C - 208 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i, total=0; /* 設定加總初始值為0 */
for (i=1; i <= 100; i++)
/* 修改判斷式邏輯
* for(...)後面不可以有分號
* 會變得無意義
*/
if(i%2 == 0) /* 設定條件,如果為偶數,則進入加總 */
total += i;
printf("1到100的偶數和: %d\n", total);
system("PAUSE");
return 0;
}
view raw TQCplus_C_208 hosted with ❤ by GitHub
/* TQC+ C - 209 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i=1, total=0;
/* 要設定加總的初始值為0 */
do {
if(i%2 == 1) /* 設定條件,如果為奇數才進去加總 */
total += i;
i++; /* 修改邏輯,要先加進去,才能++ */
} while (i<=100); /* 切記do-while的判斷式後面一定要加分號 */
printf("1到100的奇數和: %d\n", total);
system("PAUSE");
return 0;
}
view raw TQCplus_C_209 hosted with ❤ by GitHub
/* TQC+ C - 210 */
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i=0, total=0;
/* 設定i的初始值為0,加總的初始值也設定為0 */
do {
i+=2;
total += i;
} while (i<100);
/* 在這邊最後一輪的i值為100
* 但碰到最後的判斷是
* 因為100沒有小於100
* 所以不會在下一輪
*/
printf("1到100的偶數和: %d\n", total);
system("PAUSE");
return 0;
}
view raw TQCplus_C_210 hosted with ❤ by GitHub



TQC+ C 試題總覽:LINK
Github 備份:LINK


本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~

沒有留言:

張貼留言