C++
7-1 計(jì)算階梯水價(jià) ????????????????????????????????????????
?
#include<stdio.h>
int main() {
??? float usage, fee;
??? scanf("%f", &usage);
??? if (usage <= 17) {
??????? fee = usage * 3.40;
??? } else if (usage <=30) {
??????? fee = 17 * 3.40 + (usage - 17) * 5.32;
??? } else {
??????? fee = 17 * 3.40 + 13 * 5.32 + (usage - 30) * 7.00;
??? }
printf("%.2f\n", fee);
??? return 0;
}
解方程
#include<stdio.h>
#include<math.h>
int main()
{
?????? float x1,x2;
?????? int a,b,c,delta;
?????? scanf("%d%d%d",&a,&b,&c);
?????? delta=pow(b,2)-4*a*c;
?????? if(delta==0){
????????????? printf("x=%.3f",(-1.0*b)/(2*a));
?????? }
?????? else if(delta>0){
????????????? x2=(-1.0*b+sqrt(delta))*1.0/(2*a);
????????????? x1=(-1.0*b-sqrt(delta))*1.0/(2*a);
????????????? printf("x1=%.3f\nx2=%.3f\n",x1,x2);
?????? }
??? else if(delta<0){
??????? printf("no real solution");
??? }
?????? return 0;
}
?
求分?jǐn)?shù)序列前N項(xiàng)和
#include<stdio.h>
int main(){
??? int n,i;
??? double a=1,b=1,t=0,sum=0;
??? scanf("%d",&n);
??? for(i=1;i<=n;i++)
??? {
??????? t=b;
??????? b=a+b;
??????? a=t;
????? ??sum=sum+b/a;
?
??? }
??? printf("%.2f",sum);
??? return 0;
}
求奇數(shù)
#include<stdio.h>
int main()
{
??? int n,sum=0;
??? scanf("%d ",&n);
??? while(n>0)
??? {
??????? if(n%2!=0)
??????? {
??????????? sum=sum+n;
??????? }
??????? scanf("%d ",&n);
??? }
??? printf("%d",sum);
??? return 0;
}
求整數(shù)的位數(shù)及各位數(shù)字之和
#include<stdio.h>
int main()
{
??? int a,b=0,c=0;
??? scanf("%d",&a);
??? while(a)
??? {
??????? b++;
??????? c+=a%10;
??????? a/=10;
??? }
??? printf("%d %d",b,c);
??? return 0;
}
7-6 猴子吃桃問(wèn)題
#include<stdio.h>
int main()
{
??? int n,s,i,x=1;
??? scanf("%d",&n);
??? for(i=1;i<=n;i++){
??????? s=x;
??????? x=2*(x+1);
??? }
??? printf("%d",s);
??? return 0;
}
7-7 打印菱形圖案
#include <stdio.h>
int main()
{
??? int n;
??? scanf("%d",&n);
??? n=(n+1)/2;
??? int i,j;
??? for(i=1;i<=n;i++)
??? {
??????? for(j=1;j<=2*(n-i);j++)
??????? {
??????????? printf(" ");
??????? }
??????? for(j=1;j<=2*i-1;j++)
??????? {
??????????? printf("* ");
??????? }
??????? printf("\n");
??? }
??? for(i=n-1;i>=1;i--)
??? {
??????? for(j=1;j<=2*(n-i);j++)
??????? {
??????????? printf(" ");
??????? }
??????? for(j=1;j<=2*i-1;j++)
??????? {
??????????? printf("* ");
??????? }
??????? printf("\n");
??? }
}
?
7-8 換硬幣
#include<stdio.h>
int main(){
int x;
int i,j,k;
int count=0;
scanf("%d",&x);
for(i=x/5;i>0;i--)
????????????? for(j=x/2;j>0;j--)
???????????????????? for(k=x;k>0;k--)
?????? if(i*5+j*2+k*1==x){
?????? printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n",i,j,k,i+j+k);
?????? count++;
}
printf("count = %d",count);
}
?
7-9 最大公約數(shù)和最小公倍數(shù)
#include<stdio.h>
int main()
{
??? int m,n;
??? int gongyue,gongbei;
??? scanf("%d %d",&m,&n);
??? int flag;
??? if(m>n){flag = n;}else flag = m;
??? for(int x= 1;x<=flag;x++)
??? {
??????? if(m%x==0 &&n%x==0)
??????? {
??????????? gongyue = x;
??????? }
??? }
??
???
??? gongbei = m*n/gongyue;
???
??? printf("%d %d",gongyue,gongbei);
???
??? return 0;
}
?
7-10 兔子繁衍問(wèn)題
#include<stdio.h>
int main()
{
?????? int x,i;
?????? int a=2, b=0, c=0;
?????? scanf("%d", &x);
?????? if (x > 1)
?????? {
????????????? for (i = 1; c < x; i++)
????????????? {
???????????????????? c = a + b;
???????????????????? b = a;
???????????????????? a = c;
????????????? }
???????
????????????? printf("%d", i+1);
?????? }
?????? else
????????????? printf("1");
?????? return 0;
}
7-10猜數(shù)字游戲
#include <stdio.h>
int main(){
??? // randNum:隨機(jī)數(shù)? guessNum:用戶猜測(cè)數(shù)? N:次數(shù)? flag:標(biāo)志
??? int randNum, guessNum, N, flag = 0;
???
??? scanf("%d %d", &randNum, &N);
???
??? for( int i=1; i<=N; i++ ){ //循環(huán)輸入N次結(jié)束
??????? scanf("%d", &guessNum);
???????
??????? if( guessNum < 0 )?? // 輸入不合法
??????????? break;
???????
??????? if( guessNum > randNum ) // 輸入太大
??????????? printf("Too big\n");
??????? else if( guessNum < randNum )? // 輸入太小
??????????? printf("Too small\n");
??????? else if( guessNum == randNum ){? // 相等
??????????? if( i == 1 )? // 1次成功
??????????????? printf("Bingo!\n");
??????????? else if( i <= 3 )? // 3次以內(nèi)成功
??????????????? printf("Lucky You!\n");
???????? ???else if( i > 3 )?? // 3次以上成功
??????????????? printf("Good Guess!\n");
??????????? flag = 1;
??????????? break;
??????? }
??? }
??? // 判斷是否在N次內(nèi)猜對(duì)
??? if( flag == 0 )
??????? printf("Game Over\n");
???
??? return 0;
}