URAL 1007 - Code Words
来源:互联网 发布:office2016 for mac卡 编辑:程序博客网 时间:2024/05/18 01:32
简单题,用一个数组存储从i到n共有多少个1,方便移位的时候计数
只有以下三种情况:
1、l=n如果满足条件,直接输出,否则需要修改1
2、l=n+1需要删除1或0
3、l=n-1需要添加1或0
这个题输入的时候要用scanf或cin(题目提示有空格或空行)
#include <stdio.h>#include <string.h>char s[1050];int d[1050],cnt;int main(){ int n; scanf("%d",&n); while(scanf("%s",s+1)==1) { int l=strlen(s+1); d[l+1]=0; cnt=0; for(int i=l;i>=1;i--) { d[i]=d[i+1]; if(s[i]=='1') { d[i]++; cnt+=i; } } if(l==n) { if(cnt%(n+1)==0)puts(s+1); else { for(int i=1;i<=l;i++) { if(s[i]=='1'&&((cnt-i)%(n+1)==0)) { s[i]='0'; puts(s+1);break; } } } } if(l-1==n) { for(int i=1;i<=l;i++) { if(s[i]=='1'&&((cnt-i-d[i+1])%(n+1)==0)) { for(int j=1;j<=l;j++) { if(j!=i)printf("%c",s[j]); }printf("\n"); break; } if(s[i]=='0'&&((cnt-d[i+1])%(n+1)==0)) { for(int j=1;j<=l;j++) { if(j!=i)printf("%c",s[j]); }printf("\n"); break; } } } if(l+1==n) { for(int i=1;i<=l+1;i++) { if((cnt+d[i])%(n+1)==0) { for(int j=1;j<i;j++) { printf("%c",s[j]); } printf("0"); for(int j=i;j<=l;j++) { printf("%c",s[j]); } printf("\n"); break; } if((cnt+i+d[i])%(n+1)==0) { for(int j=1;j<i;j++) { printf("%c",s[j]); } printf("1"); for(int j=i;j<=l;j++) { printf("%c",s[j]); } printf("\n"); break; } } } } return 0;}
- ural 1007 Code Words
- ural 1007 Code Words
- URAL 1007 - Code Words
- Ural 2045 Richness of words
- ural 1069. Prufer Code
- URAL 1069 Prufer Code
- ural 1069. Prufer Code
- URAL 1792 Hamming Code
- URAL 1792Hamming Code
- Ural 1313. Some Words about Sport
- URAL 2037 Richness of binary words
- Ural 2045 Richness of words(构造)
- Ural 2037 Richness of binary words
- URAL - 1792 Hamming Code(枚举)
- URAL 1792. Hamming Code (枚举)
- Code[VS] 3301 Square words
- code[vs]3301 Square words
- 【Google Code Jam】Reverse Words
- minSdkVersion、targetSdkVersion、targetApiLevel的区别
- linux cp 覆盖不提示
- Oracle—用户管理的完全恢复(一)
- android图片的内存优化 释放内存
- CV—目标跟踪资源整理
- URAL 1007 - Code Words
- ASP.Net中的MD5加密
- cell可以复用 元素加载cell上面只创建一次
- Git常用操作命令
- ORA-02049 超时分布式事务处理等待锁
- win7安装3dmax5
- __attribute__((unused))
- htmlentities 和htmlspecialchars 及解码
- HDUOJ 4638 2013多校第四场第7题 Group