標籤:選擇結構設計 c程式設計
// 從鍵盤輸入一個小於1000的正數,輸出它的平方根(若平方根不是整數,則輸出它的整數部分)// 要求在輸入資料後檢查是否為小於1000的正數,若不是則要求重新輸入#include <stdio.h>#include <math.h>int main(){int a;double b;printf("請輸入一個小於1000的正數:");scanf("%d",&a);if( a > 0 && a < 1000 ){b = sqrt(a);printf("它的平方根是:%2.0f\n",b);}elseprintf("輸入的資料不在範圍,請重新輸入:\n");return 0;}
// 有一個函數:// x < 1 --- y = x 1 <= x < 10 --- y = 2 * x - 1 x >= 10 --- y = 3 * x - 11// 輸入x,求y#include <stdio.h>int main(){int x,y;printf("請輸入x值:");scanf("%d",&x);if(x < 1)y = x;else if(x >= 1 && x < 10)y = 2 * x - 1;elsey = 3 * x - 11;printf("對應的y值是:%d\n",y);return 0;}
// 給出100分製成績,要求輸出成績等級A,B,C,D,E。90分以上為A,80~89位B,70~79位C ,60~69位D,60以下為E#include <stdio.h>int main(){int a;printf("請輸入成績:");scanf("%d",&a);if(a >= 90)printf("等級是A\n");else if(a >= 80 && a <= 89)printf("等級是B\n");else if(a >= 70 && a <= 79)printf("等級是C\n");else if(a >= 60 && a <= 69)printf("等級是D\n");elseprintf("等級是E\n");return 0;}
// 給一個不多於5位的正整數,要求:// 1、求出它是幾位元// 2、分別輸出每一位元字// 3、按逆序輸出各位元字#include <stdio.h>#include <math.h>int main(){int a,b,c,d;int count = 1;printf("請輸入一個不多於5位的正整數:");scanf("%d",&a);c = a;printf("逆序:\n");while(c / 10 != 0){count++;b = c % 10;printf("%d\n",b);c = c / 10;}b = c % 10;printf("%d\n",b);printf("位元是:%d\n",count);printf("順序:\n");while(a % 10 != 0){d = a / (int)pow(10,(count-1));printf("%d\n",d);a = a % (int)pow(10,count-1);count--;}return 0;}
// 企業發放的獎金根據利潤提成。// i <= 100000 --- 10% // 100000 < i <= 200000 --- 低於100000的10%,高於100000的7.5%// 200000 < i <= 400000 --- 低於200000同上,高於200000的5%// 400000 < i <= 600000 --- 高於400000的3%,// 600000 < i <= 1000000 --- 高於600000的1.5%// i > 1000000 --- 超過1000000按1%。// 從鍵盤輸入當月利潤i,求應發獎金總數// if語句#include <stdio.h>int main(){int i,j;printf("請輸入當月利潤:");scanf("%d",&i);if(i <= 100000)j = i * 0.1;else if( i > 100000 && i <= 200000 )j = 100000 * 0.1 + ( i - 100000 ) * 0.075;else if( i > 200000 && i <= 400000 )j = 200000 * 0.1 + (i - 200000) * 0.05;else if( i > 400000 && i <= 600000 )j = 400000 * 0.1 + (i - 400000) * 0.03;else if( i > 600000 && i <= 1000000)j = 600000 * 0.1 + (i - 600000) * 0.015;elsej = 1000000 * 0.1 + (i - 1000000) * 0.01;printf("應得的獎金是:%d\n",j);return 0;}
// 企業發放的獎金根據利潤提成。// i <= 100000 --- 10% // 100000 < i <= 200000 --- 低於100000的10%,高於100000的7.5%// 200000 < i <= 400000 --- 低於200000同上,高於200000的5%// 400000 < i <= 600000 --- 高於400000的3%,// 600000 < i <= 1000000 --- 高於600000的1.5%// i > 1000000 --- 超過1000000按1%。// 從鍵盤輸入當月利潤i,求應發獎金總數// switch語句#include <stdio.h>int main(){int i,j;int c = i / 10000;printf("請輸入利潤:");scanf("%d",&i);if(c > 10)c = 10;switch(c){case 0:j = i * 0.1;break;case 1:100000 * 0.1 + ( i - 100000 ) * 0.075;break;case 2:break;case 3:200000 * 0.1 + (i - 200000) * 0.05;break;case 4:break;case 5:400000 * 0.1 + (i - 400000) * 0.03;break;case 6:break;case 7:break;case 8:break;case 9:600000 * 0.1 + (i - 600000) * 0.015;break;case 10:1000000 * 0.1 + (i - 1000000) * 0.01;break;default:break;}printf("利潤是:%d\n",j);return 0;}
// 輸入4個整數,要求按由小到大的順序輸出#include <stdio.h>int main() { int a[4] ; int i,j; int temp; printf("請輸入4個數: "); for( i = 0; i < 4; i++ ) { scanf("%d",&a[i]); } for( i = 0; i < 4; i++ ) { for( j = 0; j < (4-i); j++ ) if( a[j] > a[j+1] ) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } printf("這4個數由小到大的排列順序如下:"); for( i = 0; i < 4; i++ ) { printf("%d ",a[i]); } printf("\n"); return 0; }
// 有4個圓塔。圓心分別為(2,2),(-2,2),(-2,-2),(2,-2),圓半徑為1,這4個塔的高度為10,// 塔以外無建築物,今輸入任意一點的座標,求該點的建築高度(塔外高度為0)#include <stdio.h>int main(){int x,y,h;printf("請輸入x,y:");scanf("%d%d",&x,&y);if(x >= -2 && x <= 2 && y >= -2 && y <= 2)h = 10;elseh = 0;printf("該座標處塔高為:%d\n",h);}
【c語言】c程式設計--譚浩強--(第四章)--2