Blue Bridge Cup 2018 Preliminary-Which day will you return?

Title description

Xiao Ming was hijacked by unknown forces. After somehow he was thrown to the X-star station without any concern.
Xiao Ming learned that a spacecraft flies to the earth every day, but he needs a ticket of 108 yuan, but he is penniless.
He decided to work in X Star Wars. The kind boss agreed to include board and lodging and gave him 1 yuan on the first day.
And, every day after that, he will be 2 yuan more than the day before, until he has enough money to buy tickets.
Please calculate, Xiao Ming can make up 108 yuan in the first few days to return to Earth.

Output

Output an integer to indicate the answer

code show as below:

#include <iostream>
using namespace std;

int main() {
    
    
	int sum = 0, ans = 0;
	for (int i = 1;; i = i + 2) {
    
    
		sum += i;
		ans++;
		if (sum >= 108) {
    
    
			cout << ans << endl;
			return 0;
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_51955470/article/details/113667005