输入包括多组数据,每组数据包含一个 skew 数。
对应每一组数据,输出相应的十进制形式。结果不超过 2^31-1。
10120 200000000000000000000000000000 10 1000000000000000000000000000000 11 100 11111000001110000101101102000
44 2147483646 3 2147483647 4 7 1041110737
#include<stdio.h> #include<string.h> int main (){//the shorter,the better. int i,len,c,n;char str[64]; for(;fgets(str,64,stdin)!=NULL;printf("%d\n",n)) for(n=i=0,len=strlen(str)-1;i<len&&str[i]!='\n';n+=(str[i]-'0')*((1<<len-i)-1),i++); }
#include <iostream> #include <string> #include <cmath> using namespace std; int main() { string str; while (cin >> str) { int skew = 0; for (int i = 0, j = str.size(); i < str.size(); i++, j--) { skew += (str[i] - '0') * (pow(2, j) - 1); } cout << skew << endl; } return 0; }
//shew 数 #include<iostream> #include<cmath> #include<cstring> using namespace std; int shew(int m,int x) { if(x==0){ return 0; } else { //cout<<x<<endl; return x*(pow(2,m)-1); } } int main() { string s; while(cin>>s) { int L=s.size(); int sum=0; for(int i=0;i<s.size();i++){ sum+=shew(L,s[i]-'0'); L--; } cout<<sum<<endl; } return 0; }
#include<iostream> #include<string> #include<math.h> using namespace std; int main(){ string input; long ans; int len; while(cin >> input) { ans = 0; len = input.size(); for(int i = 0; i < len; i ++) { if(input[i] != '0') ans += (input[i] - '0') * (pow(2, len - i) - 1); } cout << ans << endl; } }
# include<stdio.h> # include<string.h> # include<math.h> int skew(char str[]) { int len=strlen(str); int i; int sum=0; for(i=0;i<len;i++) { int num=str[i]-'0'; sum=sum+num*(pow(2,len-i)-1); } return sum; } int main() { char str[200]; while(scanf("%s",str)!=EOF) { printf("%d\n",skew(str)); } return 0; }
importjava.util.Arrays;importjava.util.Comparator;importjava.util.Scanner;publicclassMain {publicstaticvoidmain(String[] args) {Scanner in = newScanner(System.in);while(in.hasNext()) {String x = in.next();System.out.println(compute(x));}}publicstaticintcompute(String x) {intans = 0;intratio = 2;for(inti = x.length()-1; i >= 0; i--) {intnum = x.charAt(i) - '0';ans += num * (ratio - 1);ratio <<= 1;}returnans;}}
#include <iostream> #include<string> using namespace std; int main() { string str; int sum,r; while(cin>>str){ sum=0; r=2;//基数 for(int i=str.size()-1;i>=0;--i){ sum+= (str[i]-'0')*(r-1);//套公式 r*=2;//每次乘以2 } cout<<sum<<endl; } return 0; }
#include <iostream> #include <cmath> #include <string> using namespace std; int main(){ string str; //字符串存大数 while(cin >> str ){ int k=str.size(); int res=0; for(int i=0; i<str.size(); ++i){ res+=(str[i]-'0')*(pow(2,k)-1); //善用-'a'或'0' --k; } cout << res << endl; } }
#include <stdio.h> #include <math.h> #include <string.h> int main() { int skew, sum, i, t, len; char skew_s[32]; while(scanf("%s", skew_s) != EOF){ len = strlen(skew_s); i = 1; sum = 0; for(int j = len - 1; j >= 0; j--) sum += (skew_s[j] - '0') * (pow(2, i++) - 1); printf("%d\n", sum); }//直接用数值不行,会变成-1 return 0; }
#include<iostream> #include<cmath> #include<string> using namespace std; int main(){ string str; while(cin >> str){ int sum = 0; for(int i = str.size(), j = 0; j < str.size(); --i, ++j){ if(str[j] != '0') sum += (str[j] - '0')*( pow( (float)2, (float)i) - 1);//(float)2导致结果出错,溢出 } cout << sum << endl; } return 0; }哪位老铁能够解答下,pow函数中如果 pow( (float)2, (float)i) - 1,结果会出现错误,我试了下原因在于常数2前加了强制类型装换,如果去掉(float)就正确了,谢谢大神指教
#include<bits/stdc++.h> using namespace std; int mySkew(string n); int main(){ string n; int re; while(cin>>n){ re = mySkew(n); cout<<re<<endl; } return 0; } int mySkew(string n){ int re; int i; for(i=0;i<n.length();++i){ re += (n[i]-'0')*(pow(2,n.length()-i)-1); } return re; }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ String s = scanner.nextLine(); int sum=0; for (int i = 0,len=s.length(); i < len ; i++) { sum+= (s.charAt(i)-'0')*(Math.pow(2,len-i)-1); } System.out.println(sum); } } }
while True: try: print(sum([int(digit) * (2 ** (index + 1) - 1) for index, digit in enumerate(input()[::-1])])) except: break;看到前面有人装*用两行,我决定也装个*,一行实现😄