AuAu


私信TA

用户名:Alanwjs

访问量:5882

签 名:

上面那位兄弟,你的头被脑袋踢了

等  级
排  名 1462
经  验 2755
参赛次数 3
文章发表 10
年  龄 0
在职情况 学生
学  校 烟台大学
专  业 软件工程

  自我简介:

解题思路:
原码转化为补码,分两种情况:1.正数不变2.负数原码符号位不变,其余位取反加一

难点:取反加一

好好观察代码吧,注释挺清楚的,不懂得欢迎评论区留言哦~
参考代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
void move(char s[])
{
	int len =strlen(s);
	char c=s[0];
	if(c=='1')
	{
		for(int i=1;i<len;i++)//除符号位 其余取反 
		{
			if(s[i]=='0') s[i]='1';
			else s[i]='0';
		}
		int t=0;
		for(int i=len-1;i>=1;i--)//+1操作 
		{
			if(i==len-1) s[i]+=1;//1
			else s[i]+=t;//2 1+2  皆为不管怎样 先加上进位的数再说 
			if(s[i]>'1') {//此为特判弥补错误 改此位为'0' 进位为1 
				s[i]='0';
				t=1;
			}
		}
	}
	return;
}
int main() {
	char s[10000];
	gets(s);
	move(s);
	puts(s);
	return 0;
}

欢迎留言评论,记得留下你的小心心哦~~~

 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

欢迎评论哦
2018-03-22 19:53:08
  • «
  • 1
  • »