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;}
2、输入两个非常大的整数(完全超出了int、long的表示范围),这个整数的长度可能超过100位,计算并输出这两个数相加的结果。(HDU acm 1002 用string处理比较好)
转载请标明出处,原文地址: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;}
3、“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
转载请标明出处,原文地址: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;}
4、输入n个数,按从小到大进行排序并输出。
转载请标明出处,原文地址: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;}
5、输入一个长整型的数,从低位起取出奇数位组成一个新的数输出。
转载请标明出处,原文地址: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;}
6、输入n个字符串,将它们按字母由小到大的顺序排列并输出。
方法一:
转载请标明出处,原文地址: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;}
7、输入两个正整数,求出这两个数的最大公约数。
转载请标明出处,原文地址: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;}
8、“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3,输入一个整数,判断它是否是水仙花数。
转载请标明出处,原文地址: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;}
9、完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6=1+2+3;28=1+2+4+7+14。输入一个整数,判断它是否是完数。
转载请标明出处,原文地址: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;}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow