1. 判断是不是IP地址
2. 统计字符串里数字字母空格和其他字符个数四种字符统计。
3. 整数转二进制
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n, a, b, c, d;
scanf("%d", &n);
while (n--){
scanf("%d.%d.%d.%d", &a, &b, &c, &d);
if (a < 0 || b < 0 || c < 0 || d < 0 || a > 255 || b > 255 || c > 255 || d > 255)
printf("No!\n");
else
printf("Yes!\n");
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
char c;
int letters = 0, space = 0, digit = 0, others = 0;
while ((c = getchar())!= '\n')
{
if (c >= 'a'&&c <= 'z' || c >= 'A'&&c <= 'Z')
letters++;
else if (c == ' ')
space++;
else if (c >= '0'&&c <= '9')
digit++;
else
others++;
printf("char=%d space=%d digit=%d others=%d\n", letters, space, digit, others);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int num ;
cin >> num;
while(num)
{
cout << (num&1);
num = num >> 1;//把num转换成二进制表示后所有位向后移动一位,高位补0
}
cout <<endl;
system("pause");
return 0;
}
1.判断输入的字符串是否全是数字
2.3x3矩阵转置
3.判断字符串是否可以由其子串组成
#include <iostream>
#include <sstream>
using namespace std;
bool isNum(string str)
{
stringstream sin(str);
double d;
char c;
if (!(sin >> d))
{
return false;
}
if (sin >> c)
{
return false;
}
return true;
}
int main(void)
{
string str1,str2;
cin >> str1;
if (isNum(str1))
{
cout << "str1 is a num" << endl;
}
else
{
cout << "str1 is not a num" << endl;
}
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int matrix[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> matrix[i][j];
printf("原矩阵:\n");
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++)
cout << matrix[i][j] << " ";
cout << endl;
}
printf("\n转置后矩阵:\n");
for (int i = 0; i<3; i++){
for (int j = 0; j<3; j++)
cout << matrix[j][i] << " ";
cout << endl;
}
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
bool repeated(string s)
{
int n = s.size();
string temp1 = "";
string temp2 = "";
for (int i = 2; i <= n; ++i)
{
if (n % i == 0)
{
int length = n / i;
temp1 = string(s.begin(), s.begin() + length);
for (int j = 0; j < i; ++j)
temp2 += temp1;
}
if (temp2 == s)
cout << temp1 << endl;
return true;
temp2 = "";
}
return false;
}
};
int main()
{
string str;
cin >> str;
cout << Solution().repeated(str) << endl;
system("pause");
return 0;
}
1、给一组数,问奇数多还是偶数多
2、一串字符,第一个出现一次的字符
3、最长回文子串长度
#include<iostream>
#include<stdio.h>
using namespace std;
int WhichMore(int x[], int n)
{
int j = 0, o = 0;
for (int i = 0; i<n; i++)
if (x[i] % 2 == 0)
o++;
else
j++;
return o - j;
}
int main()
{
int x[100], i = 0, j,n;
cin >> n;
while (n--){
cin >> j;
x[i++] = j;
}
j = WhichMore(x, i);
if (j >= 0)
printf("偶数比奇数多%d个\n", j);
else
printf("奇数比偶数多%d个\n", -j);
system("pause");
return 0;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
int main()
{
int n, l, sum = 0, i;
char a[100000], ch;
int sign = 1;
scanf("%d\n", &n);
while (n--)
{
gets(a);
l = strlen(a);
for (ch = 'a'; ch <= 'z'; ch++)
{
for (i = 0; i<l; i++)
{
if (ch == a[i])
sum++;
}
if (sum == 1)
{
sign = 0;
break;
}
sum = 0; //注意,这里还要局部归零!因为下面的循环都要用sum
}
if (sign == 1)
printf("no\n");
else
printf("%c\n", ch);
sum = 0;
sign = 1;
}
system("pause");
return 0;
}
#include<iostream>
#include<vector>
#include <algorithm>
#include<string>
using namespace std;
class Solution
{
public:
string longestPalindrome(string s)
{
if (s.length() < 0)
return "";//如果长度小于0则返回空
int n = s.size();//获得字符串的长度
vector<vector<bool>> dp(n, vector<bool>(n, false));//定义bool向量
int maxlen = 1, start = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
if (i - j < 2) dp[i][j] = (s[i] == s[j]);
else
dp[i][j] = (s[i] == s[j] && dp[i - 1][j + 1] == 1);
if (dp[i][j] && maxlen < i - j + 1)
{
maxlen = i - j + 1;
start = j;
}
}
}
return s.substr(start, maxlen);
}
};
int main()
{
string line;
cin >> line;
string ret = Solution().longestPalindrome(line);
string out = (ret);
cout << out << endl;
system("pause");
return 0;
}
1.给一个数组,一个target值,求数组中俩元素和为target的俩下标
2.输入一个long类型数字,如365141,输出3-6-5-1-4-1
3.百钱买百鸡的小学问题
#include<iostream>
#include<vector>
#include <unordered_map>
using namespace std;
//构建解决方案
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
unordered_map<int, int>m;//因此遇到查找问题,常会考虑一下用unordered_map构造空的容器
vector<int>result;//定义输出结果向量
for (int i = 0; i< nums.size(); i++)///* 遍历数组 */
{
if (m.find(nums[i]) == m.end())//若这个元素在m中,则判断当前的位置i与查找到的nums[i],得到其位置
{
m[target - nums[i]] = i;
}
else
{
result.push_back(m[nums[i]]);//获得下标值
result.push_back(i);
break;
}
}
return result;
}
};
int main()
{
int target;
vector<int>nums;
cin >> target;
int x;
while (cin >> x)
{
nums.push_back(x);
if (getchar() == '\n') //遇回车结束
break;
}
vector<int> ret = Solution().twoSum(nums, target);
cout << ret[0]<<" " << ret[1] << endl;
system("pause");
return 0;
}
#include<iostream>
#include <stdio.h>
void main()
{
long a;
int b[50], n, i;
scanf("%ld", &a);
i = 0;
while (a>9)
{
b[i] = a % 10;
a /= 10;
i++;
}
b[i] = a;
for (; i >= 0; i--)
{
printf("%d", b[i]);
if (i != 0)
printf("-");
}
printf("\n");
system("pause");
}
#include<iostream>
using namespace std;
int main()
{
int x, y, z; //分别代表公鸡,母鸡,小鸡的数量
for (x = 0; x <= 20; x++)
{
for (y = 0; y <= 33; y++)
{
z = 100 - x - y;
if (z % 3 == 0 && (5 * x + 3 * y + z / 3) == 100)
{
cout << "公鸡,母鸡,小鸡数:" << x << " " << y << " " << z << " " << endl;
}
}
}
system("pause");
return 0;
}