[NYOJ] [1094] cigarettes

Problem Description

Loading...

Problem Analysis

The idea is
Tom currently has n cigarettes, and every k cigarette butts can be rolled into a new cigarette
Find the maximum number of cigarettes Tom can smoke

The code idea is to simulate the smoking process
loop over the smoking process
track changes in cigarette butts and new cigarettes
until there are no new cigarettes left
then the loop terminates

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n,k;
		scanf("%d %d",&n,&k);
		int sum=0;
		int m=0;
		while(n) {
			sum+=n;
			m+=n;
			n=0;
			n+=m/k;
			m-=m/k*k;
		}
		printf("%d\n",sum);
	}
	return 0;
}

The standard solution’s loop is simpler
it treats n/k as the currently available un-smoked new cigarettes
and uses n to record the current cigarette butts

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include "stdio.h"
#include<fstream>
int main() {
	//freopen("d:\\1.txt","r",stdin);
	//freopen("d:\\2.txt","w",stdout);
	int m;
	scanf("%d",&m);
	while(m--) {
		int n,k,sum;
		scanf("%d%d",&n,&k);
		sum=n;
		while(n/k) {
			sum+=n/k;
			n=n/k+n%k;
		}
		printf("%d\n",sum);
	}
	return 0;
}

Problem link:cigarettes

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...