10/05/27 08:43:08
>>319
だからきちんと問題書けよ。つーか、課題丸投げなんだろ。宿題スレに逝けよ。
332:デフォルトの名無しさん
10/05/27 23:13:26
>>330
問題文を短くしてgets()を使わなければ突っ込まれないと短絡的に考えたのだろうが
あいにくURLエンコーディングには英数字の平文が混ざるので16進数ではないし
実装を比べるスレの趣旨からしてscanf()を指定する必然性も全くないし
URLデコードは文字列から文字列への変換なんだからバイナリーとか発してる時点で馬科決定。
関数の使い方もコンピュータ用語も理解してないレベルで出題するなよ。
333:デフォルトの名無しさん
10/05/28 00:32:43
#include <stdio.h>
#define ENOUGH_LONG_LENGTH 24
int main(void)
{
char buf[ENOUGH_LONG_LENGTH];
FILE *fp;
int miyazaki = 0xefBeadDe;
strcpy(buf, "ef Be ad De");
fp = fopen("test.txt", "wb");
fwrite(&miyazaki, sizeof(int), 1, fp);
fclose(fp);
return 0;
}
334:デフォルトの名無しさん
10/05/30 10:54:03
お題 (次>>350) 文字コードに関係の無いURLデコード
%と16進文字2桁を標準入力(コマンド引数、ファイル以外)から入力して、
バイナリーに変換し、test.txtファイルに落とす。
%の後に2桁の16進文字が続かない場合は、変換しないでそのまま出力する。
例:
%Y %0X ⇒ 出力無し
%Y0 ⇒ '0'出力
%00 ⇒ '\0'出力
%Aa01 ⇒ '\xaa' '0' '1'出力
335:デフォルトの名無しさん
10/06/05 09:13:37
>>281 日本語でオス
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
const int NUM_SIZE = 5;
int main()
{
int i, temp = 0, sum = 0;
vector<int> intContiner;
cout << "5つ数を入力して下さい。" << endl;
for( i = 0 ; i < NUM_SIZE ; ++i ) {
// 上のfor文内でiを宣言するなら、sumやtempも移動すべき場所があるべきだろう。
cout << i + 1 << "番目入力" << endl;
cin >> temp;
intContiner.push_back( temp );
}
cout << "5つ数の合計" << endl;
vector<int>::iterator itr = intContiner.begin();
for( ; itr != intContiner.end() ; ++itr ) {
sum += *itr;
}
cout << sum << endl;
cout << "5つ数の平均" << endl;
cout << sum / NUM_SIZE << endl;
}
336:デフォルトの名無しさん
10/06/05 17:16:25
>>335
>282
337:デフォルトの名無しさん
10/06/06 15:00:48
問題:
ジョンにとって縁起のいい数字は4と7で,
他はどうでもいい.ラッキーナンバーとは,
縁起のいい数字を含んだ10進数である.
整数aとbが与えられたとき,aとbの間にあるラッキーナンバーの個数を挙げよ.
オブジェクトの定義:
クラス名 : TheLuckyNumbers
メソッド名 : count
引数の型 : int,int
返値の型 : int
メソッドの表記 : int count(ints,intb)
として,公的メソッドとせよ.
注:
aの値は1から1,000,000,000まで
bの値は1から1,000,000,000まで
TopCoderでグーグル先生に聞いたら出てきた問題を適当に張ってみる
338:デフォルトの名無しさん
10/06/06 19:38:26
a~b=(1~b)-(1~a){a,b:a<b}
ここまで分かった。
339:デフォルトの名無しさん
10/06/12 18:49:53
>>337
できたぞ
#include <iostream>
#include <cmath>
using namespace std;
class TheLuckyNumbers
{
public:
int count(int s, int b);
};
int TheLuckyNumbers::count(int s, int b)
{
int digits=0,temp;
int k=0;
if(s<1 || s > 1000000000 || b<1 || b > 1000000000) return 0;
for(int i=s;i<=b;i++)
{
temp=i;
while(temp){digits++;temp/=10;}
temp=i;
while(digits)
{
if(temp % 10 == 4 || temp % 10 == 7) k++;
temp/=static_cast<int>(pow(10.0,digits));
digits--;
}
}
return k;
}
340:339
10/06/12 19:07:36
temp/=static_cast<int>(pow(10.0,digits));
digits--;
じゃなくて
temp/=static_cast<int>(pow(10.0,--digits));
だな。
341:339
10/06/12 19:25:34
全然間違ってた
while(digits)
{
if(temp % 10 == 4 || temp % 10 == 7) k++;
temp/=10; digits--;
}
で最後だ
342:デフォルトの名無しさん
10/06/13 10:22:47
if(s<1 || s > 1000000000 || b<1 || b > 1000000000) return 0; は必要。
始めにdigitを求め無くても、tempを10で割って行ったら0になる、
そうするともっと早い修正になる。
343:デフォルトの名無しさん
10/06/13 10:55:40
>>341
7777とかだと一つの数字で+4されないか?
344:デフォルトの名無しさん
10/06/13 11:27:16
>>343
そうだな
7777→1個だな
まぁすぐ直せるだろうけど
345:344
10/06/13 11:51:22
#include <iostream>
using namespace std;
class TheLuckyNumbers
{
public:
int count(int s, int b);
};
int TheLuckyNumbers::count(int s, int b)
{
if(s<1 || s > 1000000000 || b<1 || b > 1000000000) return 0;
int digits=0,temp;
int k=0;
for(int i=s;i<=b;i++)
{
temp=i;
while(temp)
{
if(temp % 10 == 4 || temp % 10 == 7){ k++; break;}
temp/=10;
}
}
return k;
}
346:342
10/06/13 12:25:23
temp%10は2回やる必要なくてtmp=temp%10とかで、俺もやった。
347:デフォルトの名無しさん
10/06/13 13:13:26
Javaで貼ってもよかとですか?
348:デフォルトの名無しさん
10/06/13 13:24:40
>>347
リンクならOK
本体は
スレリンク(tech板)l1
349:347
10/06/13 13:33:56
ほい
スレリンク(tech板:674番)
350:348じゃばじんすげえ
10/06/13 14:47:13
class TheLuckyNumbers {
public: inline
static int count(int a, int b) {
if (a < 1 || a > 1000000000 || b < 1 || b > 1000000000 || a > b) return 0;
return count(b) - count(a - 1);
}
private: inline
static int count(int a) {
int result = 0, digits/*=0*/, add = 0;
if ((digits = search(a)) != 0) {
result = a % digits; a -= result; result += 1;
}
digits = 1;
while (a != 0) {
for (int i = 0, j = a % 10; i < j; i++) {
result += (i == 4 || i == 7) ? digits : add;
}
a /= 10; add = add * 8 + digits * 2; digits *= 10;
}
return result;
}
private: inline
static int search(int a) {
int result = 0;
for (int digits = 1; a != 0; digits *= 10, a /= 10) {
if (a % 10 == 4 || a % 10 == 7) {
result = digits;
}
}
return result;
}
};
351:デフォルトの名無しさん
10/06/14 00:35:58
>>350
コメントくらい書こうぜ。
つーか、問題文にはsの方がbより小さいとは書いてないみたいだけれど。
>>337
再帰でやってみた。
URLリンク(codepad.org)