1、输入一个十进制的数,把它转成八进制。类似的把十进制转成16进制,把十六进制转变为十进制等。
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> int main(void) { int num, a[100], i, j; i = 0; scanf("%d" , &num); while(num != 0) { a[i++] = num % 8; num /= 8; } if(i == 0) printf("0\n"); else { for(j = i - 1; j >= 0; --j) printf("%d", a[j]); printf("\n"); } return 0; }
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> #include <string.h> int main(void) { int temp, i, j, k, len1, len2, m, sum[1010]; char a[1010], b[1010], c[1010]; scanf("%s %s", a, b); len1 = strlen(a); len2 = strlen(b); if(len1 < len2) //交换两个字符串,确保第一个字符串的长度不小于第二个字符串 { strcpy(c, b); strcpy(b, a); strcpy(a, c); m = len2, len2 = len1, len1 = m; } k = m = 0; //从两个数的低位开始,手工模拟加法,逐位相加 for(i = len1 - 1, j = len2 - 1; i >= 0; --i, --j) { if(j >= 0) temp = a[i] - '0' + b[j] - '0' + m; else temp = a[i] - '0' + m; sum[k++] = temp % 10; m = temp / 10; //进位 } if(m > 0) printf("%d", m); for(i = k - 1; i >= 0; --i) printf("%d", sum[i]); printf("\n"); return 0; }
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> #include <string.h> int main(void) { char str[100]; int len, i, j; scanf("%s", &str); len = strlen(str); for(i = 0, j = len - 1; i < len && j >= i; ++i, --j) { if(str[i] != str[j]) break; } if(j < i) printf("这个字符串是回文串!\n"); else printf("这个字符串不是回文串!\n"); return 0; }
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <iostream> #include <algorithm> using namespace std; int main(void) { int n, i; scanf("%d", &n); int *p = new int[n]; for(i = 0; i < n; ++i) scanf("%d", &p[i]); sort(p, p + n); for(i = 0; i < n; ++i) printf("%d ", p[i]); printf("\n"); return 0; }
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> int main(void) { long n, k, i, j, newnum; i = j = newnum = 0; scanf("%ld", &n); while(n) { k = n % 10; for(j = 0; j < i; ++j) k *= 10; newnum += k; n /= 100; i++; //每位数字上数字的权值 } printf("%ld\n", newnum); return 0; }
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <iostream> #include <string> #include <algorithm> using namespace std; bool cmp(const string& a, const string& b) { return a < b; } int main(void) { int i, n; cin>>n; string *p = new string[n]; for(i = 0; i < n; ++i) cin>>p[i]; sort(p, p + n, cmp); for(i = 0; i < n; ++i) cout<<p[i]<<endl; delete []p; return 0; }方法二:
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <iostream> #include <string> using namespace std; int main(void) { int n, i, j; string temp; cin>>n; string *pt = new string[n]; for(i = 0; i < n; ++i) cin>>pt[i]; for(i = 0; i < n - 1; ++i) { for(j = 0; j < n - i - 1; ++j) { if(pt[j] > pt[j + 1]) { temp = pt[j]; pt[j] = pt[j + 1]; pt[j + 1] = temp; } } } for(i = 0; i < n; ++i) cout<<pt[i]<<endl; delete []pt; return 0; }
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> int gcd(int m, int n) { int r; while(r = m % n) { m = n, n = r; } return n; } int main(void) { int m, n; scanf("%d %d", &m, &n); printf("最大公约数为:%d\n", gcd(m, n)); return 0; }
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> int main(void) { int n, a, b, c; scanf("%d", &n); a = n % 10; b = (n / 10) % 10; c = n / 100; if(n == a*a*a + b*b*b + c*c*c) printf("这个数是水仙花数!\n"); else printf("这个数不是水仙花数!\n"); return 0; }
转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6667026 #include <stdio.h> int main(void) { int i, m, n; scanf("%d", &n); for(i = 1, m = 0; i <= n/2; ++i) { if(n % i == 0) m += i; } if(m == n) printf("这个数是完数!\n"); else printf("这个数不是完数!\n"); return 0; }