算法day05

先决条件:
A:

fscanf函数原型为int fscanf(FILE*stream, constchar*format, [argument…]); 其功能为根据数据格式(format)从输入流(stream)中写入数据(argument);与fgets的差别在于:fscanf遇到空格和换行时结束,注意空格时也结束,fgets遇到空格不结束。

例子:

 /*1.从标准输入中读取一个整数*/
    if(fscanf(stdin, "%d",&i))

 /*2.从文件中读取数据*/
 /*FILE * stream */
        fscanf(stream,"%s",s);
        fscanf(stream,"%ld",&l);
        fscanf(stream,"%f",&fp);
        fscanf(stream,"%c",&c);

B:

assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。
例子:

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
int main( void )
{
       FILE *fp;

       fp = fopen( "test.txt", "w" );//以可写的方式打开一个文件,如果不存在就创建一个同名文件
       assert( fp );                           //所以这里不会出错
       fclose( fp );

       fp = fopen( "noexitfile.txt", "r" );//以只读的方式打开一个文件,如果不存在就打开文件失败
       assert( fp );                           //所以这里出错
       fclose( fp );                           //程序永远都执行不到这里来
       return 0;
}

1.POJ 2260,ZOJ 1949

Description
A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here’s a 4 x 4 matrix which has the parity property:
1 0 1 0

0 0 0 0

1 1 1 1

0 1 0 1

The sums of the rows are 2, 0, 4 and 2. The sums of the columns are 2, 2, 2 and 2.
Your job is to write a program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one bit. If this is not possible either, the matrix should be classified as corrupt.

Input
The input will contain one or more test cases. The first line of each test case contains one integer n (n<100), representing the size of the matrix. On the next n lines, there will be n integers per line. No other integers than 0 and 1 will occur in the matrix. Input will be terminated by a value of 0 for n.
Output
For each matrix in the input file, print one line. If the matrix already has the parity property, print “OK”. If the parity property can be established by changing one bit, print “Change bit (i,j)” where i is the row and j the column of the bit to be changed. Otherwise, print “Corrupt”.
Sample Input
4
1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1
4
1 0 1 0
0 0 1 0
1 1 1 1
0 1 0 1
4
1 0 1 0
0 1 1 0
1 1 1 1
0 1 0 1
0
Sample Output
OK
Change bit (2,3)
Corrupt

/* 
题目意思不难理解,就是要每行每列加起来都是偶数,就输出OK 
如果不是,通过改变其中的一个值能够达到其要求就输出改变那个坐标 
否则就输出Corrupt 

比如 
1 0 1 0 
0 0 1 0 
1 1 1 1 
0 1 0 1 

行之和数组:b : 2 1 4 2 
列之和数组:C :2 2 3 2 
所以改变的坐标是(2,3) 

注:下面的方法使用了文件操作和assert断言操作
*/  

#include <iostream>
#include <stdio.h>
//#include <assert.h>
#define MAXN 512
using namespace std;

int n;
int a[MAXN][MAXN],row[MAXN],col[MAXN];
FILE *input;

int read_case()     //输入矩阵
{
    int i,j;
    fscanf(input,"%d",&n); //输入大小
    if(n==0) return 0;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        fscanf(input,"%d",&a[i][j]);
    return 1;
}

void solve_case() //判断矩阵的奇偶均势性
{
    int cc,cr,i,j,k;
    for(i=0;i<n;i++)    //初始时各行各列的数的总和为0
        row[i]=col[i]=0;
    for(i=0;i<n;i++)    //用数组形式 统计各行各列的数的总和
        for(j=0;j<n;j++)
    {
        row[i]=row[i]+a[i][j];
        col[j]=col[j]+a[i][j];
    }

    cr=cc=0;
    for(k=0;k<n;k++)       //累计数和为奇数的行数cr并记下最后数和为奇数的行序号i;列数同理
    {
        if(row[k]&1) {cr++;i=k;}
        if(col[k]&1) {cc++;j=k;}
    }
    if(cc==0 && cr==0) printf("OK\n"); //若所有行和列的数和都为偶数,则输出"OK"
    else if (cc==1 && cr==1) printf("Change bit(%d,%d)\n",i+1,j+1);//若仅有1行1列的数和为奇数,则可通过(i+1,j+1)取反回复奇偶均势性特性,否则”Corrupt“
        else printf("Corrupt\n");
}

int main()
{
    int n;
    input = fopen("error.in","w");
    //assert(input!=NULL);
    while(read_case()) solve_case();
    fclose(input);
    return 0;
}

改进精简版

#include <iostream>
#include <stdio.h>
#define MAXN 512
using namespace std;

int a[MAXN][MAXN],row[MAXN],col[MAXN];

int main()
{
    int i,j,n;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);

    int cc,cr,k;
    for(i=0;i<n;i++)    //初始时各行各列的数的总和为0
        row[i]=col[i]=0;
    for(i=0;i<n;i++)    //用数组形式 统计各行各列的数的总和
        for(j=0;j<n;j++)
    {
        row[i]=row[i]+a[i][j];
        col[j]=col[j]+a[i][j];
    }

    cr=cc=0;
    for(k=0;k<n;k++)       //累计数和为奇数的行数cr并记下最后数和为奇数的行序号i;列数同理
    {
        if(row[k]&1) {cr++;i=k;}
        if(col[k]&1) {cc++;j=k;}
    }
    if(cc==0 && cr==0) printf("OK\n"); //若所有行和列的数和都为偶数,则输出"OK"
    else if (cc==1 && cr==1) printf("Change bit(%d,%d)\n",i+1,j+1);//若仅有1行1列的数和为奇数,则可通过(i+1,j+1)取反回复奇偶均势性特性,否则”Crrupt“
        else printf("Corrupt\n");


    return 0;
}

体会:
1.文件操作需要加强,fsanf(input,”%d”,&n);
2.判断奇偶性的一种方法是“&1”非常方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值