1.[a,b]的奇数之和
[1,2k-1]的奇数之和:k^2
((b+1)/2)((b+1)/2)-(a/2) (a/2)
2.[a,b]的偶数之和:
[1,2k]的偶数和
k+k^2
:(b/2-(a-1)/2)+(b/2)(b/2)-((a-1)/2) ((a-1)/2)
3.八进制十进制小数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <iostream> #include <cstring> using namespace std;const int N = 10010 ;char d[N] ; int ans[N] ; int main () { while (cin>>d){ memset (ans,0 ,sizeof (ans)); int d2; int len = strlen (d); int t = 0 ; for (int i = len - 1 ; i > 1 ; i--){ d2 = d[i] - '0' ; int k = 0 , j = 0 ; while (j<t || d2){ d2 = d2*10 + ans[j++]; ans[k++] = d2/8 ; d2 %= 8 ; } t = k; } cout<<d<<" [8] = 0." ; for (int i = 0 ; i < t ; i++) cout<<ans[i]; cout<<" [10]" <<endl; } return 0 ; }
十进制转R进制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <stdio.h> #include <stdlib.h> char digits[] = "0123456789ABCDEF" ;#define N 32 char ans[N + 2 ];int main () { int n, r; while (~scanf ("%d%d" , &n, &r)) { if (r==0 )break ; if (n < 0 ) {printf ("-" ); n = -n;} int i = 0 ; while (n) ans[i++] = digits[n % r], n /= r; if (i == 0 ) ans[i++] = '0' ; while (--i >= 0 ) putchar (ans[i]); putchar ('\n' ); } return 0 ; }
A转B
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 #include <iostream> #include <string> #include <ctype.h> using namespace std ; string convert (int a, string & s, int b) { long val, dcount, digit; char result[72 ], c; string ans; val = 0 ; for (int i=0 ; i<(int )s.size(); i++) { if (isdigit (s[i])) val = val * a + s[i] - '0' ; else val = val * a + toupper (s[i]) - 'A' + 10 ; } dcount = 0 ; while (val) { digit = val % b; val /= b; result[dcount++] = ((digit >= 10 ) ? 'A' - 10 : '0' ) + digit; } if (dcount == 0 ) { result[dcount++] = '0' ; result[dcount] = '\0' ; } else result[dcount] = '\0' ; for (int i=0 , j=dcount-1 ; i<j; i++, j--) { c = result[i]; result[i] = result[j]; result[j] = c; } ans = result; return ans; } int main () { int a, b; string s; cin >> a >> s >> b; cout << convert(a, s, b) << endl ; return 0 ; }
2转16位数爆longlong
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <stdio.h> #include <string.h> #define N 10000 #define N2 4 char s[N + N2 + 1 ]; char convert[] = "0123456789ABCDEF" ; int main (void ){ int n, len, digits, i, k; scanf("%d" , &n); getchar(); while (n--) { gets(s + N2); s[0 ] = s[1 ] = s[2 ] = s[3 ] = '0' ; len = strlen(s + N2); digits = (len + N2 - 1 ) / N2; len += N2 - 1 ; for (i=1 , k=len; i<=digits; i++) { s[k--] = convert[(s[len - 3 ] - '0' ) * 8 + (s[len - 2 ] - '0' ) * 4 + (s[len - 1 ] - '0' ) * 2 + (s[len] - '0' )]; len -= N2; } printf("%s\n" , &s[k + 1 ]); } return 0 ; }
4.大小写转换
1 2 3 4 5 6 7 8 大写变小写、小写变大写 : 字符 ^= 32 ; 大写变小写、小写变小写 : 字符 |= 32 ; 小写变大写、大写变大写 : 字符 &= -33 ; a-z:97 -122 A-Z:65 -90 0 -9 :48 -57
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int t = x; int value = 0 ; while (x / 10 ){ value = 10 * value + x % 10 ; x /= 10 ; } if (value*10 + x == t) return 1 ; else return 0 ; }判断回文数字 #include <iostream> using namespace std; int n,a[40 ][40 ],x,y; int main () { cin>>n; x=1 ,y=(n+1 )/2 ; for (int i=1 ;i<=n*n;i++){ a[x][y]=i; if (!a[(x-2 +n)%n+1 ][y%n+1 ]) x=(x-2 +n)%n+1 ,y=y%n+1 ; else x=x%n+1 ; } for (int i=1 ;i<=n;i++){ for (int j=1 ;j<=n;j++){cout<<a[i][j]<<' ' ;} cout<<endl;} }