求教这个问题用C语言的实现

Problem Description
You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the scene and takes 1 coin from Basket 1, 2 coins from Basket 2, and so on, up to and including N-1 coins from Basket N-1. He does not take any coins from Basket N. He weighs the selected coins and concludes which of the N baskets contains the lighter coins. Your mission is to emulate the wizard's computation.

Input
The input file will consist of one or more lines; each line will contain data for one instance of the problem. More specifically, each line will contain four positive integers, separated by one blank space. The first three integers are, respectively, the numbers N, w, and d, as described above. The fourth integer is the result of weighing the selected coins.

N will be at least 2 and not more than 8000. The value of w will be at most 30. The value of d will be less than w.

Output
For each instance of the problem, your program will produce one line of output, consisting of one positive integer: the number of the basket that contains lighter coins than the other baskets.

Sample Input
10 25 8 1109
10 25 8 1045
8000 30 12 959879400

Sample Output
2
10
50

2个回答

#include<stdio.h>
#include<string.h>
#define LL long long
int main()
{
    int w,d,n;
    int i;
    LL m,sum,ans;
    while(scanf("%d%d%d%lld",&n,&w,&d,&m)!=EOF)
    {
        sum=0;
        for(i=1;i<n;i++)
            sum+=i*w;
        ans=sum-m;
        if(ans==0)
        printf("%d\n",n);
        else
        printf("%lld\n",ans/d);
    }
    return 0;
}
暴力破解,按顺序从1到N-1假设它为特殊篮子,如果最后求和如果跟输入的结果一样,说明假设成立,如果所有假设都不成立,那说明最后一个是特殊的篮子,另外,n<8000,需要用到大数加法,这里用我用char*,(用string操作比较简单) 
如果有帮助请采纳=.=
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
char* trans(char a[]) {         //字符串倒叙
    char ch;
    for (int i = 0; i < strlen(a)/2; i++) {
        ch = a[i];
        a[i] = a[strlen(a) - i - 1];
        a[strlen(a) - i - 1] = ch;
    }
    return a;
}
char* add(char a[], char b[])   {       //把2个字符串相加
    while (strlen(a) != strlen(b)) {
        int al = strlen(a), bl = strlen(b);
        if (al > bl) 
            b[bl] = '0';
        else 
            a[al] = '0';
        a[al+1] = b[bl+1] = '\0';
    }
    a[strlen(a)]=b[strlen(b)] = '\0';
    int up = 0, sum;        //up储存进位
    char c[1000];
    int i = 0;
    for ( ; i < strlen(a); i++) {   //使用字符串模拟大数加法
        sum = a[i] - '0' + b[i] - '0' + up;
        up = sum / 10;
        sum %= 10;
        c[i] = char(sum+'0');
        c[i + 1] = '\0';
    }
    if (up > 0) {
        c[i] = up + '0';
        c[i + 1] = '\0';
    }
    return c;
}
int main(){
    int N, w, d;
    char sum[1000]="0", result[1000];
    int flag;                       //用于记录是否有假设成立
    char tmp[1000];
    while (1) {
        flag = 1;
        scanf("%d%d%d%s", &N, &w, &d, &result);
        int i = 1;
        for (; i < N; i++) {        //假设第i个篮子是特殊的
            strcpy(sum, "0");       //重置sum
            for (int j = 1; j < N; j++) {
                if (i == j)
                    itoa((w - d)*j, tmp, 10);
                else
                    itoa(w*j, tmp, 10);
                strcpy(sum, trans(add(trans(sum), trans(tmp))));
            }
            if (strcmp(sum, result) == 0) {
                flag = 0;
                printf("%d\n", i);
                break;
            }
        }
        if (flag) 
            printf("%d\n", N);
    }
    return 0;
}

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
求教:c语言实现
1:给定一个【0,1】区间均匀随机数,给出产生泊松分布的随机数原理分布rn rn2:给出产生高斯分币的随机数原理分布
c语言问题求教
warning C4518: 'void ' : storage-class or type specifier(s) unexpected here; ignoredrnerror C2146: syntax error : missing ';' before identifier 'save'rnfatal error C1004: unexpected end of file foundrn执行 cl.exe 时出错.rnrn求大神解答,谢谢
c语言问题,求教!
#include "math.h"rn#define S(a,b,c) (a+b+c)/2rn#define Area(a,b,c) sqrt(S(a,b,c)*(S(a,b,c)-a)*(S(a,b,c)-b)*(S(a,b,c)-c))rnrnvoid main(void)rnrn int x,y,z,a;rnrn scanf("%d,%d,%d",&x,&y,&z);rn a=Area(x,y,z);rn printf("\n%d,%d,%d\n",x,y,z);rn printf("%d\n",a);rnrn这是一个计算三角形面积的小程序,如果不加入#include "math.h"语句,输入3,4,5后rn计算的结果是8,而加入#include "math.h"语句,输入3,4,5后计算结果是6,请问这是rn为什么?
C语言问题求教
[code=C/C++]rnvoid str_cat()rnrn char string[48];rn memset(string,0,sizeof(string));rn char string1[12] = "Hi!";rn char string2[12] = "Xiaoming,";rn char string3[12] = "Good morning!";rnrn strcat(string, string1);rn strcat(string, string2);rn strcat(string, string3);rnrn printf("%s \n", string); rnrn[/code]rnrn打印结果为啥是:Hi!Xiaoming,Good morningXiaoming, rn而不是:Hi!Xiaoming,Good morning! rn
这个这样用C语言实现?
我已经在图形界面 并且在界面上做了一个按钮 rn这个按钮我想实现的功能 就是在命令提示符下 输入的命令是 service ipsec restart rn我要用C语言怎么样实现啊~rnrn谢谢大家帮忙rnrn帮我写个简单的例子来就好了
怎么用C语言实现这个?
*rn * *rn * * *rn * * * *rn * * * * *rn * * * *rn * * *rn * *rn *
求教:用C语言发邮件的问题
小第最近遇到一个问题,即用标准C语言编写一个程序利用SMTP发送邮件.(不考虑附件)rn望各位大虾不啬指教,小弟感激不尽!!!rn 请给出一些编写思路.建议或部分核心程序.谢谢!!rn
用C语言实现
[img=http://b50.photo.store.qq.com/http_imgload.cgi?/rurl4_b=6214c8ad69f68f70aa5f5a2b98a28e388825dafc1f7cbc65978961b54d5c124a9e268c984dc402a627e8e1ba8d2ef6c60a8502baf76e237165dd0b179b1bd43ef8aa033a66966f9754a0e5a941a41b5382ee433e&a=46&b=50][/img]rn[img=http://b52.photo.store.qq.com/http_imgload.cgi?/rurl4_b=6214c8ad69f68f70aa5f5a2b98a28e38a674d3d86678018b6467334667bcb89a6be5c84204d54bd587ca1cd715436ea90caefbb36d397a2c09e8b8db673cd5972cf7b8f5fee0fe370655d090993400fccf5db353&a=49&b=52][/img]rn[img=http://b50.photo.store.qq.com/http_imgload.cgi?/rurl4_b=6214c8ad69f68f70aa5f5a2b98a28e384d78e3de6f089020bb17e3d2bc6529eff776150ae62363dedeaf66cb81717dd4da573e1c9c35705a6d56473fed164999c566fecaed5178a3f9bb75097da816b08fc6feeb&a=50&b=50][/img]
用c语言实现
设多项式P(x)采用课本中所述链接方法存储。写一算法,对给定的x值,求P(x)的值。用C语言实现。
简单xml语言的c语言实现——求教!!!
一个简单的XML文件: rn rn rn ABC rn 123456 rn rn rn每组“标记”包含两部分:标记名称和标记值。例如, 123456 是一组标记。标记名称包含在一对尖括号中(" <" 和 ">"), 称为标记头, 称为标记尾。 标记头和标记尾之间的称为标记的值。 在上面的例子中,标记名称=id, 标记值=123456。 rn有时候,在标记头和标记尾之间还可以嵌入其他标记组。如标记 包含了标记 和标记 作为它的值,我们称这种包含关系为父-子关系。 或者说, 是 和 的父亲。 rn这种简单的XML 文件可以用一种树状结构来描述, 它由一组结点的集合组成。如下图所示, 上述的文件中的XM语句描述的是一棵包含了4个结点(用圆圈表示)的树。在这个作业中每个结点包含3个成员变量: rn• 一个称为 "name" 的字符串变量- 用于存储结点的名称 rn• 一个称为 "value" 的字符串变量- 用于存储结点的值 rn• 一个指向儿子的指针数组 – 用于指向当前结点的各个儿子结点 rn• 如果需要, 你也可以加上其它的成员变量。 rnrn这个作业的目标是需要你编制一个程序,读入一个XML文件,然后从顶部(根结点)按层次顺序打印出该树状结构的所有结点(每行打印一个结点的内容), 每层按从左到右的顺序打印该层的节点。每个结点包含它的3个成员变量值。 rn每个结点内容的打印顺序为:结点编号 - 结点标记名称:标记值,儿子:儿子结点标号,儿子结点标号,…… rn例如,上面的文件将打印输出如下: rnElement 1- class : null, child: Element 2 rnElement 2 - student: null, child: Element 3, Element 4 rnElement 3 - name : ABC, child: null rnElement 4 - id: 123456, child null rn注意,在你的作业中不允许使用现成的XML 解释器。 输入的文件是一个括号完全匹配的有效的 XML文件。 每个结点的儿子数可以不相同,层次的深度也不限定。尖括号 <>之间无空格。 rn你的程序首先应当检查括号的匹配情况。如果括号不匹配,应能报告错误并退出程序的运行。 rn以下是你可以用来测试验证你的程序的XML实例文件:test.xml 和对应的结果输出文件:Testoutput rn你的程序的运行格式是:程序文件名 输入的XML文件名 ,例如:>yourprogram test.xml rn
【C语言 】用C语言实现乘法口诀表
程序代码:#define _CRT_SECURE_NO_WARNINGS 1 #include #include void MultiplicationTable() {     for (int i = 1; i < 10; i++)     {         for (int j = 1; j < 10; j++)         {             printf("%d*%d=%
这个动态规划的问题有点难,用C语言实现
Problem DescriptionrnMr Cheng is a collector of old Chinese porcelain, more specifically late 15th century Feng dynasty vases. The art of vase-making at this time followed very strict artistic rules. There was a limited number of accepted styles, each defined by its shape and decoration. More specifically, there were 36 vase shapes and 36 different patterns of decoration – in all 1296 different styles.rnrnFor a collector, the obvious goal is to own a sample of each of the 1296 styles. Mr Cheng however, like so many other collectors, could never afford a complete collection, and instead concentrates on some shapes and some decorations. As symmetry between shape and decoration was one of thernmain aestheathical paradigms of the Feng dynasty, Mr Cheng wants to have a full collection of all combinations of k shapes and k decorations, for as large a k as possible. However, he has discovered that determining this k for a given collection is not always trivial. This means that his collection might actually be better than he thinks. Can you help him?rn rnrnInputrnOn the first line of the input, there is a single positive integer n, telling the number of test scenarios to follow. Each test scenario begins with a line containing a single positive integer m <=100 , the number of vases in the collection. Then follow m lines, one per vase, each with a pair of numbers, si and di, separated by a single space, where si ( 0 < i <= 36 ) indicates the shape of Mr Cheng's i:th vase, and di ( 0 < i <=36 ) indicates its decoration.rn rnrnOutputrnFor each test scenario, output one line containing the maximum k, such that there are k shapes and k decorations for which Mr Cheng's collection contains all k*k combined styles.rn rnrnSample Inputrn2rn5rn11 13rn23 5rn17 36rn11 5rn23 13rn2rn23 15rn15 23rn rnrnSample Outputrn2rn1
Cover,这个问题用C语言怎么实现的?
Problem DescriptionrnThe Wall has down and the King in the north has to send his soldiers to sentinel.rnThe North can be regard as a undirected graph (not necessary to be connected), one soldier can cover one path. Today there's no so many people still breathing in the north, so the King wants to minimize the number of soldiers he sent to cover each edge exactly once. As a master of his, you should tell him how to arrange soldiers.rn rnrnInputrnThere might be multiple test cases, no more than 20. You need to read till the end of input.rnIn the first line, two integers n and m, representing the number of nodes and edges in the graph.rnIn the following m lines, each contain two integers, representing two ends of an edge.rnThere are no parallel edges or self loops.rn1≤n,m≤100000rn rnrnOutputrnFor each test case, the first line contains number of needed routes, p.rnFor the following p lines, an integer x in the beginning, followed by x integers, representing the list of used edges. Every integer should be a positive or negative integer. Its absolute value represents the number of chosen edge (1~n). If it's positive, it shows that this edge should be passed as the direction as the input, otherwise this edge should be passed in the direction different from the input. Edges should be in correct order.rn rnrnSample Inputrn3 3rn1 2rn1 3rn2 3rn rnrnSample Outputrn1rn3 1 3 -2
求教一个C语言问题
[code=C/C++]rn#include rnrn#define CUBE(X) ((X) * (X) * (X))rnrnvoid main()rnrn int i = 2;rn int a = 81 / CUBE(++i);rn printf("%d\n", a);rnrn[/code]rnrn我在VC++6.0下运行的结果是1,求教为什么,代码没有问题,是书上的。
求教C语言图形编程的问题
请问:许多图形处理函数都需要横纵坐标作参数,那如果我想在窗口的特定位置进行画图,我要怎么知道那个特定点的横纵坐标呢?
C语言新手问题求教
写一程序,输入X,输出Y。rn当x<1时,y=x;rn当1<=x<10时,y=2x-1;rn当x>=10时,y=3x-11.rn下面是我写的代码,编译通不过。rn请问错误在哪?rnmain()rnrnfloat x,y;rnscanf ("%f",&x);rnif(x>=1)rn if(1<=x&&x<10) y=2x-1;rn else y=3x-11;rnelse y=x;rnprintf ("y=%f",y);rnrn
C语言新手求教问题
正在学习C语言,书后题有个不会了,求指教:要求用户每次输入一个的数,当输入零的时候视为最大,而且这些数中并不仅限于整数;下面是例子:rnenter a number:60rnenter a number:38.3rnenter a number:4.89rnenter a number:100.62rnenter a number:75.2295rnenter a number:0rnthe largest number entered was 100.62rn求指教哈,本人小菜鸟刚刚学,学到while 和for循环这里,我想的是应该用while循环while(i!=0)这样,但是就是怎么输出最大的值呢
求教一个c语言问题
一个大的工程项目包含三个小项目,每一个小项目都有很多.c文件,大项目除了包含这三个小项目外还有些.C文件实现整合和控制,现在我想在大项目的一个 ctrl.c 文件调用三个小项目中的一个capture.c文件中的变量“char *buf”,需要怎么做,除了外部ctrl.c文件包含 capture.h头文件 把这个buf变量设成全局变量还需要做什么呢?rn(语言表达能力没做好 ,希望大家谅解啊 )rnC语言真是学到老活到老
求教:C语言 文件操作问题
我用C写一个文件操作程序。rn发现只是向文件里写东西的函数,如fwrite、fputs、fputc.........rn就是没有删除内容的函数..!!!!!费解费解~~~~rnrn1、如果我需要删除文件某些内容怎么办?rn2、另外,有插入内容的函数吗? (上边举的写函数都是以覆盖形式写的)rnrnrn先谢过谢过....
求教关于C语言的问题
main()rnrn int x,y,z;rn x=y=y=2;rn ++x||++y&&++z;rn printf("x=%d,y=%d,z=%d\n",x,y,z);rnrnrnrn为什么参考书上结果是 3,2,2啊,我自己是这样算的++y&&++z结果为1,后面的或不用算了,故 y=3,z=3,x=2啊,为什么啊?
c语言二级指针问题求教
我写了个有关二级指针的程序,可是有问题,大家帮忙看看。小弟谢谢了!!rn#include rn#include rnmain()rn char**p; int count,j,i; p=0;/* p为指向指针的指针*/rn scanf("%d",&count);rn p=calloc(count,sizeof(char*));rn for(j=0;j
C语言问题,求教大神?????
int i =0x12345678;rni 的地址是大端储存还是小端储存(只是自己书写的地址)
求教,C语言system()问题
本人是C语言新手,在VC中写下如下程序rn int main()rnrn time_t rawtime;rn struct tm* timeinfo;rn time(&rawtime);rn printf("About to spwan command.com and run a DOS commmand\n");rn system("dir");rn system("pause");rn printf("%d\n",rawtime);rn system("pause");rn system("telnet");rn printf("%d\n",rawtime);rnrn为什么在程序最后,调用了sytem("telnet");不会在窗口中有文字输出呢?rnrn
c语言初级问题,求教
给定10个数 从中选取3个 并将所有结果显示rn这如何编程啊
C 语言简单问题 求教
// 我是想用递归循环出所有目录rn//rn#include rn#include rn#include rnrnstruct Filernrn char *p;rn struct File *TT; rn struct File *Next; rn;rnint FindFile(char *str);rnint main(int argc, char *argv[])rnrn FindFile("c:\\"); // 要递归的起始路径rn getch();rn return 0;rnrnrnint FindFile(char *str)rnrn WIN32_FIND_DATA wfd;rn char *p = (char *)malloc(strlen(str)+1);rn char *str1 = (char *)malloc(strlen(str)+4);rn strcpy(str1,str);rn strcat(str1,"\\*.*");rn HANDLE ff = FindFirstFile(str1,&wfd);rn dornrn if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&rn strcmp(wfd.cFileName,".") != 0 &&rn strcmp(wfd.cFileName,"..") != 0 )rn rn p = (char *)malloc(strlen(str)+strlen(wfd.cFileName)+4);rn strcpy(p,str);rn strcat(p,"\\");rn strcat(p,wfd.cFileName); rn puts(p);rn FindFile(p); rn puts("-------------");rn rn rn while(FindNextFile(ff,&wfd));rn FindClose(ff);rn return 0;rnrn-----------------------------问题来了----------------------rn在递归中 一般只要目录多与 200 个左右 就会跳出什么0x77880a 不能读rn要是200个目录以下 就可以顺利的递归完成rn我想是不是栈的容量有限了 溢出了? 望高手解答
C语言 栈的问题求教
int DestroyStack(pLStack pLS)rnrn ClearStack(pLS);rn free(pLS);rn pLS = NULL;rnrnrnint StackEmpty(pLStack pLS)rnrn if(pLS == NULL)rn rn printf("栈不存在!" ); rn exit(0);rn rn if(pLS->next==NULL)rn rn return 1;rn rn elsern rn return 0;rn rnrnrn先调用上面的DestroyStack函数,然后调用StackEmpty函数。但是没有出现printf("栈不存在!" ); ??rn为什么??rn其中rntypedef struct ListStackrnrn DataType data;rn struct ListStack * next;rnLStack,* pLStack;
C语言求教简单问题
s=10;i=10;rnfor(;--i;)s+=i;rn这是什么意思?
C语言二叉树遍历问题求教
某二叉树的前序序列为ABCD,中序序列为DCBA,则后序序列为?————答案为:DCBA,求大侠指教解题详细思路步骤,谢谢
C语言问题 新手求教
问题是关于链表的操作。操作包含链表的初始化,链表的释放和判断链表是否为空等等。问题是若对一个已经释放了的链表进行是否为空的判断应该是有出错的提示的,我的意思是在编写这个函数时应该考虑,但我不知道该如何实现提示出错这一点。忘大家指教。链表节点结构为:rntypedef struct lnodern char ldata;rn struct lnode *next;rnlnode;
求教几个C语言问题
1.fopen()函数的问题,fopen几个参数很是不解,r、r+、w、w+,这几个参数到底是有什么区别?谭书上说的我不是很理解。rn2.算法与数据结构对于我一名普通非计算机专业的学生来说有点吃力,我看了1个月了,还剩2个月自学完应付考研题来得及么?一方面自己成绩还可以,对计算机也感兴趣,但学到现在感觉有点吃力。rn希望前辈们给我一些意见,鼓励和批评都可以,谢谢!!!
怎么用c实现这个问题???
一篇文章,共有3行文字,每行有80个字符。要求分别统计其中英文大写字母、小写字母、数字、空格以及其他字符的个数。rn求c编程代码
用C语言实现哲学家进餐的问题
  设有5个哲学家,共享一张放油把椅子的桌子,每人分得一吧椅子.但是桌子上总共执友支筷子,在每个人两边分开各放一支.哲学家只有在肚子饥饿时才试图分两次从两边拾起筷子就餐.
用C语言实现说谎问题的推理题
题目介绍:A说B说谎,B说C说谎,C说A,B都在说谎,编程说明,谁在说谎话,谁在说真话解法:#include &amp;lt;stdio.h&amp;gt; //A说B说谎,B说C说谎,C说A,B都在说谎,编程说明,谁在说谎话,谁在说真话 int main(int argc, char *argv[]) { int a,b,c; for(a=0;a&amp;lt;=1;a++) { for(b=0;b&amp;lt;=...
用C语言实现数据结构问题 括号匹配
用C语言实现数据结构问题 括号匹配
用C语言实现约瑟夫环问题
用C语言实现的约瑟夫环问题,我是数据结构的课程设计,因此用的数据结构的知识,存储使用但循环链表实现的,当然数组也可以解决此问题
用c语言实现微分的问题!!!!
请问有什么办法实现微分和偏微分的程序??rn例如d(f(x))/dx d(f(x,y))/dxdy rn谢谢!!!!!
3n+1问题 用c语言实现
Consider the following algorithm to generate a sequence of numbers.Start with an integer n.If n is even, divide by 2.If n is odd, multiply by 3 and add 1.Repeat this process with the new value of n
最近点对问题用c语言实现
#include&amp;lt;stdio.h&amp;gt; #include&amp;lt;math.h&amp;gt; #include&amp;lt;stdlib.h&amp;gt; #define MAX 100 struct { int x,y; }zuobiao[MAX]; double zuijin() { int i,j,t; int d=100; int n=rand()%100+1; printf(...
用C语言实现舞伴配对问题
用C语言实现,关于跳舞时,男女搭配。用队列存储到场者的信息包括姓名及性别,然后根据性别分别配对出队,最后输出配对结果,以及未配对人员的数量和男女首等待者姓名。
用c语言实现josephus环问题
#include <stdio.h> #include <malloc.h> #define NULL 0 #include <iostream.h> typedef struct Lnode{ int data; struct Lnode *next; }joseph;
相关热词 c#入门推荐书 c# 解码海康数据流 c# xml的遍历循环 c# 取 查看源码没有的 c#解决高并发 委托 c#日期转化为字符串 c# 显示问号 c# 字典对象池 c#5.0 安装程序 c# 分页算法