标签:size 基础 main stdio.h ack return 自己 algorithm count
字符串基础用法题,包含有大数减法大数除法模板,不难理解,代码如下:
#include<stdio.h> #include<string.h> #include<string> #include<math.h> #include<time.h> #include<iostream> #include<ctype.h> #include<map> #include<set> #include<algorithm> #include<stdlib.h> #include<queue> #include<stack> using namespace std; string a,b,c; string sub(string a, string b) { string c; bool ok = 0; int len1 = a.length(); int len2 = b.length(); int len = max(len1, len2); for(int i = len1; i < len; i++)//短的串前边加0方便后边计算 a = "0" + a; for(int i = len2; i < len; i++) b = "0" + b; if(a < b)//判断正负 { string temp = a; a = b; b = temp; ok = 1; } for(int i = len - 1; i >= 0; i--) { if(a[i] < b[i]) { a[i - 1] -= 1; a[i] += 10; } char temp = a[i] - b[i] + ‘0‘; c = temp + c;//此处不能写为c+temp因为是倒着算,需要正着存,顺序写错就不对了,自己可以试几组样例 } int pos = 0; while(c[pos] == ‘0‘ && pos < len) pos++;//去除前导0 if(pos == len) return "0"; if(ok) return "-" + c.substr(pos); return c.substr(pos); } string chufa(string s,int x) { int jj=0,fla=0; string ans=""; for(int i=0; i<s.size(); i++) { jj=jj*10+s[i]-‘0‘; if(jj>=x) { fla=1; ans+=jj/x+‘0‘; jj%=x; } else { if(fla==1)//在运算过程中不够除商0; ans+=‘0‘; } } return ans; } int main() { int n,i,j; while(cin>>a) { set<char>qq; b=a; reverse(a.begin(),a.end()); c=sub(b,a); if(c[0]==‘-‘)//题目不要求正负,可以删除再计算 c.erase(0,1); if(c.size()==1) { printf("YES\n"); continue; } string ans=chufa(c,9); for(int i=0; i<ans.size(); i++) { qq.insert(ans[i]); } if(qq.size()==1) printf("YES\n"); else printf("NO\n"); } }
标签:size 基础 main stdio.h ack return 自己 algorithm count
原文地址:https://www.cnblogs.com/nr1999/p/8946628.html