24-Language Primer -24-cigarettes

Source: Internet
Author: User
Tags integer numbers

Title address: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=94?? Description
Tom has many cigarettes. We hypothesized that he has n cigarettes and smokes them
One by one keeping all the butts. Out of k > 1 butts He can roll a new cigarette.
now, know how many cigarettes can Tom have?

input
First input is a single Line,it's N and stands for there be n testdata.then there be n lines, each line contains Integer numbers giving the values of N and K.
Output
For each line of input, output one integer number on a separate line giving the maximum number of cigarettes that Peter CA N has.
Sample Input
3
4 3
10 3
100 5
Sample Output
5
-124?? Code:? #include <stdio.h>

Calculate how many cigarettes are pumped
N-Current number of cigarette butts
K-k cigarette butts can smoke 1 cigarettes.
static int calcigarettes (int n,int k);

int main ()
{
???? int readlen = 0;
???? scanf ("%d", &readlen);
???? GetChar ();
????
???? while (Readlen > 0)
???? {
????????? int n = 0;
????????? int k = 0;
????????? scanf ("%d%d", &n,&k);
????????? GetChar ();
????????? printf ("%d\n", Calcigarettes (n,k));
?????????
????????? --readlen;
???? }
????
???? return 0;
}

Calculate how many cigarettes are pumped
N-Current number of cigarette butts
K-k cigarette butts can smoke 1 cigarettes.
static int calcigarettes (int n,int k)
{
???? if (n<k | | k<=1)
????????? return n;

???? Already smoked n cigarettes.
???? int result = N;
????
???? Do
???? {
????????? K-cigarettes can be extracted from cigarette butts
????????? int addcigaretcount = n/k;
????????? n = n k + addcigaretcount;
????????? result + = Addcigaretcount;
???? }while (n >= k);//The remainder of the cigarette butts and K, equal also need to be
????
???? return result;
}?? When doing the problem, missed the original smoking cigarette-N In addition to judge the condition is also wrong, missing the equal situation of the judgment?? For a given test data test no problem, has been unable to find the problem where the error, and then learn from the "16-language primer -16-who won the highest scholarship" error to find the wrong experience from the input data, continuous input of the following data? 2 33 34 35 36 37 38 39 3 when Input 9 3 o'clock, find a little count. 1, finally identified the place where the error occurred.

24-Language Primer -24-cigarettes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.