字符串分隔
连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组;
长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
输入:
abc
123456789
输出:
abc00000
12345678
90000000
1 #include
2 #include
3
4 using namespacestd;5 intmain(){6 stringstr;7 while(cin >>str)8 {9 string temp(8, '0');10 if(str.size()<= 8)11 {12 temp.replace(0, str.size(), str);13 cout << temp <
进制转换
写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 )
输入一个十六进制的数值字符串。
输出该数值的十进制字符串。
输入:
0xA
输出:
10
1 #include
2 using namespacestd;3
4 intmain()5 {6 inta;7 while(cin >> hex >>a){8 cout << a <
质数因子
功能:输入一个正整数,按照从小到大的顺序输出它的所有质数的因子(如180的质数因子为2 2 3 3 5 )
1 #include
2 using namespacestd;3
4 intmain()5 {6 longa;7 while(cin >>a)8 {9 while(a != 1)10 {11 for(long j = 2; j<= a; j++)12 {13 if(a%j == 0)14 {15 a /=j;16 cout << j << ' ';17 break;18 }19 }20 }21 }22 return 0;23 }
坐标移动
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
输入:
合法坐标为A(或者D或者W或者S) + 数字(两位以内)
坐标之间以;分隔。
非法坐标点需要进行丢弃。如AA10; A1A; $%$; YAD; 等。
下面是一个简单的例子 如:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
处理过程:
起点(0,0)
+ A10 = (-10,0)
+ S20 = (-10,-20)
+ W10 = (-10,-10)
+ D30 = (20,-10)
+ x = 无效
+ A1A = 无效
+ B10A11 = 无效
+ 一个空 不影响
+ A10 = (10,-10)
结果 (10, -10)
输入:
一行字符串
输出:
最终坐标,以,分隔
#include #include#include#include#include
using namespacestd;intmain()
{stringinput;char separator = ';';string valid = "0123456789";int value = 0;while(getline(cin, input))
{
vector points(2, 0);
size_t new_start= 0;
size_t posSeparator=input.find(separator);while(posSeparator != string::npos)
{string current_command = input.substr(new_start, posSeparator -new_start);
new_start= posSeparator + 1;
posSeparator=input.find(separator, new_start);if(current_command.size() <=1 || current_command.size() >3)continue;if(current_command[0] == 'A' ||current_command[0] == 'S'||current_command[0] == 'D'||current_command[0] == 'W')
{string small = current_command.substr(1);if(small.find_first_not_of(valid) == string::npos )
{
value= stoi(small, nullptr, 10);if(current_command[0] == 'A')
points[0] -=value;else if(current_command[0] == 'D')
points[0] +=value;else if(current_command[0] == 'W')
points[1] +=value;else if(current_command[0] == 'S')
points[1] -=value;
}
}
}
cout<< points[0] << ',' << points[1] <
}return 0;
}
汽水瓶换饮料
有这样一道智力题:“某商店规定:三个空汽水瓶可以换一瓶汽水。小张手上有十个空汽水瓶,她最多可以换多少瓶汽水喝?”答案是5瓶,方法如下:先用9个空瓶子换3瓶汽水,喝掉3瓶满的,喝完以后4个空瓶子,用3个再换一瓶,喝掉这瓶满的,这时候剩2个空瓶子。然后你让老板先借给你一瓶汽水,喝掉这瓶满的,喝完以后用3个空瓶子换一瓶满的还给老板。如果小张手上有n个空汽水瓶,最多可以换多少瓶汽水喝?
对于每组测试数据,输出一行,表示最多可以喝的汽水瓶数。如果一瓶也喝不到,输出0。
输入:
3
10
81
0
输出:
1
5
40
#include
using namespacestd;intmain()
{intinput;while(cin >>input)
{int drink = 0;if(input == 0) break;if(input == 1) cout << drink <
full= empty / 3;
drink+=full;
remain= empty % 3;
empty= full +remain;
}if(remain == 2)
drink++;
cout<< drink <
}return 0;
}
最小公倍数
输入两个正整数A和B
输出A和B的最小公倍数
最小公倍数 = A x B / 最大公约数
#include#include
using namespacestd;int gcd(int a, intb);intmain(){inta, b;intminMax;while(cin >> a >>b){
minMax= a*b/gcd(a,b);
cout<< minMax <
}
}int gcd(int a, intb)
{if(b == 0)returna;return gcd(b, a%b);
}
求解立方根
输入:double 待求解参数
返回值:double 输入参数的立方根,保留一位小数
牛顿迭代法
#include#include#include
using namespacestd;intmain()
{double a=0.0, t = 0.0;double e = 0.0001;while(cin >>a){
t=a;while(abs(t*t*t - a) >e){
t= t - ( t*t*t - a )* 1.0 / (3 * t*t);
}
cout<< setiosflags(ios::fixed)<< setprecision(1) << t <
}return 0;
}