继续刷水题的节奏,话说回来,我今天才想起来,假设要把一个数组的值都设为同一个,可以这样写:
bool test[10] = {true}; //数组中值全为true
我以前全都是用一个循环来赋值。。。蠢哭ing。。。
-
1263
: 标准水题,就是字符串的基本操作,题目不难读懂,就是根据keyword
中字母在字母表中的位置,把加密后的字符串恢复到正确的顺序。中间还因为数组越界WA了两次,这让我深刻的明白,
当题目中说数组大小不大于100时,一定不能设个大小就为100的数组
。。。
代码如下:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main(){
int keyword_len, cipher_len, letter_used, tmp;
char keyword[20], ciphertext[200], result[200];
while(cin >> keyword){
if(!strcmp(keyword, "THEEND"))
break;
else{
keyword_len = strlen(keyword);
letter_used = 0;
cin >> ciphertext;
cipher_len = strlen(ciphertext);
result[strlen(ciphertext)] = '\0';
for(int i = 65; i < 91; i++){
for(int j = 0; j < keyword_len; j++){
tmp = j;
if(i == (int)keyword[j]){
for(int k = letter_used * cipher_len / keyword_len; k < (letter_used + 1) * cipher_len / keyword_len; k += 1, tmp += keyword_len)
result[tmp] = ciphertext[k];
letter_used += 1;
}
}
}
cout << result << endl;
}
}
}
-
1271
: 水的不能更水的题了,就是根据轮子的直径、转数、时间来求距离和速度,给后来人一点提示,题目中给的三个数据的单位分别是inch
、转
和秒
。代码如下:
#include <iostream> #include <iomanip> #define Pi 3.1415927 using namespace std; int main(){ double diameter, time, distance, MPH; int revolutions, count = 0; while(cin >> diameter >> revolutions >> time){ if(revolutions == 0) break; else{ cout << "Trip #" << ++count << ": "; distance = diameter * Pi * (double)revolutions / 12.0 / 5280.0; cout << fixed << setprecision(2) << distance << ' ' << distance * 3600.0 / time << endl; } } }