[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
【问题描述】
有n(2<=n<=20)块芯片,已知好芯片比坏芯片多。
每个芯片都能用来测试其他芯片。用好芯片测试其他芯片时,能正确给出被测芯片是好还是坏。而用坏芯片测试其他芯片时,会随机给出好还是坏的测试结果(即此结果与被测试芯片实际的好坏无关)。
给出所有芯片的测试结果,问哪些芯片是好芯片。
【输入格式】
输入数据第一行为一个整数n,表示芯片个数。
第二行到第n+1行为n*n的一张表,每行n个数据。表中的每个数据为0或1,在这n行中的第i行第j列(1<=i,j<=n)的数据表示用第i块芯片测试第j块芯片时得到的测试结果,1表示好,0表示坏,i=j时一律为1(并不表示该芯片对本身的测试结果。芯片不能对本身进行测试)。
【输出格式】
按从小到的顺序输出所有好芯片的编号。
【样例输入】
3
1 0 1
0 1 0
1 0 1
【样例输出】
1 3
【思路】
本题的逻辑就是先找出出现最多次数的一维数组。无论是奇数还是偶数,好芯片出现的最少次数都应该是n/2+1.找到之后,再输出这些相同的一维数组下标+1.
逻辑还是比较简单的,但是实现起来还是比较繁琐的。
可以看代码注释。
#include<stdio.h> int main(){ int n,i,j,N[20][20]; scanf("%d",&n);
for(i=0;i<n;i++){ //先输入 for(j=0;j<n;j++){ scanf("%d",&N[i][j]); } } int num;
//用来检测该芯片是好芯片的变量 for(i=0;i<n;i++){ //从前往后依次检测,出现好芯片就终止循环 num=1;
for(j=0;j<n;j++){ //和其余的检测数据进行对比 if(j==i){ continue; } int sign=0,x;
for(x=0;x<n;x++){ //对比的具体代码 if(N[i][x]!=N[j][x]){ break; } } if(x==n){ num++;
//数据相同,则加一 } } if(num>=(n/2+1)){ //符合好芯片的条件 printf("%d",i+1); for(j=0;j<n;j++){
if(j==i){ continue; } int sign=0,x; for(x=0;x<n;x++){ //输出好芯片的编号
if(N[i][x]!=N[j][x]){ break; } } if(x==n){ printf(" %d",j+1); } } break; } }
//这种输出方式可以在最后不会多输出空格 return 0; }