首页 > 试题广场 >

买房子

[编程题]买房子
  • 热度指数:7566 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平米的房子,现在价格是200万,假设房子价格以每年百分之K增长,并且该程序员未来年薪不变,且不吃不喝,不用交税,每年所得N万全都积攒起来,问第几年能够买下这套房子(第一年房价200万,收入N万)

输入描述:
    有多行,每行两个整数N(10<=N<=50), K(1<=K<=20)


输出描述:
    针对每组数据,如果在第21年或者之前就能买下这套房子,则输出一个整数M,表示最早需要在第M年能买下,否则输出Impossible,输出需要换行
示例1

输入

50 10
40 10
40 8

输出

8
Impossible
10

python solution:

while True:
    try:
        salary,increase=map(int,input().split())
        canBuy=False
        for i in range(1,22):
            if salary*i>=200*(1+increase/100)**(i-1):
                canBuy=i
                break
        print(canBuy if canBuy else "Impossible")

    except:
        break
发表于 2017-10-17 10:32:01 回复(0)
#include <iostream>
#include<vector>
using namespace std;
int main()
{
	double n, k;
	while (cin >> n >> k)
	{
		int cnt = 1;
		double sum = n;
		double price = 200;
		bool sta = true;
		while (sum < price)
		{
			sum += n;
			price = price*(1 + k / 100);
			cnt++;
			if (cnt > 21)
			{
				sta = false;
				break;
			}
		}
		if (sta == true)
			cout << cnt << endl;
		else
			cout << "Impossible" << endl;
	}
	return 0;
}

发表于 2017-04-16 23:59:12 回复(0)
#include<stdio.h>
int main(){
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int total=200,sum=n,i;
        if(sum>=total) 
            {printf("1\n"); continue;}//第一年就成功
        for(i=2;i<22;i++)
        {
            total*=1+k*0.01;//在当前基础上上涨,而不是一直在200的基础上
            sum+=n;
            if(sum>=total) break;
        }
        if(i<=21) printf("%d\n",i);
        else printf("Impossible\n");
    }
}

编辑于 2020-04-02 16:17:20 回复(1)
程序员真惨,买不起房QAQ
#include<iostream>
using namespace std;
int main(){
    int n,k;
    while(cin>>n>>k){
        int total=200;
        int money=n;
        int year=0,judge=0;
        for(int i=2;i<22;i++){
            total*=1+(double)k/100;
            money+=n;
            if(money>=total){
                year=i;
                judge=1;
                break;
            }
        }
        if(judge==1) cout<<year<<endl;
        else cout<<"Impossible"<<endl;
    }
}

发表于 2019-02-08 21:48:32 回复(1)
#include <iostream>
using namespace std;
int main(){
    int n,k;
    while(cin>>n>>k){
        double price=200;
        double despoit=n;
        int year=1;
        bool flag=false;
        while(n>=price/100*k){
            if(despoit>=price){
                flag=true;
                break;
            }
            despoit+=n;
            price+=price/100*k;
            year++;
        }
        if(!flag){
            cout<<"Impossible"<<endl;
        }else{
            cout<<year<<endl;
        }
    }
    return 0;
}

发表于 2019-02-03 21:43:10 回复(0)

苦逼的程序员

while True:
    try:
        n,k = list(map(int,input().split()))
        years = 1
        while n*years <= 200*(1+k/100)**(years-1) and years<=21:  #注意,第一年不增长
            years += 1
        if years > 21:
            print("Impossible")
        else:
            print(years)
    except Exception:
        break
编辑于 2018-10-03 01:30:36 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            double target = 200.0;
            int n = sc.nextInt();
            int k = sc.nextInt();
            int i;
            for(i=2;i<=21;i++){
                target = target*(k+100)/100;
                if(n*i >= target){
                    System.out.println(i);
                    break;
                }
            }
            if(i>21){
                System.out.println("Impossible");
            }
        }
    }
}

发表于 2018-09-25 10:12:05 回复(0)
while True:
    try:
        string = raw_input().strip().split()
        salary = int(string[0])*1.00
        num = salary
        k = int(string[1])*1.00
        payment = 200.00
        i = 1
        while num < payment and i <= 21:
            num += salary
            payment *= (k/100+1.0)
            i += 1
        
        print i if i<=21 else "Impossible"
    except:
        break

发表于 2016-07-04 09:40:41 回复(0)
21+年不涨工资的苦逼程序员。。。
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void)
{
    int n,k,year;
    double price,sum;
    while(cin>>n>>k)
    {
        year=1;
        price=200;
        sum=n;
        while(year<=21)
        {
            if(sum>=price) break;
            sum+=n;
            price=price*(1+k*0.01);
            year++;
        }
        if(year<=21&&sum>=price) cout<<year<<endl;
        if(year>21||sum<price)  cout<<"Impossible"<<endl;
    }
    return 0;
}
发表于 2019-11-18 22:40:56 回复(0)
一边调代码,一边在想这只程序猿怎么死的
#include<bits/stdc++.h>

using namespace std;

#define LL long long

int main(){
    int n,k;
    while(cin >> n >> k){
        double max = n * 1.0 / (k * 1.0 / 100);
        //cout<<" "<<max<<endl;
        int price = 200;
        int money = n;
        int y = 1;
        while(price < max){
            if(money >= price){
                cout<<y<<endl;
                break;
            }
            y ++;
            money += n;
            price *= (1 + k * 1.0 / 100);
            //cout<<"y = "<<y<<"\tmoney = "<<money<<"\t price = "<<price<<endl;

        }
        if(price >= max){
            cout<<"Impossible\n";
        }
    }

    return 0;
}

发表于 2019-03-19 14:53:22 回复(0)
def buyhoues(n, k):
    price = 200
    flag = 0
    for i in range(1, 24):
        if i == 1:
            continue
        else:
            price = price * (1 + k / 100)
        s = n*i 
        if price <= s and i < 21:
            flag = 1
            break
        
    if flag == 1:
        print(i)
    else:
        print('Impossible')

while True:
    try:
        N, K = map(int, input().split())
        buyhoues(N, K)
    except:
        break

编辑于 2024-03-20 15:53:23 回复(0)
#include <cmath>
#include <iostream>
using namespace std;

int main() {
    int n, k;
    while (cin >> n >> k) {
        int m = 1;
        double money = 0, cost = 200;   //程序员的现有资金和房子的价格
        while (m <= 21) {
            money += n; //程序员赚钱
            if (money >= cost) {
                break;
            }
            cost *= 1 + k / 100.0;  //房价上涨
            cost = floor(cost * 100 + 0.5) / 100;   //保留2位小数
            m++;    //进入下一年
        }
        m <= 21 ? cout << m << endl : cout << "Impossible" << endl;
    }
    return 0;
}

发表于 2024-02-01 12:17:30 回复(0)
买不起了,毁灭吧
发表于 2023-03-26 15:30:18 回复(0)
#include <cstdio>

int main(){
    int n,k;
    while(scanf("%d %d",&n,&k) != EOF){
        int money = 0;
        float price = 200;
        bool flag = false;
        for(int i = 0; i <= 20; ++i){
            if(money >= price){ //买得起房
                printf("%d\n",i);
                flag = true;
                break;
            }else{ //今年买不起。接着攒钱,房子涨价
                money += n;
                if(i == 0) continue; //第零年房价不增
                price += price*k/100;
            }
        }
        if(flag == false){
            printf("Impossible\n");
        }
    }
    return 0;
}

发表于 2023-03-22 13:29:48 回复(0)
难崩,第一次提交全是Impossible
#include <iostream>
using namespace std;

int main() {
    double n, k;
    double sum = 0;                         //总工资
    double price = 200;
    while (cin >> n >> k) { 
        k = k / 100;                        //k转换为百分比
        int i;
        for (i = 1; i <= 21; i++) {
            sum = i * n;
            if (i > 1)
                price = price * (1 + k);
            if (sum >= price) {
                cout << i << endl;
                break;
            }
        }
        if (i == 22)
            cout << "Impossible" << endl;
        sum = 0;
        price = 200;
    }
}


发表于 2023-03-15 00:45:19 回复(0)
什么苦逼买房
发表于 2023-03-14 16:43:31 回复(0)
//房子价格每年增长的部分多于程序猿工资时,就Impossible了(寄)
#include "stdio.h"

int main(){
    int N,K;//N为程序员年薪,K为房价每年增长率
    while (scanf("%d%d",&N,&K)!=EOF){
        double price_up = 0;//每年增加的房价
        double house = 200.0;
        int sum = N;//攒下的工资
        int year = 1;
        while (true){
            if(sum >= house){
                printf("%d\n",year);
                break;
            }
            if(price_up > N){
                printf("Impossible\n");
                break;
            }
            sum += N;
            price_up = house*K*1.0/100;
            house += price_up;
            ++year;
        }
    }
}

发表于 2023-03-09 17:55:17 回复(0)
奶奶的,我说怎么输出的都是impossible,我还以为是我房子涨价的方式写错了,后来发现是题设预设他一开始就有n万,这说明什么,说明要买房还是得有爸妈给的启动资金,不然是追不上涨价速度的
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
    double n,k;
    while(scanf("%lf%lf",&n,&k)!=-1){
        
        int m=1;
        double house=200,wallet=n;
        
        while(m<=21){
            m++;
            house*=1+k/100;
            wallet+=n;

            if(wallet>=house){
                cout<<m<<endl;
                break;
            }
        }

        if(m>21){
            cout<<"Impossible"<<endl;
        }      
    }
}


发表于 2023-02-13 15:22:17 回复(0)
#include<iostream>
using namespace std;
int main(){
    int N, K;
    while(cin >> N >> K){
        double price = 200;
        if(N >= price) {cout << 1 << endl; continue;}
        int year = 2;
        int deposit = N;
        double interval = price - deposit;
        for(;;year++){
            price *= (1 + K * 0.01);
            deposit += N;
            double new_interval = price - deposit;
            if(new_interval > interval) {cout << "Impossible" << endl; break;}
            interval = new_interval;
            if(interval <= 0) { cout << year << endl; break;}
        }
    }
}

发表于 2022-05-10 11:40:28 回复(0)
1. 注意一下:今年底房价是200,存款是50
2. 记得用double
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {
    int n, k, year; //n-年薪 k-房价涨幅 存款年数
    while (scanf("%d %d", &n, &k) != EOF) {
        double total_save = n, value = 200; //value-房价
        for (year = 1; total_save < value && n > value * k / 100.0 ; year++) {
            total_save += n;
            value *= (1.0 + k / 100.0);
        }
        if (total_save >= value) {
            printf("%d\n", year);
        } else {
            printf("Impossible\n");
        }
    }
}


发表于 2022-03-28 16:52:17 回复(0)

问题信息

难度:
66条回答 7423浏览

热门推荐

通过挑战的用户

查看代码