本节目录
- 1、KY15 abc
- 2、KY45 skew数
- 3、KY59 神奇的口袋
- 4、KY72 Digital Roots
- 5、KY115 后缀子串排序
- 6、KY132 xxx定律 3n+1思想
- 7、KY168 字符串内排序
1、KY15 abc
#include<stdio.h>
int main()
{
int a,b,c;
for(a=1;a<=9;a++)
for(b=1;b<=9;b++)
for(c=0;c<=9;c++)
{
int x=a*100 + b*10 + c;
int y=b*100 + c*10 + c;
if (x+y ==532)
printf("%d %d %d\n",a,b,c);
}
return 0;
}
2、KY45 skew数
#include<stdio.h>
#include<string.h>
#include<math.h>
#define MAX 32
int main()
{
char s[MAX];
while(scanf("%s",s)!=EOF)
{
long sum = 0;
int n = strlen(s);
for (int i=0;i<n;i++)
{
sum = sum + (s[i]- '0')*(pow(2,n-i) - 1);
}
printf("%ld\n",sum);
}
return 0;
}
3、KY59 神奇的口袋
4、KY72 Digital Roots
简单翻译一下这个题的思路:就是找根,看懂英文是关键。
#include<stdio.h>
int zhengchu(int num){
int sum=0;
while(num){
sum = sum+ num%10;
num = num / 10;
}
return sum;
}
int main()
{
int num;
while(scanf("%d",&num)!= EOF)
{
while(num>=10)
num = zhengchu(num);
printf("%d\n",num);
}
}
5、KY115 后缀子串排序
STL版本:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string str;
cin >> str;
vector<string> toSort;
for (int i = 0; i < str.size(); ++i) //分割子串
toSort.push_back(str.substr(i));
sort(toSort.begin(), toSort.end()); //默认字典序排序
for (int i = 0; i < toSort.size(); ++i)
cout << toSort[i] << endl;
return 0;
}
自写CMP算法:
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAX 1024
string suffix[MAX];
//按字典顺序排序
bool CMP(string a,string b) {
bool ret =true;
int index = 0;
while (index < a.size() && index < b.size()) {
if (a[index] != b[index]) {
ret = (a[index] < b[index]);
break;
}
index++;
}
return ret;
}
int main() {
string s; cin >> s;
//suffix[0] = s;
//int n = s.size();
for (int i = 0;i<s.size(); i++) {
suffix[i] = s.substr(i, s.size()-i);
}
sort(suffix, suffix + s.size(), CMP);
for (int i = 0; i < s.size(); i++) {
cout << suffix[i] << endl;
}
return 0;
}
6、KY132 xxx定律 3n+1思想
#include <stdio.h>
#include <stdlib.h>
int nums(int n){
if(n==1)
return 0;
if(n%2==0){
return nums(n/2)+1;
}else{
return nums((n*3+1)/2)+1;
}
}
int main(){
int n;
while(scanf("%d\n",&n)!=EOF){
int sum=0;
sum=nums(n);
printf("%d\n",sum);
}
return 0;
}
7、KY168 字符串内排序
//用内置函数
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string str;
while (cin >> str) {
sort(str.begin(), str.end());
cout << str << endl;
}
return 0;
}
//简单选择排序,将
#include<stdio.h>
#include<string.h>
int main(){
char string[200];
char str;
int i,j,k,len;
scanf("%s",string);
len=strlen(string);
for(i=0;i<len-1;i++){
k=i;
for(j=i+1;j<len;j++){
if(string[j] < string[k]){
k=j;
}
}
if(i!=k){
str = string[i];
string[i]=string[k];
string[k] = str;
}
}
printf("%s",string);
return 0;
}