有大大能幫解答的嗎(用Dev-c++ Compiler解答)
問題是:
程式功能:
四位數密碼輸入,最多三次機會:
三次密碼錯誤則結束程式。
密碼正確則執行下列功能:
資料輸入:由鍵盤輸入一整數
運算與決策:判斷此整數為奇數或偶數?
奇數:計算小於等於此整數之所有奇數和
偶數:計算小於等於此整數之所有偶數和
螢幕輸出:輸出奇數或偶數之判斷結果及運算結果(格式自訂)
2007-12-16 08:30:39 · 2 個解答 · 發問者 樂仔 1 in 電腦與網際網路 ➔ 程式設計
//Power by Visual Studio 2005
//Download Site: http://www.microsoft.com/taiwan/vstudio/express/
#include
#include
#include
unsigned int oddsum(unsigned int Odd){
return ((Odd-1)/2+1)*(Odd+1)/2;
}
unsigned int evensum(unsigned int Even){
return ((Even-2)/2+1)*(Even+2)/2;
}
int main(int argc, char* argv[]){
//=====START=====//
char *pw="1234",buffer[32767];
int num;
printf("Input password: "),scanf("%s",buffer);
if(!strcmp(pw,buffer)){
printf("Access allowed.\nInput a integer: "),scanf("%d",&num);
printf("Number is %s, ",(num&1?"Odd":"Even"));
printf("Sum= %u\n",(num&1?oddsum(num):evensum(num)));
}else{
printf("Access Denied.\n");
}
//=====END=====//
system("PAUSE");
return 0;
}
2007-12-16 15:20:03 補充:
密碼:1234
2007-12-16 10:19:49 · answer #1 · answered by Big_John-tw 7 · 0⤊ 0⤋
#include
int password = 1234;
main(){
int trail = 3; //機會次數
int i, sum;
int input;
do{
if(!trail){ //過了次數
printf("你試了很多次了! 再見!");
getch();
exit(1); //結束程式
}
trail--;
printf("請輸入密碼: ");
fflush(stdin);
scanf("%d", &input);
if(input != password){
printf("密碼錯誤!");
}
}while(input != password);
while(1){
printf("請輸入一整數: ");
fflush(stdin);
scanf("%d", &input);
if(input%2) //奇數或偶數
i = 1;
else
i = 2;
sum = 0; //總和
for(;i<=input; i += 2){
sum += i;
}
printf("總和是%d\n", sum); //結果
}
}
2007-12-16 16:47:17 補充:
「耗呆小綿羊」大大提了我一點.
總和方面是可以用"(首項+末項)*項數/2"來計
所以
sum = 0; //總和
for(;i<=input; i += 2){
sum += i;
}
可以改作
sum = (i+input)*((input-1)/2+1)/2;
2007-12-16 10:13:08 · answer #2 · answered by C 3 · 0⤊ 0⤋