| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 654 人关注过本帖
标题:求大神帮我看下代码哪里出错了!
只看楼主 加入收藏
mengt2012
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2014-8-8
收藏
 问题点数:0 回复次数:6 
求大神帮我看下代码哪里出错了!
程序代码:
#include <stdio.h>
int main(void)
{
    int r, c;
    scanf("%d %d", &r, &c);
    int cake[r][c];
    int i, j, cnt=0, cou=0, a=0, b=0, cell=0;
    getchar();    //抵消一个回车符 
    for( i=0; i<r; i++ ){
        for( j=0; j<c; j++ ){
            scanf("%c", &cake[i][j]);
        }
    getchar();

    }
//    printf("%c\n", cake[0][0]);
//        printf("%c\n", cake[0][1]);
//            printf("%c\n", cake[0][2]);
//                printf("%c\n", cake[0][3]);
//                    printf("%c\n", cake[2][2]);
    for( i=0; i<r; i++ ){
        for( j=0; j<c; j++ ){
            cnt++;
            if( cake[i][j] == 'S' ){
                cnt = 0;
                break;
            }
        }
        if( cnt>0 ){
            a++;
        }
    }
    for( j=0; j<c; j++ ){
        for( i=0; i<r; i++ ){
            cou++;
            if( cake[i][j] == 'S' ){
                cou = 0;
                break;
            }
        }
        if( cou>0 ){
            b++;
        }
    }
    cell = cnt + cou - a*b;
    printf("%d", cell);
    return 0;
}

输入:   3 4
         S...
         ....
         ..S.
为什么if( cake[i][j] == 'S' )会出错呢?没有一个是相等的!
2014-10-28 14:36
mengt2012
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2014-8-8
收藏
得分:0 
真的不知道为什么错了!

大神在哪?55555
2014-10-28 15:29
happy两棵树
Rank: 2
等 级:论坛游民
帖 子:28
专家分:62
注 册:2013-7-18
收藏
得分:0 
你这个代码想实现什么功能? 说的详细些啊
2014-10-28 16:33
mengt2012
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2014-8-8
收藏
得分:0 
回复 3 楼 happy 两棵树
You are given a rectangular cake, represented as an r × c grid. Each cell either has an evil strawberry, or is empty. For example, a 3 × 4 cake may look as follows:


The cakeminator is going to eat the cake! Each time he eats, he chooses a row or a column that does not contain any evil strawberries and contains at least one cake cell that has not been eaten before, and eats all the cake cells there. He may decide to eat any number of times.

Please output the maximum number of cake cells that the cakeminator can eat.

Input
The first line contains two integers r and c (2 ≤ r, c ≤ 10), denoting the number of rows and the number of columns of the cake. The next r lines each contains c characters — the j-th character of the i-th line denotes the content of the cell at row i and column j, and is either one of these:

'.' character denotes a cake cell with no evil strawberry;
'S' character denotes a cake cell with an evil strawberry.
Output
Output the maximum number of cake cells that the cakeminator can eat.

Sample Input
Input
3 4
S...
....
..S.
Output
8
2014-10-28 16:45
mengt2012
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2014-8-8
收藏
得分:0 
没有人在吗……
2014-10-28 19:08
卖身契
Rank: 1
等 级:新手上路
帖 子:8
专家分:5
注 册:2013-12-20
收藏
得分:0 
不能这样声明数组。。。int cake[r][c]中的r, c不能由你输入数字来决定,,而应该是一个确定好的值。即一个常量。。如果要声明动态数组应该用malloc函数
2014-10-28 20:35
mengt2012
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2014-8-8
收藏
得分:0 
回复 6 楼 卖身契
不是这个错误,C99已经允许这样做了。

不过我已经发现错误了。
这是正确的代码:
程序代码:
#include <stdio.h>
int main(void)
{
    int r, c;
    scanf("%d %d", &r, &c);
    char cake[r][c];
    int i, j, cnt=0, cou=0, a=0, b=0, cell=0;
    getchar();    //抵消一个回车符 
    for( i=0; i<r; i++ ){
        for( j=0; j<c; j++ ){
            scanf("%c", &cake[i][j]);
        }
    getchar();

    }
//    printf("%c\n", cake[0][0]);
//        printf("%c\n", cake[0][1]);
//            printf("%c\n", cake[0][2]);
//                printf("%c\n", cake[0][3]);
//                    printf("%c\n", cake[2][2]);
    int cnt1=0,cou1=0;
    for( i=0; i<r; i++ ){
        for( j=0; j<c; j++ ){
            cnt++;
            if( cake[i][j] == 'S' ){
                cnt = 0;
                break;
            }
        }
        if( cnt>0 ){
            cnt1 += cnt;
            cnt = 0;
            a++;
        }
    }
    for( j=0; j<c; j++ ){
        for( i=0; i<r; i++ ){
            cou++;
            if( cake[i][j] == 'S' ){
                cou = 0;
                break;
            }
        }
        if( cou>0 ){
            cou1 += cou;
            cou = 0;
            b++;
        }
    }
    cell = cnt1 + cou1 - a*b;
    printf("%d", cell);
    return 0;
}
2014-10-28 20:43
快速回复:求大神帮我看下代码哪里出错了!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013124 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved